# Makefile for MH-E.

# Copyright (C) 2003 Bill Wohler

# This file is part of MH-E.

# MH-E 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 2, or (at your option)
# any later version.

# MH-E 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 MH-E; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#
# Note: most changes to this file should also be mirrored in the Makefiles in
# $(TOP)/doc and $(TOP)/contrib.
#
TOP		= ..

EMACS_HOME	= $(TOP)/../emacs

EMACS		= emacs

EMACS_OPTIONS   = -batch --no-site-file --multibyte \
	--eval "(setq load-path (cons \".\" load-path))"

COMPILE_COMMAND	= $(EMACS) $(EMACS_OPTIONS)

# XEmacs variables.
XEMACS_OPTIONS  = -batch -vanilla -eval \("push default-directory load-path"\)

AUTO_PRELOADS = -eval \("setq autoload-package-name \"mh-e\""\)

# Source that goes in tarball and Emacs lisp/mh-e directory.
MH-E-SRC	=			\
        mh-alias.el                     \
        mh-comp.el                      \
        mh-customize.el                 \
        mh-e.el                         \
        mh-funcs.el                     \
        mh-mime.el                      \
        mh-pick.el                      \
        mh-inc.el                       \
        mh-index.el                     \
        mh-identity.el                  \
        mh-junk.el                      \
        mh-seq.el                       \
	mh-speed.el			\
        mh-utils.el			\
	mh-xemacs-compat.el		\
	mh-xemacs-icons.el

MH-E-LOADDEFS	=			\
        mh-loaddefs.el

MH-E-OTHERS	=			\
	mh-gnus.el			\
	$(MH-E-LOADDEFS)		\
	ChangeLog

MH-E-OBJ =  $(MH-E-SRC:.el=.elc)

# Images that go in tarball and Emacs lisp/toolbar directory.
MH-E-IMG	=			\
	alias.pbm			\
	alias.xpm			\
	execute.pbm			\
	execute.xpm			\
	highlight.xpm			\
	highlight.pbm			\
	mh-logo.xpm			\
	page-down.pbm			\
	page-down.xpm			\
	refile.pbm			\
	refile.xpm			\
	repack.pbm			\
	repack.xpm			\
	reply-all.pbm			\
	reply-all.xpm			\
	reply-from.pbm			\
	reply-from.xpm			\
	reply-to.pbm			\
	reply-to.xpm			\
	rescan.pbm			\
	rescan.xpm			\
	show.pbm			\
	show.xpm			\
	widen.pbm			\
	widen.xpm

# Images that go in tarball and Emacs lisp/mail directory.
MH-E-IMG2	=			\
	mail/reply2.pbm			\
	mail/reply2.xpm

# Files that go in tarball and Emacs etc directory.
MH-E-ETC        = 			\
	MH-E-NEWS

# Files that go in tarball only.
MH-E-ETC-ETC	=			\
	COPYING				\
	Makefile			\
	README

# Default target. Works for both Emacs and XEmacs.
all:
	case $(EMACS) in \
	*xemacs*) \
	    $(MAKE) EMACS='$(EMACS)' \
		EMACS_OPTIONS='$(XEMACS_OPTIONS)' \
		EMACS_HOME='$(EMACS_HOME)' \
		xemacs ;;\
	*) \
	     $(MAKE) EMACS='$(EMACS)' \
		EMACS_HOME='$(EMACS_HOME)' \
		emacs ;;\
	esac

# The .PHONY targets are always considered to be out-of-date.
# This rule needs to be after the ``all'' rule for compatibility 
# with old versions of make.
.PHONY: all emacs xemacs clean compile

# Ensure clean compile under Emacs and XEmacs which is advised since
# old .elc files can interfere with the compilation process.
emacs: clean autoloads compile

xemacs: clean auto-autoloads.elc custom-load.elc compile

# Remove everything we make.
clean::
	rm -rf $(MH-E-OBJ) auto-autoloads.el* custom-load.el*

# Compile MH-E. See $EMACS_HOME/lisp/Makefile for commentary.
compile: $(MH-E-SRC)
	$(COMPILE_COMMAND) -f batch-byte-compile $?

# Automatically extract autoloads.
# This can be used only with GNU Emacs. If you are using XEmacs please
# use:
#   make EMACS=/path/to/GNU/Emacs mh-loaddefs.el
# to regnerate mh-loaddefs.el.
.PHONY: autoloads
autoloads: $(MH-E-LOADDEFS)
mh-loaddefs.el: $(MH-E-SRC)
	echo ";;; mh-loaddefs.el --- automatically extracted autoloads" > $@
	echo ";;" >> $@
	echo ";;; Copyright (C) 2003 Free Software Foundation, Inc." >> $@
	echo ";;; Author: Bill Wohler <wohler@newt.com>" >> $@
	echo ";;; Keywords: mail" >> $@
	echo ";;; Commentary:" >> $@
	echo ";;; Change Log:" >> $@
	echo ";;; Code:" >> $@
	$(COMPILE_COMMAND) -l autoload \
          --eval '(setq generate-autoload-cookie ";;;###mh-autoload")' \
          --eval '(setq generated-autoload-file (expand-file-name "mh-loaddefs.el"))' \
          --eval '(setq make-backup-files nil)' \
         -f batch-update-autoloads .
	echo "" >> $@
	echo "(provide 'mh-loaddefs)" >> $@
	echo ";;; Local Variables:" >> $@
	echo ";;; version-control: never" >> $@
	echo ";;; no-byte-compile: t" >> $@
	echo ";;; no-update-autoloads: t" >> $@
	echo ";;; End:" >> $@
	echo ";;; mh-loaddefs.el ends here" >> $@

# XEmacs targets.
auto-autoloads.elc:
	$(COMPILE_COMMAND) $(AUTO_PRELOADS) -f batch-update-directory ./
	$(COMPILE_COMMAND) -f batch-byte-compile ./auto-autoloads.el

custom-load.elc: 
	$(COMPILE_COMMAND) -f Custom-make-dependencies ./
	$(COMPILE_COMMAND) -f batch-byte-compile ./custom-load.el

#
# The following targets are only of interest to maintainers.
#
.PHONY: dist install-emacs import-emacs

# Create a distribution suitable for installing at SourceForge.
dist: all
	@echo "Determining release..."
	@TAG=`cvs stat Makefile | awk '/Sticky Tag/{print $$3}'`; \
        if [ "$$TAG" = "(none)" ]; then \
            echo "No sticky tags here. Do a 'cvs update -rmh-e-M_N'."; \
            exit 1; \
        fi; \
        RELEASE=`echo $$TAG | sed 's/_/./g'`; \
	echo "Building $$RELEASE..."; \
        rm -rf $$RELEASE; \
        mkdir $$RELEASE $$RELEASE/mail; \
        cp $(MH-E-SRC) $(MH-E-OTHERS) \
           $(MH-E-ETC) $(MH-E-ETC-ETC) $(MH-E-IMG) $$RELEASE; \
        cp $(MH-E-IMG2) $$RELEASE/mail; \
        tar -czhf $$RELEASE.tgz $$RELEASE

# Install the source into the Emacs source.
install-emacs:
	cp $(MH-E-ETC) $(EMACS_HOME)/etc
	cp $(MH-E-SRC) $(MH-E-OTHERS) $(EMACS_HOME)/lisp/mh-e
	cp $(MH-E-IMG) $(EMACS_HOME)/lisp/toolbar
	cp $(MH-E-IMG2) $(EMACS_HOME)/lisp/mail

# View the CVS logs for the Emacs versions (to see if they have been modified).
emacs-logs:
	(cd $(EMACS_HOME)/etc; cvs log $(MH-E-ETC))
	(cd $(EMACS_HOME)/lisp/mail; cvs log $(MH-E-SRC) $(MH-E-OTHERS))
	(cd $(EMACS_HOME)/lisp/toolbar; cvs log $(MH-E-IMG))
	(cd $(EMACS_HOME)/lisp/mail; cvs log $(MH-E-IMG2))

# Import Emacs changes.
import-emacs:
	import-emacs

# The unit test that finds unused code runs much faster when compiled.
clean::
	rm -f mh-unit.elc

mh-unit.elc: mh-unit.el
	$(COMPILE_COMMAND) -f batch-byte-compile $?
