INSTALL      = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
prefix       = @prefix@
datadir      = @datadir@
lispdir      = @lispdir@
PACKAGEDIR   = @PACKAGEDIR@
ICONDIR      = @ICONDIR@
ADDITIONAL_LOAD_PATH = @ADDITIONAL_LOAD_PATH@

SHELL        = /bin/sh

@SET_MAKE@
EMACS   = @EMACS@
FLAGS   = -q -no-site-file -batch -l w3mhack.el $(ADDITIONAL_LOAD_PATH)

PACKAGE = emacs-w3m
TARBALL = $(PACKAGE)-$(VERSION).tar.gz
DISTDIR = $(PACKAGE)-$(VERSION)
DOCS    = COPYING ChangeLog ChangeLog.1 FAQ FAQ.ja README README.ja \
	  README.namazu.ja README.shimbun.ja TIPS TIPS.ja
DISTS   = Makefile.in aclocal.m4 configure configure.in install-sh mkinstalldirs


default: Makefile
	env test ! -f w3m-util.elc -o w3m-util.elc -nt w3m-util.el || $(MAKE) clean
	env test ! -f w3m-proc.elc -o w3m-proc.elc -nt w3m-proc.el || $(MAKE) clean
	$(EMACS) $(FLAGS) -f w3mhack-compile

what-where:
	@$(EMACS) $(FLAGS) -f w3mhack-what-where\
	"$(lispdir)" "$(ICONDIR)" "$(PACKAGEDIR)"

install: default
	@$(SHELL) ./mkinstalldirs $(lispdir);\
	for p in ChangeLog* *.el; do\
	  echo " $(INSTALL_DATA) $$p $(lispdir)/$$p";\
	  $(INSTALL_DATA) $$p $(lispdir)/$$p;\
	done;\
	for p in *.elc; do\
	  if test -f "$$p"; then\
	    echo " $(INSTALL_DATA) $$p $(lispdir)/$$p";\
	    $(INSTALL_DATA) $$p $(lispdir)/$$p;\
	  fi;\
	done;\
	if test -f shimbun/shimbun.elc; then\
	  for p in `cd shimbun && echo *.el`; do\
	    echo " $(INSTALL_DATA) shimbun/$$p $(lispdir)/$$p";\
	    $(INSTALL_DATA) shimbun/$$p $(lispdir)/$$p;\
	  done;\
	  for p in `cd shimbun && echo *.elc`; do\
	    echo " $(INSTALL_DATA) shimbun/$$p $(lispdir)/$$p";\
	    $(INSTALL_DATA) shimbun/$$p $(lispdir)/$$p;\
	  done;\
	fi

install-icons:
	@if test $(ICONDIR) = NONE; then\
	  echo "You don't have to install icon files for \"$(EMACS)\".";\
	else\
	  $(SHELL) ./mkinstalldirs $(ICONDIR);\
	  for i in `cd icons && echo *.xpm`; do\
	    echo " $(INSTALL_DATA) icons/$$i $(ICONDIR)/$$i";\
	    $(INSTALL_DATA) icons/$$i $(ICONDIR)/$$i;\
	  done;\
	fi

install-package:
	@if test $(PACKAGEDIR) = NONE; then\
	  echo "What a pity!  Your \"$(EMACS)\" does not support"\
		"a package system.";\
	else\
	  $(MAKE) lispdir="$(PACKAGEDIR)/lisp/w3m" install;\
	  $(MAKE) ICONDIR="$(PACKAGEDIR)/etc/w3m" install-icons;\
	  echo "$(EMACS) $(FLAGS) -f w3mhack-make-package $(PACKAGEDIR)";\
	  $(EMACS) $(FLAGS) -f w3mhack-make-package $(PACKAGEDIR);\
	fi

dist: Makefile
	$(MAKE) VERSION=`$(EMACS) $(FLAGS) -f w3mhack-version 2>/dev/null` tarball

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

configure: configure.in aclocal.m4
	autoconf

tarball: $(DOCS) $(DISTS)
	-rm -f $(TARBALL) `basename $(TARBALL) .gz`
	mkdir $(DISTDIR)
	cp -p $(DOCS) $(DISTS) *.el $(DISTDIR)
	mkdir $(DISTDIR)/attic
	cp -p `echo attic/*|sed s,attic/CVS,,` $(DISTDIR)/attic
	mkdir $(DISTDIR)/icons
	cp -p icons/*.xpm $(DISTDIR)/icons
	mkdir $(DISTDIR)/patches
	cp -p `echo patches/*|sed s,patches/CVS,,` $(DISTDIR)/patches
	mkdir $(DISTDIR)/shimbun
	cp -p shimbun/*.el $(DISTDIR)/shimbun
	find $(DISTDIR) -type d | xargs chmod 755
	find $(DISTDIR) -type f | xargs chmod 644
	chmod 755 $(DISTDIR)/configure $(DISTDIR)/install-sh
	tar -cf `basename $(TARBALL) .gz` $(DISTDIR)
	gzip -9 `basename $(TARBALL) .gz`
	rm -rf $(DISTDIR)

clean:
	-rm -rf *~ *.elc shimbun/*.elc $(PACKAGE)* w3m-kwds.el

distclean: clean
	-rm -f config.log config.status config.cache Makefile;\
	rm -fr autom4te*.cache

## Rules for the developers to check a portability for each module.
.SUFFIXES: .elc .el

.el.elc:
	echo "$(EMACS) $(FLAGS) -f batch-byte-compile $*.el";\
	$(EMACS) $(FLAGS) -f batch-byte-compile $*.el

slow: Makefile
	@for i in `$(EMACS) $(FLAGS) -f w3mhack-examine-modules 2>/dev/null`;\
	do $(MAKE) -s $$i; done

very-slow: clean Makefile
	@args="-q -no-site-file -batch";\
	args="$$args -l attic/addpath.el $(ADDITIONAL_LOAD_PATH)";\
	echo "=========================================";\
	echo "Compiling 1st stage-----without elc files";\
	echo "=========================================";\
	for i in `$(EMACS) $(FLAGS) -f w3mhack-examine-modules 2>/dev/null`;\
	  do\
	  j=`echo $$i| sed 's/elc$$/el/g'`;\
	  echo "$(EMACS) ARGS -f batch-byte-compile $$j";\
	  $(EMACS) $$args -f batch-byte-compile $$j;\
	  mv $$i $$j"x";\
	done;\
	for i in `echo *.elx shimbun/*.elx`; do\
	  j=`echo $$i| sed 's/elx$$/elc/g'`;\
	  mv $$i $$j;\
	done;\
	echo "==========================================";\
	echo "Compiling 2nd stage-----with all elc files";\
	echo "==========================================";\
	for i in `$(EMACS) $(FLAGS) -f w3mhack-examine-modules 2>/dev/null`;\
	  do\
	  j=`echo $$i| sed 's/elc$$/el/g'`;\
	  echo "$(EMACS) ARGS -f batch-byte-compile $$j";\
	  $(EMACS) $$args -f batch-byte-compile $$j;\
	done

## Create the file w3m-kwds.el if it is needed for $(EMACS).
keywords:
	@ echo "$(EMACS) $(FLAGS)";\
	$(EMACS) $(FLAGS);\
	if test ! -f w3m-kwds.el; then\
	  echo "There is no need to bind colon keywords for \"$(EMACS)\".";\
	  echo "The file w3m-kwds.el is not created.";\
	fi
