##
##  WML top-level Makefile
##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved. 
##
@SET_MAKE@

# ------------------------------------------------
#   DEFINITIONS
# ------------------------------------------------

SHELL           = /bin/sh

top_srcdir      = @top_srcdir@
srcdir          = @srcdir@
VPATH           = @srcdir@

SHTOOL	        = $(top_srcdir)/etc/shtool
INSTALL_PROGRAM = $(SHTOOL) install -c -m 755 -s
INSTALL_SCRIPT  = $(SHTOOL) install -c -m 755
INSTALL_DATA    = $(SHTOOL) install -c -m 644
MKDIR           = $(SHTOOL) mkdir -f -p -m 755
VERSION_TOOL    = $(SHTOOL) version
FIXPERM         = $(SHTOOL) fixperm
TARBALL         = $(SHTOOL) tarball -t

prefix          = @prefix@
exec_prefix     = $(prefix)
bindir          = $(prefix)/bin
libsubdir       = @libsubdir@
libdir          = $(prefix)/lib$(libsubdir)
mandir          = $(prefix)/man
catdir          = @catdir@
tmpdir          = @tmpdir@

RM              = rm -f

SUBDIRS = \
   wml_frontend     \
   wml_aux          \
   wml_backend      \
   wml_common       \
   wml_docs         \
   wml_misc         \
   wml_include      \
   wml_test

PROBLEMATIC_FILES = \
   wml_backend/p3_eperl/eperl_readme.c \
   wml_backend/p3_eperl/eperl_license.c \
   wml_backend/p3_eperl/eperl_logo.c \
   wml_backend/p3_eperl/eperl_powered.c \
   wml_backend/p9_slice/slice_term.pl

# ------------------------------------------------
#   THE DEFAULT TARGET
# ------------------------------------------------

#  We want to stop compilation when an error occur.
#  The trick is from Automake, to allow -k option keeps its natural
#  meaning.
all: @MAINT@.touch
	@set -e; \
	set dummy $(MAKEFLAGS); amf=$$2; \
	for dir in $(SUBDIRS); do \
	    echo "===> $$dir ($@)"; \
	    (cd $$dir && $(MAKE)) \
	    || case "$$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; \
	    echo "<=== $$dir"; \
	done && test -z "$$fail"
	@:> .build-ok

@MAINT@.touch:
@MAINT@	@for file in $(PROBLEMATIC_FILES); do \
@MAINT@	    touch $$file; \
@MAINT@	done
@MAINT@	@:> .touch

wml_contrib/wml.spec: config.status wml_contrib/wml.spec.in
	CONFIG_FILES=$@ CONFIG_HEADERS= $(SHELL) ./config.status

# ------------------------------------------------
#   TEST
# ------------------------------------------------

test:
	@[ -f .build-ok ] || $(MAKE) all
	@cd wml_test && $(MAKE) test

check: test

# ------------------------------------------------
#   INSTALLATION
# ------------------------------------------------

install: install-dirs install-core install-perl \
  install-doc install-aux install-misc install-tags

install-dirs:
	@[ -f .build-ok ] || $(MAKE) all
	@echo "___ INSTALL: prepare directory structure ___"
	$(MKDIR) $(bindir)
	$(MKDIR) $(libdir)
	$(MKDIR) $(libdir)/data
	$(MKDIR) $(libdir)/exec
	$(MKDIR) $(libdir)/include
	$(MKDIR) $(libdir)/perl
	$(MKDIR) $(mandir)
	$(MKDIR) $(mandir)/man1
	$(MKDIR) $(mandir)/man3
	$(MKDIR) $(mandir)/man7
	$(MKDIR) $(mandir)/$(catdir)1
	$(MKDIR) $(mandir)/$(catdir)7

install-core:
	@echo ""
	@echo "___ INSTALL: core language programs and manpages ___"
	$(INSTALL_SCRIPT) wml_frontend/wmk.pl $(bindir)/wmk
	$(INSTALL_DATA) wml_frontend/wmk.1 $(mandir)/man1/wmk.1
	$(INSTALL_SCRIPT) wml_frontend/wml.pl $(bindir)/wml
	$(INSTALL_DATA) wml_frontend/wml.1 $(mandir)/man1/wml.1
	$(INSTALL_SCRIPT) wml_frontend/wmd.pl $(bindir)/wmd
	$(INSTALL_DATA) wml_frontend/wmd.1 $(mandir)/man1/wmd.1
	$(INSTALL_DATA) wml_frontend/wmd.txt $(libdir)/data/wmd.txt
	if [ -f wml_frontend/wmd.map ]; then \
                $(INSTALL_DATA) wml_frontend/wmd.map $(libdir)/data/wmd.map; \
	else :; fi
	$(INSTALL_DATA) wml_frontend/wmd_missing.1 $(mandir)/man1/wmd_missing.1
	$(INSTALL_SCRIPT) wml_frontend/wmb.pl $(bindir)/wmb
	$(INSTALL_DATA) wml_frontend/wmb.1 $(mandir)/man1/wmb.1
	$(INSTALL_SCRIPT) wml_frontend/wmu.pl $(bindir)/wmu
	$(INSTALL_DATA) wml_frontend/wmu.1 $(mandir)/man1/wmu.1
	$(INSTALL_SCRIPT) wml_backend/p1_ipp/ipp.pl $(libdir)/exec/wml_p1_ipp
	$(INSTALL_DATA) wml_backend/p1_ipp/ipp.1 $(mandir)/man1/wml_p1_ipp.1
	$(INSTALL_PROGRAM) wml_backend/p2_mp4h/src/mp4h@EXEEXT@ $(libdir)/exec/wml_p2_mp4h@EXEEXT@
	$(INSTALL_DATA) wml_backend/p2_mp4h/doc/mp4h.1 $(mandir)/man1/wml_p2_mp4h.1
	$(INSTALL_PROGRAM) wml_backend/p3_eperl/eperl@EXEEXT@ $(libdir)/exec/wml_p3_eperl@EXEEXT@
	$(INSTALL_DATA) wml_backend/p3_eperl/eperl.1 $(mandir)/man1/wml_p3_eperl.1
	$(INSTALL_PROGRAM) wml_backend/p4_gm4/src/m4@EXEEXT@ $(libdir)/exec/wml_p4_gm4@EXEEXT@
	$(INSTALL_DATA) wml_backend/p4_gm4/doc/m4.txt $(mandir)/$(catdir)1/wml_p4_gm4.1
	$(INSTALL_SCRIPT) wml_backend/p5_divert/divert.pl $(libdir)/exec/wml_p5_divert
	$(INSTALL_DATA) wml_backend/p5_divert/divert.1 $(mandir)/man1/wml_p5_divert.1
	$(INSTALL_SCRIPT) wml_backend/p6_asubst/asubst.pl $(libdir)/exec/wml_p6_asubst
	$(INSTALL_DATA) wml_backend/p6_asubst/asubst.1 $(mandir)/man1/wml_p6_asubst.1
	$(INSTALL_SCRIPT) wml_backend/p7_htmlfix/htmlfix.pl $(libdir)/exec/wml_p7_htmlfix
	$(INSTALL_DATA) wml_backend/p7_htmlfix/htmlfix.1 $(mandir)/man1/wml_p7_htmlfix.1
	$(INSTALL_SCRIPT) wml_backend/p8_htmlstrip/htmlstrip.pl $(libdir)/exec/wml_p8_htmlstrip
	$(INSTALL_DATA) wml_backend/p8_htmlstrip/htmlstrip.1 $(mandir)/man1/wml_p8_htmlstrip.1
	$(INSTALL_SCRIPT) wml_backend/p9_slice/slice $(libdir)/exec/wml_p9_slice
	$(INSTALL_DATA) wml_backend/p9_slice/slice.1 $(mandir)/man1/wml_p9_slice.1

install-perl:
	@echo ""
	@echo "___ INSTALL: commonly used Perl 5 modules ___"
	-@set dummy $(MAKEFLAGS); amf=$$2; \
	(cd wml_common && $(MAKE) install prefix=$(prefix)) \
	    || case "$$amf" in *=*) exit 1;; *k*) ;; *) exit 1;; esac

install-doc:
	@echo ""
	@echo "___ INSTALL: general documentation ___"
	$(INSTALL_DATA) wml_docs/wml_intro.7 $(mandir)/man7/wml_intro.7
	$(INSTALL_DATA) wml_docs/wml_tutorial.7 $(mandir)/man7/wml_tutorial.7
	$(INSTALL_DATA) wml_docs/wml_macros.7 $(mandir)/man7/wml_macros.7
	$(INSTALL_DATA) wml_docs/wml_faq.7 $(mandir)/man7/wml_faq.7
	$(INSTALL_DATA) wml_docs/wml_tags.7 $(mandir)/man7/wml_tags.7
	$(INSTALL_DATA) wml_docs/wml_std_html20.txt $(mandir)/$(catdir)7/wml_std_html20.7
	$(INSTALL_DATA) wml_docs/wml_std_html32.txt $(mandir)/$(catdir)7/wml_std_html32.7
	$(INSTALL_DATA) wml_docs/wml_std_html40.txt $(mandir)/$(catdir)7/wml_std_html40.7
	$(INSTALL_DATA) wml_docs/wml_std_html401.txt $(mandir)/$(catdir)7/wml_std_html401.7
	$(INSTALL_DATA) wml_docs/wml_std_xhtml10.txt $(mandir)/$(catdir)7/wml_std_xhtml10.7
	$(INSTALL_DATA) wml_docs/wml_std_css1.txt $(mandir)/$(catdir)7/wml_std_css1.7
	$(INSTALL_DATA) wml_docs/wml_std_css2.txt $(mandir)/$(catdir)7/wml_std_css2.7
	$(INSTALL_DATA) wml_docs/wml_std_csspos.txt $(mandir)/$(catdir)7/wml_std_csspos.7
	$(INSTALL_DATA) wml_docs/wml_std_wai10.txt $(mandir)/$(catdir)7/wml_std_wai10.7
	$(INSTALL_DATA) wml_docs/wml_barebone.txt $(mandir)/$(catdir)7/wml_barebone.7

install-aux:
	@echo ""
	@echo "___ INSTALL: auxiliary programs and manpages ___"
	$(INSTALL_SCRIPT) wml_aux/htmlclean/htmlclean.pl $(libdir)/exec/wml_aux_htmlclean
	$(INSTALL_DATA) wml_aux/htmlclean/htmlclean.1 $(mandir)/man1/wml_aux_htmlclean.1
	$(INSTALL_PROGRAM) wml_aux/iselect/iselect@EXEEXT@ $(libdir)/exec/wml_aux_iselect@EXEEXT@
	$(INSTALL_DATA) wml_aux/iselect/iselect.1 $(mandir)/man1/wml_aux_iselect.1
	$(INSTALL_SCRIPT) wml_aux/map2html/map2html.pl $(libdir)/exec/wml_aux_map2html
	$(INSTALL_DATA) wml_aux/map2html/map2html.1 $(mandir)/man1/wml_aux_map2html.1
	$(INSTALL_SCRIPT) wml_aux/txt2html/txt2html.pl $(libdir)/exec/wml_aux_txt2html
	$(INSTALL_DATA) wml_aux/txt2html/txt2html.txt $(mandir)/$(catdir)1/wml_aux_txt2html.1
	$(INSTALL_DATA) wml_aux/txt2html/txt2html.dict $(libdir)/data/txt2html.dict
	$(INSTALL_PROGRAM) wml_aux/tidy/tidy@EXEEXT@ $(libdir)/exec/wml_aux_tidy@EXEEXT@
	$(INSTALL_DATA) wml_aux/tidy/tidy.txt $(mandir)/$(catdir)1/wml_aux_tidy.1
	$(INSTALL_SCRIPT) wml_aux/htmlinfo/htmlinfo.pl $(libdir)/exec/wml_aux_htmlinfo
	$(INSTALL_DATA) wml_aux/htmlinfo/htmlinfo.1 $(mandir)/man1/wml_aux_htmlinfo.1
	$(INSTALL_SCRIPT) wml_aux/linklint/linklint.pl $(libdir)/exec/wml_aux_linklint
	$(INSTALL_DATA) wml_aux/linklint/linklint.1 $(mandir)/man1/wml_aux_linklint.1
	$(INSTALL_SCRIPT) wml_aux/weblint/weblint.pl $(libdir)/exec/wml_aux_weblint
	$(INSTALL_DATA) wml_aux/weblint/weblint.1 $(mandir)/man1/wml_aux_weblint.1
	$(INSTALL_DATA) wml_aux/weblint/weblintrc $(libdir)/data/weblintrc
	$(INSTALL_SCRIPT) wml_aux/freetable/freetable.pl $(libdir)/exec/wml_aux_freetable
	$(INSTALL_DATA) wml_aux/freetable/freetable.1 $(mandir)/man1/wml_aux_freetable.1

install-misc:
	@echo ""
	@echo "___ INSTALL: miscellaneous files ___"
	-@set dummy $(MAKEFLAGS); amf=$$2; \
	(cd wml_misc && $(MAKE) install prefix=$(prefix)) \
	    || case "$$amf" in *=*) exit 1;; *k*) ;; *) exit 1;; esac

install-tags:
	@echo ""
	@echo "___ INSTALL: include files and manpages ___"
	-@set dummy $(MAKEFLAGS); amf=$$2; \
	(cd wml_include && $(MAKE) install prefix=$(prefix)) \
	    || case "$$amf" in *=*) exit 1;; *k*) ;; *) exit 1;; esac

# ------------------------------------------------
#   CLEANUP
# ------------------------------------------------

clean:
	-@for dir in $(SUBDIRS); do \
	    echo "===> $$dir ($@)"; \
	    (cd $$dir && $(MAKE) clean); \
	    echo "<=== $$dir"; \
	done
	-@$(RM) .build-ok 2>/dev/null

distclean: 
	-@for dir in $(SUBDIRS) config; do \
	    echo "===> $$dir ($@)"; \
	    (cd $$dir && $(MAKE) distclean); \
	    echo "<=== $$dir"; \
	done
	-@$(RM) .build-ok .touch Makefile 2>/dev/null

# ------------------------------------------------
#   THE CONFIGURATION SUPPORT
# ------------------------------------------------

SHTOOL_DIR_FILES = \
   .:etc/shtool \
   wml_aux/iselect:etc/shtool \
   wml_backend/p2_mp4h:shtool \
   wml_backend/p3_eperl:etc/shtool \
   wml_backend/p9_slice:etc/shtool

update-shtool:
	-@set -e; for f in $(SHTOOL_DIR_FILES); do \
	  dir=`echo $$f | sed -e 's,:.*$$,,'`; \
	  file=`echo $$f | sed -e 's,^.*:,,'`; \
	  rm -f $${dir}/$${file}; \
	  ( cd $${dir} && $(MAKE) $${file} ) || exit 1; \
	done

etc/shtool:
	@shtoolize -o $@ install mkdir version echo guessos fixperm tarball

fixperm:
	$(FIXPERM) *

EXCLUDE_FILES = CVS .cvsignore \
   autom4te.cache /\\.deps build-stamp \
   ^wml- ^wml_backend/p9_slice/lib ^wml_backend/p9_slice/t

manifest: distclean
	unset LC_ALL; \
	x=`echo $(EXCLUDE_FILES) |\
	   sed -e 's;/;\\\\/;g' -e 's#[ 	]\{1,\}#/d -e /#g'`; \
	$(FIND) * -type f -depth -print |\
	sed -e /$$x/d | sort >MANIFEST

summary:
	@$(top_srcdir)/etc/summary ChangeLog

# ------------------------------------------------
#   THE RELEASE STUFF
# ------------------------------------------------

TAR    = tar       # where to find GNU Tar
FIND   = find      # where to find a good Find tool
GZIP   = gzip      # where to find GNU Zip
NAME   = barbier   # name of maintainer who rolls the tarball

_GETDISTINFO = \
    _version=`$(VERSION_TOOL) -l txt -d short VERSION`; \
    _date=`date '+%Y%m%d_%H%M'`;

_BUILDDIST = \
    _cwd=`pwd`; \
    echo "Creating tarball..."; \
    cd $(top_srcdir) && \
    cat MANIFEST | xargs $(TARBALL) -u $(NAME) -g wml -c '$(GZIP) --best' \
        -o $${_tarball}; \
    echo "Done"; \
    cd $$_cwd && ls -l $${_tarball}

_NEWVERS = \
    $(VERSION_TOOL) -l txt -n WML $$OPT VERSION; \
    V=`$(VERSION_TOOL) -l txt -d long VERSION`;\
    sed -e "s/Version .*(.*)/Version $$V/g" <README >README.n && mv README.n README; \
    sed -e "s/Version .*(.*)/Version $$V/g" <wml_frontend/wmd.txt >wml_frontend/wmd.txt.n && mv wml_frontend/wmd.txt.n wml_frontend/wmd.txt

_UPDATEVERS = \
    V=`$(VERSION_TOOL) -l txt -d short VERSION`;\
    $(VERSION_TOOL) -l txt -n WML -s $$V VERSION; \
    V=`$(VERSION_TOOL) -l txt -d long VERSION`;\
    sed -e "s/Version .*(.*)/Version $$V/g" <README >README.n && mv README.n README; \
    sed -e "s/Version .*(.*)/Version $$V/g" <wml_frontend/wmd.txt >wml_frontend/wmd.txt.n && mv wml_frontend/wmd.txt.n wml_frontend/wmd.txt

release: wml_contrib/wml.spec distclean manifest fixperm
	@$(_GETDISTINFO) \
    _distname="wml-$${_version}"; \
    _tarball="$(tmpdir)/$${_distname}.tar.gz"; \
    echo "Release Distribution: WML Version $$_version"; \
    $(_BUILDDIST); \
    mv $${_tarball} $${_distname}.tar.gz

snap: distclean manifest fixperm
	@$(_GETDISTINFO) \
    _distname="wml-$${_version}-SNAP"; \
    _tarball="$(tmpdir)/$${_distname}.tar.gz"; \
    echo "Snap of whole source tree: WML Version $$_version as of $$_date"; \
    $(_BUILDDIST); \
    mv $${_tarball} $${_distname}.tar.gz

new-version:
	@V="$(VERSION)"; \
	if [ ".$$V" != . ]; then \
		OPT="-s$$V"; \
	else \
		OPT="-e"; \
	fi; \
	$(_NEWVERS)

update-version:
	$(_UPDATEVERS)

##EOF##
