# Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde.

# This file is part of miscbits-el.
#
# miscbits-el is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# miscbits-el is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with miscbits-el; see the file COPYING.  Failing that, go to
# <http://www.gnu.org/licenses/>.


# Basic usage is
#
#     make all
#     make check      # optional author checks only, really
#     make install
#
# The install is to $(lispdir) below, override it for somewhere different.
# Override "prefix" or "EMACS" too if desired, eg.
#
#     make prefix=/my/tree EMACS=xemacs
#

VERSION = 182

prefix = /usr/local
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
datarootdir = $(prefix)/share
lispdir = $(datarootdir)/emacs/site-lisp
mandir = $(datarootdir)/man
man1dir = $(mandir)/man1
DESTDIR =
EMACS = emacs


EL_FILES = \
  $(EL_FILES_COMPILE)  
EL_FILES_COMPILE =		\
  ac-tex-ref.el			\
  ac-tex-macros-used.el		\
  align-let.el			\
  arc-lzh-exe.el		\
  areabrowse.el			\
  bytecomp-simplify.el		\
  checkdoc-autoload.el		\
  checkdoc-batch.el		\
  checkdoc-keywords.el		\
  checkdoc-url-buttonize.el	\
  cl-loop-aplist.el		\
  compilation-always-kill.el	\
  compilation-emacs21-batch.el	\
  compilation-ledger.el		\
  compilation-onewin.el		\
  compilation-perl.el		\
  compilation-recenter-end.el	\
  compile-command-default.el	\
  compile-history-local.el	\
  completion-ignored-build.el	\
  copyright-both-ends.el	\
  diff-save-buffer.el		\
  dired-visit-history.el	\
  do-not-edit.el		\
  elisp-docstring-preview.el	\
  ffap-gcc-path.el		\
  ffap-href.el			\
  ffap-I-option.el		\
  ffap-include-start.el		\
  ffap-makefile-vars.el		\
  ffap-mml.el			\
  ffap-perl-module.el		\
  ffap-pod-F.el			\
  ffap-rfc-directories.el	\
  ffap-rfc-space.el		\
  filladapt-pat.el		\
  formfeed-hline.el		\
  gtk-look.el			\
  gutenberg-coding.el		\
  help-xref-elfiles.el		\
  html-coding.el		\
  html-sizes.el			\
  imagetext.el			\
  info-lookmore.el		\
  ispell-stopwords.el		\
  jka-compr-dictzip.el		\
  jka-compr-lz4.el		\
  localedef-overlay.el		\
  makeinfo-info.el		\
  make-mode-fillindent.el	\
  mailcap-mode.el		\
  man-completion.el		\
  man-preview.el		\
  mine-sweeper-auto.el		\
  mo-mode.el			\
  nobreak-fade.el		\
  nroff-filladapt.el		\
  oeis.el			\
  parasep.el			\
  perl-env-substitute.el	\
  perl-pod-coding.el		\
  perl-pod-gt.el		\
  perl-pod-preview.el		\
  perl-quote.el			\
  perl-use-utf8-coding.el	\
  pst-format.el			\
  quick-yes.el			\
  renice.el			\
  scroll-1.el			\
  sentence-spacing.el		\
  sh-autoconf.el		\
  sqlite-dump.el		\
  startup-file-name-history.el	\
  texinfo-nobreak.el		\
  texinfo-warn.el		\
  tex-increase.el		\
  tex-math-preview.el		\
  tty-format.el			\
  unicode-disp.el		\
  upstr.el                      \
  variable-at-point-tick.el	\
  wx-look.el                    \
  xml-coding.el

ELC_FILES =			\
  ac-tex-ref.elc		\
  ac-tex-macros-used.elc	\
  align-let.elc			\
  arc-lzh-exe.elc		\
  areabrowse.elc		\
  bytecomp-simplify.elc		\
  checkdoc-autoload.elc		\
  checkdoc-batch.elc		\
  checkdoc-keywords.elc		\
  checkdoc-url-buttonize.elc	\
  cl-loop-aplist.elc		\
  compilation-always-kill.elc	\
  compilation-emacs21-batch.elc	\
  compilation-ledger.elc	\
  compilation-onewin.elc	\
  compilation-perl.elc		\
  compilation-recenter-end.elc	\
  compile-command-default.elc	\
  compile-history-local.elc	\
  completion-ignored-build.elc	\
  copyright-both-ends.elc	\
  diff-save-buffer.elc		\
  dired-visit-history.elc	\
  do-not-edit.elc		\
  elisp-docstring-preview.elc	\
  ffap-gcc-path.elc		\
  ffap-href.elc			\
  ffap-I-option.elc		\
  ffap-include-start.elc	\
  ffap-makefile-vars.elc	\
  ffap-mml.elc			\
  ffap-perl-module.elc		\
  ffap-pod-F.elc		\
  ffap-rfc-directories.elc	\
  ffap-rfc-space.elc		\
  filladapt-pat.elc		\
  formfeed-hline.elc		\
  gtk-look.elc			\
  gutenberg-coding.elc		\
  help-xref-elfiles.elc		\
  html-coding.elc		\
  html-sizes.elc		\
  imagetext.elc			\
  info-lookmore.elc		\
  ispell-stopwords.elc		\
  jka-compr-dictzip.elc		\
  jka-compr-lz4.elc		\
  localedef-overlay.elc		\
  makeinfo-info.elc		\
  make-mode-fillindent.elc	\
  mailcap-mode.elc		\
  man-completion.elc		\
  man-preview.elc		\
  mine-sweeper-auto.elc		\
  mo-mode.elc			\
  nobreak-fade.elc		\
  nroff-filladapt.elc		\
  parasep.elc			\
  perl-env-substitute.elc	\
  perl-pod-coding.elc		\
  perl-pod-gt.elc		\
  perl-pod-preview.elc		\
  perl-quote.elc		\
  perl-use-utf8-coding.elc	\
  pst-format.elc		\
  quick-yes.elc			\
  renice.elc			\
  scroll-1.elc			\
  sentence-spacing.elc		\
  sh-autoconf.elc		\
  sqlite-dump.elc		\
  startup-file-name-history.elc	\
  texinfo-nobreak.elc		\
  texinfo-warn.elc		\
  tex-increase.elc		\
  tex-math-preview.elc		\
  tty-format.elc		\
  unicode-disp.elc		\
  upstr.elc                     \
  variable-at-point-tick.elc	\
  wx-look.elc                   \
  xml-coding.elc

.SUFFIXES: .el .elc
.el.elc:
	$(EMACS) -q -no-site-file -batch \
	  -l tools/miscbits-byte-compile.el \
	  -f batch-byte-compile \
	  $<

all: miscbits-el-loaddefs.elc $(ELC_FILES)

miscbits-el-loaddefs.elc $(ELC_FILES): miscbits-el-loaddefs.el $(EL_FILES) tools/miscbits-byte-compile.el
	$(EMACS) -q -no-site-file -batch \
	  -l tools/miscbits-byte-compile.el \
	  -f batch-byte-compile \
	  miscbits-el-loaddefs.el $(EL_FILES)
	ls -l miscbits-el-loaddefs.el miscbits-el-loaddefs.elc

# Autoload generated for defcustom and defgroup vary slightly between
# xemacs21, emacs21 and emacs22, so create the loaddefs for the particular
# $(EMACS) at build time.
#
loaddefs: miscbits-el-loaddefs.el
miscbits-el-loaddefs.el: $(EL_FILES) tools/miscbits-generate-loaddefs.el
	$(EMACS) -q -no-site-file -batch \
	  -l tools/miscbits-generate-loaddefs.el \
	  -f my-loaddefs-batch \
	  $(EL_FILES)
	ls -l miscbits-el-loaddefs.el

clean:
	rm -f miscbits-el-loaddefs.el *.elc \
	  tests/t-mo-mode--temp.mo \
	  tests/t-mo-mode--temp.mo~

INSTALL_FILES =		\
  miscbits-el-loaddefs.el	\
  miscbits-el-loaddefs.elc	\
  $(EL_FILES)		\
  $(ELC_FILES)

install: $(INSTALL_FILES)
	$(MAKE) -$(MAKEFLAGS) all
	install -m 755 -d $(DESTDIR)$(lispdir)
	install -m 644 $(INSTALL_FILES) $(DESTDIR)$(lispdir)
	install -m 755 -d $(DESTDIR)$(bindir)
	install -m 755 emacs-checkdoc-batch   $(DESTDIR)$(bindir)/emacs-checkdoc-batch
	install -m 755 -d $(DESTDIR)$(man1dir)
	install -m 644 emacs-checkdoc-batch.1 $(DESTDIR)$(man1dir)/emacs-checkdoc-batch.1

TEST_FILES = t-*.el
test check: all check-uncomp
test-uncomp check-uncomp: miscbits-el-loaddefs.el
	pass=0; fail=0; \
	cd tests; \
	for i in $(TEST_FILES); do \
	  echo "$(EMACS) -q -no-site-file -batch -l $$i -f my-run-test"; \
	  if $(EMACS) -q -no-site-file -batch -l $$i -f my-run-test; then \
	    pass=`expr $$pass + 1`; \
	  else \
	    echo "  ... fail"; \
	    fail=`expr $$fail + 1`; \
          fi \
	done; \
	echo "Tests pass $$pass fail $$fail"; \
	test $$fail -eq 0

# No, must be at start of file ...
test-lex:
	make clean
	make EMACS="$(EMACS) --load devel/d-lexical-force.el" all
	make test

tests/t-mo-mode.mo: tests/t-mo-mode.po
	msgfmt -o tests/t-mo-mode.mo tests/t-mo-mode.po

tests/t-sqlite-dump.sq2: tests/t-sqlite-dump.sql
	rm -f tests/t-sqlite-dump.sq2
	sqlite tests/t-sqlite-dump.sq2 <tests/t-sqlite-dump.sql
tests/t-sqlite-dump.sq3: tests/t-sqlite-dump.sql
	rm -f tests/t-sqlite-dump.sq3
	sqlite3 tests/t-sqlite-dump.sq3 <tests/t-sqlite-dump.sql


#------------------------------------------------------------------------------
# development

DISTNAME = miscbits-el
DISTVNAME = $(DISTNAME)-$(VERSION)
DEBNAME = $(DISTNAME)
DEBFILE = $(DEBNAME)_$(VERSION)-0.1_all.deb

version:
	@echo $(VERSION)

DIST_FILES =			\
  COPYING			\
  README			\
  Makefile			\
  emacs-checkdoc-batch		\
  emacs-checkdoc-batch.1	\
  tools/*.el			\
  tools/*.sh			\
  $(EL_FILES)			\
  tests/Makefile		\
  tests/*.el			\
  tests/*.txt			\
  tests/*.txt.gz		\
  tests/*.po			\
  tests/*.mo			\
  tests/*.png			\
  tests/*.sq2			\
  tests/*.sq3			\
  tests/*.sql			\
  tests/*.xpm			\
  coming-soon/*			\
  devel/Makefile		\
  devel/*.1			\
  devel/*.c			\
  devel/*.el			\
  devel/*.devhelp2		\
  devel/*.html			\
  devel/*.ledger		\
  devel/*.ltx			\
  devel/*.makefile		\
  devel/*.man			\
  devel/*.mailcap		\
  devel/*.org			\
  devel/*.pl			\
  devel/*.pm			\
  devel/*.pod			\
  devel/*.pst0			\
  devel/*.script		\
  devel/*.sh			\
  devel/*.sqdb			\
  devel/*.sql			\
  devel/*.t			\
  devel/*.tar			\
  devel/*.tex			\
  devel/*.texi			\
  devel/*.txt			\
  devel/*.xs			\
  devel/*.zip			\
  devel/configure.in		\
  devel/sqlite-dump/Makefile	\
  devel/sqlite-dump/*.c		\
  unused/*.el			\
  unused/*.pl			\
  debian/changelog		\
  debian/compat			\
  debian/control		\
  debian/copyright		\
  debian/emacsen-compat		\
  debian/emacsen-install	\
  debian/emacsen-remove		\
  debian/emacsen-startup	\
  debian/NEWS.Debian		\
  debian/rules			\
  debian/source/format		\
  debian/watch

dist: $(DISTVNAME).tar.gz $(DIST_FILES)
	ls -l $(DISTVNAME).tar.gz

$(DISTVNAME).tar.gz: $(DIST_FILES)
	$(MAKE) -$(MAKEFLAGS) distdir
	tar cf $(DISTVNAME).tar --sort=name $(DISTVNAME)
	gzip -9 --force $(DISTVNAME).tar
	rm -rf $(DISTVNAME)

distdir:
	rm -rf $(DISTVNAME)
	mkdir $(DISTVNAME)
	cp -p --parents $(DIST_FILES) $(DISTVNAME)

# find files in the dist with mod times this year, but without this year in
# the copyright line
check-copyright-years: $(DISTVNAME).tar.gz
	year=`date +%Y`; \
	tar tvfz $(DISTVNAME).tar.gz \
	| grep $$year- \
	| sed 's:^.*$(DISTVNAME)/::' \
	| (result=0; \
	  while read i; do \
	    case $$i in \
	      '' \
	      | */ \
	      | COPYING \
	      | *.pst0 \
	      | *.mo \
	      | *.zip \
	      | *.png \
	      | tests/file-expand-wildcards.el \
	      | tests/*.sq2 | tests/*.sq3 \
	      | debian/compat \
	      | debian/emacsen-compat \
	      | debian/changelog \
	      | debian/source/format) \
	      continue ;; \
	    esac; \
	    if ! zgrep -q "Copyright.*$$year" $$i; then \
	      echo "$$i:1: this file"; \
	      grep Copyright $$i; \
	      result=1; \
	    fi; \
	  done; \
	  exit $$result)

check-spelling:
	if egrep -nHi '[o]mmitt?ed|[o]mited|[e]xistant|[e]xplict|[a]gument|[d]estionation|\b[t]he the\b|\b[n]ote sure\b' -r . \
	  | egrep -v '(dist-deb).*grep -nH'; \
	then false; else true; fi

check-file-part-of:
	if grep --text 'This file is'' part of ' -r . | fgrep -v '~:' | egrep -iv miscbits; then false; else true; fi

myman:
	: EL_FILES
	@ls $(EL_FILES) *.el | sort | uniq -u
	: ELC_FILES
	@perl -e 'for(@ARGV){s/el$$/elc/;print"$$_\n"}' \
	  $(EL_FILES) $(ELC_FILES) | sort | uniq -u
	: DIST_FILES
	@(ls $(DIST_FILES) dist-deb dist-deb.log; \
	  echo $(DEBFILE); \
	  echo $(DEBFILE); \
	  echo $(DISTVNAME).tar.gz; \
	  echo $(DISTVNAME).tar.gz; \
	  find . ! -name \*~ -printf '%P\n') | sort | uniq -u

myman2:
	: EL_FILES versus '*.el'
	@ls $(EL_FILES) *.el | sort | uniq -u
	: DIST_FILES versus '*.el'
	@ls $(DIST_FILES) $(DIST_FILES) *.el | sort | uniq -u
	: EL_FILES versus ELC_FILES
	@perl -e 'for(@ARGV){s/elc$$/el/;print"$$_\n"}' \
	  $(EL_FILES) $(ELC_FILES) | sort | uniq -u

diff-prev:
	rm -rf /tmp/miscbits-el-diff-prev
	mkdir /tmp/miscbits-el-diff-prev
	tar -C /tmp/miscbits-el-diff-prev -xz -f miscbits-el-$(VERSION).tar.gz
	tar -C /tmp/miscbits-el-diff-prev -xz -f miscbits-el-`expr $(VERSION) - 1`.tar.gz
	cd /tmp/miscbits-el-diff-prev; \
	  diff -Naur miscbits-el-`expr $(VERSION) - 1` miscbits-el-$(VERSION) | less

diff-lisp:
	(for i in $(EL_FILES); do \
	  if test -f $(HOME)/lisp/$$i; then \
	    diff -u $$i $(HOME)/lisp/$$i; \
	  fi \
	done) | less

diff-wip:
	(for i in $(EL_FILES); do \
	  if test -f wip/$$i; then \
	    diff -u $$i wip/$$i; \
	  fi \
	done) | less

diff-tux:
	(for i in $(EL_FILES); do \
	  diff -u $(HOME)/tux/data/$$i $$i; \
	done) | less

checkdoc:
	./emacs-checkdoc-batch *.el
count:
	ls $(EL_FILES) | wc -l

el-missing:
	@ls *.el $(EL_FILES) | sort | uniq -u
	@(ls *.el | sed 's/el$$/elc/'; \
	 for i in $(ELC_FILES); do echo $$i; done) \
	  | sort | uniq -u

my-list:
	ls -l -U *.tar.gz *.deb
	@echo -n '$(DEBFILE) '
	@dpkg-deb -f $(DEBFILE) | grep Installed-Size 

.PHONY: all clean maintainer-clean install loaddefs deb diff-prev dist distdir version test check test-uncomp check-uncomp
