##--------------------------------------------------------------------------##
##  File:
##	$Id: Makefile,v 1.27 2002/09/05 04:12:43 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

READ_MAIL	= ./bin/read-mail
WEB_ARCHIVE	= ./bin/web-archive
MK_PROCMAILRC	= ./bin/mk-procmailrc
APPLY_CONFIG	= ./bin/apply-config
MHA_CHECK	= ./bin/mhonarc-check
COMPRESS_FILES	= ./bin/compress-files
COMPRESS_MBOXES	= ./bin/compress-mboxes

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

NO_ARCHIVE	= .noarchive
PROCMAIL_RC	= .procmailrc

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

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

default: procmailrc

procmailrc: _FORCE
	$(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: mhonarc-check
	-@$(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
	-@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:
	-@echo "Target 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 "WARNING: 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	= \
		  ./bin \
		  ./cgi-bin \
		  ./etc \
		  ./html/.PNM* \
		  ./html/stylesheet.css* \
		  ./lib \
		  ./Makefile \
		  ./TODO \
		  ./README \
		  ./NEWS \
		  ./COPYING \
		  # 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 = \
	    $(WEB_ARCHIVE) \
	    $(MK_PROCMAILRC) \
	    $(APPLY_CONFIG) \
	    $(MHA_CHECK) \
	    $(COMPRESS_FILES) \
	    $(COMPRESS_MBOXES) \
	    # End PERL_PRGS

release: syntax_check dist

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

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

distclean: clean
	$(APPLY_CONFIG) -distclean -verbose
	$(RM) -f $(CONFIG_SH) $(LIST_DEF) VERSION
	$(RM) -rf dist

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