##--------------------------------------------------------------------------##
##  File:
##	$Id: Makefile,v 1.35 2002/10/01 22:49:46 ehood Exp $
##  Description:
##	Administrative makefile for mailing list archives.
##--------------------------------------------------------------------------##
##  Copyright (C) 2001-2002	Earl Hood <earl@earlhood.com>
##
##  This program 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 of the License, or
##  (at your option) any later version.
##  
##  This program 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 this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
##  02111-1307, USA
##--------------------------------------------------------------------------##

.PHONY: default editidx rebuild rootidx dist enable disable readmail \
	help editidxonly release clean distclean configure mhonarc-check \
	doc doc_pod doc_file doc_install

INSTALL_PL	= ./install.pl
APPLY_CONFIG	= ./bin/apply-config
COMPRESS_FILES	= ./bin/compress-files
COMPRESS_MBOXES	= ./bin/compress-mboxes
CONFIG_CHECK	= ./bin/config-check
FILTER_SPOOL	= ./bin/filter-spool
LOG_CMD         = ./bin/logcmd
MBOX_MONTH_PACK = ./bin/mbox-month-pack
MHA_CHECK	= ./bin/mhonarc-check
MH_MONTH_PACK   = ./bin/mh-month-pack
MK_PROCMAILRC	= ./bin/mk-procmailrc
READ_MAIL	= ./bin/read-mail
WEB_ARCHIVE	= ./bin/web-archive

PRGS		= \
		  $(INSTALL_PL) \
		  $(LOG_CMD) \
		  $(MH_MONTH_PACK) \
		  $(MBOX_MONTH_PACK) \
		  $(FILTER_SPOOL) \
		  $(APPLY_CONFIG) \
		  $(COMPRESS_FILES) \
		  $(COMPRESS_MBOXES) \
		  $(CONFIG_CHECK) \
		  $(MHA_CHECK) \
		  $(MK_PROCMAILRC) \
		  $(READ_MAIL) \
		  $(WEB_ARCHIVE) \
		  # End PRGS

CONFIG_SH	= ./lib/config.sh
LIST_DEF	= ./lib/lists.def

NO_ARCHIVE	= .noarchive
PROCMAIL_RC	= procmailrc.mharc

CHMOD		= chmod
CP		= cp
FIND		= find
GZIP		= gzip
MKDIR		= mkdir
MV		= mv
PERL		= perl
RM		= rm
TAR		= tar
TOUCH		= touch

##--------------------------------------------------------------------------##

default: procmailrc

procmailrc: mhonarc-check
	$(MK_PROCMAILRC)

editidx: disable
	-$(PERL) $(WEB_ARCHIVE) -verbose -editidx -nosearch $(MLISTS)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is DISABLED !!!"
	-@echo "============================================================="

editidxonly: disable
	-$(PERL) $(WEB_ARCHIVE) -verbose -editidxonly -nosearch $(MLISTS)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is DISABLED !!!"
	-@echo "============================================================="

editrootidx: disable
	-$(PERL) $(WEB_ARCHIVE) -verbose -editrootidx -nosearch $(MLISTS)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is DISABLED !!!"
	-@echo "============================================================="

rebuild: disable
	-$(PERL) $(WEB_ARCHIVE) -verbose -rebuild $(MLISTS)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is DISABLED !!!"
	-@echo "============================================================="

rootidx: _FORCE
	-$(PERL) $(WEB_ARCHIVE) -verbose -editrootidx $(MLISTS)

enable: _FORCE
	@$(RM) -f $(NO_ARCHIVE)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is ENABLED !!!"
	-@echo "============================================================="

disable: _FORCE
	@$(TOUCH) $(NO_ARCHIVE)
	-@echo "============================================================="
	-@echo "!!! Auto-archive processing is DISABLED !!!"
	-@echo "============================================================="

readmail: _FORCE
	$(READ_MAIL) -verbose -force

configure: _FORCE
	-@$(MKDIR) -p log mbox html
	@if [ ! -f $(CONFIG_SH) ]; then \
	  $(CP) $(CONFIG_SH).dist $(CONFIG_SH); \
	  $(CHMOD) u+w $(CONFIG_SH); \
	fi
	@if [ ! -f $(LIST_DEF) ]; then \
	  $(CP) $(LIST_DEF).dist $(LIST_DEF); \
	  $(CHMOD) u+w $(LIST_DEF); \
	fi
	$(APPLY_CONFIG) -verbose
	-$(MHA_CHECK)
	-@echo "============================================================="
	-@echo "* Make sure to rerun 'make configure' when you change"
	-@echo "* lib/config.sh or change a .in template file."
	-@echo "============================================================="

mhonarc-check: _FORCE
	@$(MHA_CHECK)

help: _FORCE
	-@echo "Targets available:"
	-@echo "  (default)     Generate $(PROCMAIL_RC) from $(LIST_DEF)."
	-@echo "  configure:    Apply $(CONFIG_SH) settings."
	-@echo "  disable:      Disable automated processing of new messages."
	-@echo "  editidx:      Edit all mhonarc archive pages."
	-@echo "  editidxonly:  Edit only mhonarc archive index pages."
	-@echo "  editrootidx:  Edit only top period index pages."
	-@echo "  enable:       Enable automated processing of new messages."
	-@echo "  help:         This message."
	-@echo "  readmail:     Process mail spool."
	-@echo "  rebuild:      Rebuild archives from raw message data."
	-@echo "  rootidx:      Regenerated top index for archives."
	-@echo ""
	-@echo "NOTE: Targets that modify archives run with debugging enabled."
	-@echo "NOTE: 'editidx', 'editidxonly', and 'rebuild' automatically"
	-@echo "      disable auto-processing.  Execute 'enable' target to"
	-@echo "      reenable."
	-@echo "CAUTION: The 'clean' target should not have to be invoked."
	-@echo "         But if done, it removes all .in generated files.  The"
	-@echo "         'configure' target can be executed to regenerate"
	-@echo "         file from templates"
	-@echo "WARNING: The 'dist' and 'distclean' targets should only be"
	-@echo "         invoked IF YOU KNOW WHAT YOU ARE DOING!"


# Dummy target to force building of other targets
_FORCE:

##--------------------------------------------------------------------------##
##  The following targets are mainly intended by mharc developers
##  and used for creating releases
##--------------------------------------------------------------------------##

DIST_BUNDLE	= mharc-$(_RELEASE_VERSION).tar

# List of files/dirs to be included in distribtion bundle
# NOTE: The leading '.' is important.
DIST_FILES	= \
		  ./README \
		  ./INSTALL \
		  ./NEWS \
		  ./COPYING \
		  ./TODO \
		  ./install.pl \
		  ./doc \
		  ./bin \
		  ./cgi-bin \
		  ./etc \
		  ./html/.PNM* \
		  ./html/stylesheet.css* \
		  ./lib \
		  ./Makefile \
		  # End DIST_FILES

# List of files/dirs to explicitly exclude from distribtion bundle
# NOTE: The leading '.' is important.
DIST_EXC_FILES	= \
		  ./etc/release-mharc \
		  # End DIST_EXC_FILES

TAR_EXC_FILE	= dist/.dist-exclude

PERL_PRGS = $(PRGS)

release: syntax_check dist

dist: _FORCE
	@$(RM) -rf dist
	@$(MKDIR) -p dist/mharc
	@$(FIND) . -name 'CVS' -print > $(TAR_EXC_FILE)
	@$(FIND) . -name '.*.sw*' -print >> $(TAR_EXC_FILE)
	@for i in $(DIST_EXC_FILES); do \
	  echo $$i >> $(TAR_EXC_FILE); \
	 done
	@$(TOUCH) INSTALL
	@$(TAR) -c \
	        -X $(TAR_EXC_FILE) \
	        -f - $(DIST_FILES) | (cd dist/mharc; tar xfp -)
	@(cd dist/mharc && $(MAKE) distclean)
	@(cd dist/mharc && $(MAKE) doc)
	@(cd dist/mharc && $(MAKE) doc_pod)
	@(cd dist/mharc && echo "$(_RELEASE_VERSION)" > VERSION)
	@$(MV) dist/mharc dist/mharc-$(_RELEASE_VERSION)
	@(cd dist && $(TAR) cvf $(DIST_BUNDLE) mharc-$(_RELEASE_VERSION))
	@(cd dist && $(GZIP) $(DIST_BUNDLE))

syntax_check: _FORCE
	@for i in $(PERL_PRGS); do \
	  echo "Checking $$i syntax..."; \
	  $(PERL) -cw $$i; \
	 done

# doc_pod listed twice to resolve xrefs
doc: doc_pod doc_file doc_install

doc_pod: _FORCE
	-@mkdir -p ./doc/bin ./doc/lib ./doc/file
	@for i in $(PRGS); do \
	  echo "processing $$i..." ; \
	  pod2html --header --infile=$$i \
		   --htmlroot=.. \
		   --podroot=./doc \
		   --podpath='bin:lib' \
		   --outfile=./doc/bin/`basename $$i`.html ; \
	done
	@for i in `find ./lib/MHArc -name '*.pm' -print`; do \
	  echo "processing $$i..." ; \
	  pod2html --header \
		   --infile=$$i \
		   --htmlroot=.. \
		   --podroot=./doc \
		   --podpath='bin:lib' \
		   --outfile=./doc/lib/MHArc::`basename $$i`.html ; \
	done
	@$(RM) -f pod2htm*.x~~

doc_file: _FORCE
	$(CP) -f Makefile ./doc/file/Makefile.txt
	$(CP) -f ./lib/common.mrc.in.dist ./doc/file/common.mrc.txt
	$(CP) -f ./lib/config.sh.dist ./doc/file/config.sh.txt
	$(CP) -f ./lib/lists.def.dist ./doc/file/lists.def.txt
	$(CP) -f ./etc/apache.conf.in.dist ./doc/file/apache.conf.txt
	$(CP) -f ./etc/crontab.in.dist ./doc/file/crontab.txt
	$(CP) -f ./etc/.htaccess.in.dist ./doc/file/htaccess.txt

doc_install:
	w3m -dump -cols 76 ./doc/install.html > INSTALL

distclean: clean
	$(APPLY_CONFIG) -distclean -verbose
	$(RM) -f $(CONFIG_SH) $(CONFIG_SH).cache.pl $(LIST_DEF) VERSION INSTALL
	$(RM) -rf ./dist ./doc/bin ./doc/lib ./doc/file

clean: _FORCE
	$(RM) -f $(PROCMAIL_RC)
	$(APPLY_CONFIG) -clean -verbose

