NEEDS_MAKEFILE	= cfortran mclibs phtools
LIB_PACKAGES	= mclibs phtools
TEST_PACKAGES   = $(LIB_PACKAGES)

prefix		= /usr/local
datadir		= $(prefix)/share
include $(datadir)/cernlib/cernlib.mk

MCDOC		= $(DESTDIR)$(datadir)/doc/montecarlo-doc

cernlib-arch-local: cernlib-mc-docs
cernlib-indep-local: cernlib-mc-data

cernlib-configure-local:
ifneq (,$(findstring nospdf,$(DEB_BUILD_OPTIONS)))
	cp -p $(PATCHDIR)/optional/901-dont-compile-spdf.dpatch $(PATCHDIR)
	echo 901-dont-compile-spdf.dpatch >> $(PATCHDIR)/00list
endif
#ifeq ($(DEB_BUILD_ARCH),m68k)
#	cp -p $(PATCHDIR)/optional/902-no-isajet-on-m68k.dpatch $(PATCHDIR)
#	echo 902-no-isajet-on-m68k.dpatch >> $(PATCHDIR)/00list
#endif

cernlib-mc-data: patch buildtree
	$(MAKE) -C $(CERN_BUILDDIR) MAKEOVERRIDES= mclibs/Makefile
	$(MAKE) -C $(CERN_BUILDDIR)/mclibs MAKEOVERRIDES= \
		cojets/Makefile eurodec/Makefile
	$(MAKE) -C $(CERN_BUILDDIR)/mclibs/cojets MAKEOVERRIDES= \
		$(CERN_LIBDIR)/cojets.dat
	$(MAKE) -C $(CERN_BUILDDIR)/mclibs/eurodec MAKEOVERRIDES= \
		$(CERN_LIBDIR)/eurodec.dat
	$(MAKE) -C $(CERN_BUILDDIR)/mclibs MAKEOVERRIDES= isajet/Makefile
	$(MAKE) -C $(CERN_BUILDDIR)/mclibs/isajet MAKEOVERRIDES= \
	    	$(CERN_LIBDIR)/isajet.dat

# now build some documentation that isn't created automatically.
# Unfortunately, the cernlib authors weren't thoughtful enough to
# give any indication of the order in which chapters go from their
# filenames.
cernlib-mc-docs: patch
	mkdir -p $(CERN_DOCDIR)
	#
	# cojets
	set -e ; \
	cd $(CVSCOSRC)/mclibs/cojets/doc ; \
	cat title.doc intro.doc physics.doc usage.doc backward.doc patchy.doc \
		external.doc iocon.doc ioconfor.doc ioconinp.doc ioconout.doc \
		ioisa.doc ioisafor.doc ioisainp.doc ioisaout.doc taperead.doc \
		table.doc | sed -e '/^\*$$/d' -e '/^\* .*$$/d' > \
		$(CERN_DOCDIR)/cojets.txt
	# isajet
	set -e ; \
	cd $(CVSCOSRC)/mclibs/isajet/doc ; \
	sed '/^\*.*$$/d' ztext.doc > $(CERN_DOCDIR)/isajet-zebra.txt ; \
	sed '/^\*.*$$/d' isassdoc.doc > $(CERN_DOCDIR)/isasusy.txt ; \
	cat intro.doc physics.doc sample.doc patchy.doc main.doc \
		input.doc output.doc tape.doc decay.doc ident.doc \
		higher.doc susy.doc changes.doc > $(CERN_DOCDIR)/isajet.tex; \
	cd $(CERN_DOCDIR) ; \
	latex isajet.tex ; \
	latex isajet.tex ; \
	latex isajet.tex ; \
	dvips isajet.dvi -o isajet.ps
	# eurodec
	set -e ; \
	cd $(CVSCOSRC)/mclibs/eurodec/doc ; \
	sed -e '/^\*.*$$/d' -e 's/C\.//g' eudtxt > $(CERN_DOCDIR)/eurodec.txt

# target to install include files for development packages
install-includes:
	install -d -m 0755 $(DESTDIR)$(includedir)

	# install them all (patching includes, e.g. #include "ksys.h"
	# -> #include <kuip/ksys.h> )
	for dir in `cat $(ADDONDIR)/includelist.txt` ; do \
		basedir=`basename $$dir` ; \
		rm -rf $(DESTDIR)$(includedir)/$$basedir ; \
		cp -Rp $(CVSCOSRC)/$$dir $(DESTDIR)$(includedir) ; \
		ls -1 $(DESTDIR)$(includedir)/$$basedir/* | \
			egrep -v '\.(h|inc)$$' | xargs rm -rf ; \
		for file in `ls -1 $(DESTDIR)$(includedir)/$$basedir/*` ; do \
		if [ -f $$file ] ; then \
	sed -e 's,^\([ ]*#[ ]*include\)[ 	]*"\([^"/]*\)",\1 <'$$basedir/'\2>,g' \
		-e 's,^\([ ]*#[ ]*include\)[ 	]*"\([^"]*\)",\1 <\2>,g' \
		-e 's,cfortran/,,g' \
		$$file > $$file.tmp ; \
	mv -f $$file.tmp $$file ; \
	fi ; \
	done ; \
	done

	# install some symlinks, e.g. herwig$$version -> herwig
	rm -f $(DESTDIR)$(includedir)/herwig && \
		ln -sf herwig59 $(DESTDIR)$(includedir)/herwig
	rm -rf $(DESTDIR)$(includedir)/isajet758
	mv $(DESTDIR)$(includedir)/isajet $(DESTDIR)$(includedir)/isajet758
	ln -sf isajet758 $(DESTDIR)$(includedir)/isajet
	rm -rf $(DESTDIR)$(includedir)/pdf804
	mv $(DESTDIR)$(includedir)/pdf $(DESTDIR)$(includedir)/pdf804
	ln -sf pdf804 $(DESTDIR)$(includedir)/pdf
	
	# fix location of eurodec data file in header file
	sed 's,eurodec.dat,$(datadir)/montecarlo-data/eurodec.dat,' \
		$(DESTDIR)$(includedir)/eurodec/eufiles.inc \
		> $(DESTDIR)$(includedir)/eurodec/eufiles.inc.tmp && \
	mv -f $(DESTDIR)$(includedir)/eurodec/eufiles.inc.tmp \
		$(DESTDIR)$(includedir)/eurodec/eufiles.inc

# target to install files in arch-dependent packages
install-arch: install-includes
	# install libraries
	install -d -m 0755 $(DESTDIR)$(libdir)
	install -p -m 0644 $(CERN_LIBDIR)/*.a $(DESTDIR)$(libdir)/
	# we go through the following rigmarole because "install" dereferences
	# symlinks and older GNU versions of "cp" have inconsistent
	# syntax for the short form of the --no-dereference flag
	set -e ; \
	if [ -d $(CERN_SHLIBDIR) ] ; then \
	( abslibdir="`cd $(DESTDIR)$(libdir)/ && pwd`" ; \
	  cd $(CERN_SHLIBDIR)/ ; \
	  for file in `ls | grep 'lib.*\..*\..*\.'` ; do \
		basename=`echo $$file | cut -f 1 -d .` ; \
		install -p -m 0644 $$file "$$abslibdir/" ; \
		for link in `ls $${basename}.* |grep -v 'lib.*\..*\..*\.'`; do \
			ln -sf $$file "$$abslibdir"/$$link ; \
		done ; \
	  done ) ; \
	fi

	# ... and mclibs documentation
	for dir in cojets eurodec herwig isajet pdflib ; do \
		install -d -m 0755 $(MCDOC)/$$dir ; \
	done
	install -p -m 0644 $(CERN_DOCDIR)/cojets.txt $(MCDOC)/cojets/
	install -p -m 0644 $(CERN_DOCDIR)/isa* $(MCDOC)/isajet/
	install -p -m 0644 $(CERN_DOCDIR)/eurodec.txt $(MCDOC)/eurodec/
	install -p -m 0644 $(CVSCOSRC)/mclibs/herwig/doc/herwig59 \
		$(MCDOC)/herwig/herwig59.txt
	install -p -m 0644 $(CVSCOSRC)/mclibs/pdf/dpdf/pdfdoc.doc \
		$(MCDOC)/pdflib/pdfdoc.txt

# target to install files in arch-independent packages
install-indep:
	# install some include files for montecarlo-base package
	install -d -m 0755 $(DESTDIR)$(includedir)
	install -p -m 0644 $(CVSCOSRC)/cfortran/jetset74.h \
		$(CVSCOSRC)/cfortran/lepto62.h $(DESTDIR)$(includedir)/
	
	# install Monte Carlo data files
	install -d -m 0755 $(DESTDIR)$(datadir)/montecarlo-data
	install -p -m 0644 $(CERN_LIBDIR)/cojets.dat \
		$(DESTDIR)$(datadir)/montecarlo-data/
	install -p -m 0644 $(CERN_LIBDIR)/eurodec.dat \
		$(DESTDIR)$(datadir)/montecarlo-data/
	install -p -m 0644 $(CERN_LIBDIR)/isajet.dat \
		$(DESTDIR)$(datadir)/montecarlo-data/

.PHONY: cernlib-configure-local cernlib-arch-local  \
	cernlib-mc-data cernlib-mc-docs \
	install install-arch install-includes install-indep

