# Be sure that you
# 1. Have a subdirectory `hostname`.
# 2. Set the installation number in `hostname` properly.
# 3. Change local-defs in `hostname` only.

# Directories, where "Makefile"s are to be generated from "IMakefile"s.
# These directories are specified relative to the setup directory!
# The documentation directory is handled a little bit differently.
PREPARE_DIRS = \
	../lib\
	../setup-lib ../sup ../otherc ../h ../pltotf\
	../dvitps ../dvitps/src ../dvitps/h ../dvitps/psr ../dvitps/capab\
	../pltotf\
	../pfd2tfm ../pfd2tfm/src ../pfd2tfm/h ../pfd2tfm/evs\
	../printpdr\
	../pfd ../pfd/regular ../pfd/regular-II ../pfd/extra\
	../doc ../doc/SliTeX ../psfig ../psfig/ps-ex ../man ../afm

# Directories in which to execute "make all, make print, etc.".
DIRS = \
	../lib\
	../setup-lib ../sup ../otherc ../h ../pltotf\
	../dvitps\
	../printpdr\
	../pfd2tfm\
	../pfd\
	../man ../doc ../psfig ../afm

# Directories in which to execute "make install".
INSTALL_DIRS = $(DIRS)

# Directories in which to execute "make clean".
CLEAN_DIRS =   $(DIRS)

CPP = /lib/cpp

prepare:
# Write file "installation_num" which contains the correct installation number.
	cd `hostname`; make prepare
#	Remove certain files to force recompilation and relinking.
	rm -f   ../otherc/*.o ../pfd2tfm/src/pfd2tfm\
		../pfd2tfm/src/printpdr\
		../dvitps/src/special.o ../dvitps/src/search2.o ../dvitps/src/dvitps\
		../printpdr/printpdr\
		../man/docman.tex ../man/*.l ../psfig/xwindows.tex
	@make prepare-setup
	cd ../pfd/regular; make prepare
	cd ../pfd/regular-II; make prepare

# local-defs.tmp contains the stuff extracted from local-defs and furthermore
# some definitions used by the C preprocessor.
local-defs.tmp:	../setup-lib/setup-ld.awk local-defs makefile
	@rm -f $@ local-defs.log
	awk -f ../setup-lib/setup-ld.awk "hostname=`hostname`" \
		"InstallationNumber=`cat installation_num`" local-defs > $@

prepare-setup:	local-defs.tmp ../setup-lib/setup-1.awk ../setup-lib/setup-2.awk ../setup-lib/closing.defs makefile
	-for dir in $(PREPARE_DIRS) ; do\
		echo "TeXPS: compute Makefile from IMakefile, Directory: $$dir";\
		if [ ! -r $$dir/IMakefile ] ; then co -l $$dir/IMakefile; fi; \
		echo '' > $$dir/Makefile;\
		echo "#DO NOT CHANGE THIS MAKEFILE, generated from IMakefile at `date`"\
				>> $$dir/Makefile;\
		echo "#Installation number used here: `cat installation_num`"\
				>> $$dir/Makefile;\
		echo '' >> $$dir/Makefile;\
		awk -f ../setup-lib/setup-1.awk local-defs.tmp $$dir/IMakefile |\
			$(CPP) -P -C |\
			awk -f ../setup-lib/setup-2.awk >> $$dir/Makefile;\
		cat ../setup-lib/closing.defs >> $$dir/Makefile;\
	done

all print depend lint relink:
	echo "Make \"$@\" in `pwd`"
	for dir in $(DIRS); do\
		cd $$dir;\
		echo "Make \"$@\" in `pwd`";\
			make $@;\
		cd ../setup;\
	done;

doc:
	cd ../doc; make $@

install:
	echo "Make \"$@\" in `pwd`"
	for dir in $(INSTALL_DIRS); do\
		cd $$dir;\
		echo "Make \"$@\" in `pwd`";\
			make $@;\
		cd ../setup;\
	done;

clean::
	echo "Make \"$@\" in `pwd`"
	for dir in $(CLEAN_DIRS); do\
		cd $$dir;\
		echo "Make \"$@\" in `pwd`";\
			make $@;\
		cd ../setup;\
	done;
	make clean-x
clean-x::
	@echo make clean in `pwd`
	rm -f ,* \#* *.BAK *.bak *.CKP *~ *.o *.tmp ERR* a.out core
	rm -f .emacs_* lint ,* .,* *.dvi *.aux *.log *.toc *.lof *.lot
	rm -f dvitype.out texspell.out .*~ TAGS local-defs.tmp
	rm -f installation_num local-defs
