SHELL=/bin/sh
CONF =	Config
TARGETS	= xntpd/xntpd xntpdc/xntpdc ntpq/ntpq ntpdate/ntpdate \
	  ntptrace/ntptrace xntpres/xntpres authstuff/authspeed util/tickadj
MAKE=	make

all:	$(TARGETS)

makeconfig:
	./scripts/makeconfig.sh $(OS)

xntpd/xntpd:	lib/libntp.a
	cd xntpd ; $(MAKE) $(MFLAGS) xntpd

xntpdc/xntpdc:	lib/libntp.a
	cd xntpdc ; $(MAKE) $(MFLAGS) xntpdc

ntpq/ntpq:	lib/libntp.a
	cd ntpq ; $(MAKE) $(MFLAGS) ntpq

ntptrace/ntptrace:	lib/libntp.a
	cd ntptrace ; $(MAKE) $(MFLAGS) ntptrace

ntpdate/ntpdate:	lib/libntp.a
	cd ntpdate ; $(MAKE) $(MFLAGS) ntpdate

authstuff/authspeed:	lib/libntp.a
	cd authstuff ; $(MAKE) $(MFLAGS) all

xntpres/xntpres:	lib/libntp.a
	cd xntpres ; $(MAKE) $(MFLAGS) xntpres

util/tickadj:
	cd util ; $(MAKE) $(MFLAGS) tickadj

lib/libntp.a:	lib/*.c
	cd lib ; $(MAKE) $(MFLAGS)

savebin:	$(TARGETS)
	@test -d bin || mkdir bin
	mv $(TARGETS) bin

neat:
	rm -f $(CONF).sed eddep makedep Makefile.bak make.log make.out

distclean clean:	neat
	rm -f *.orig *.rej
	rm -f include/*.orig lib/*.rej
	rm -f scripts/*.orig scripts/*.rej
	rm -f machines/*.orig machines/*.rej
	rm -f compilers/*.orig compilers/*.rej
	cd adjtime ; $(MAKE) $(MFLAGS) $@
	cd authstuff ; $(MAKE) $(MFLAGS) $@
	cd clockstuff ; $(MAKE) $(MFLAGS) $@
	cd gizmo ; $(MAKE) $(MFLAGS) $@
	cd lib ; $(MAKE) $(MFLAGS) $@
	cd ntpdate ; $(MAKE) $(MFLAGS) $@
	cd ntpq ; $(MAKE) $(MFLAGS) $@
	cd ntptrace ; $(MAKE) $(MFLAGS) $@
	cd util ; $(MAKE) $(MFLAGS) $@
	cd xntpd ; $(MAKE) $(MFLAGS) $@
	cd xntpdc ; $(MAKE) $(MFLAGS) $@
	cd xntpres ; $(MAKE) $(MFLAGS) $@

install:	all
	cd xntpd ; $(MAKE) $(MFLAGS) install
	cd xntpdc ; $(MAKE) $(MFLAGS) install
	cd ntpq ; $(MAKE) $(MFLAGS) install
	cd ntptrace ; $(MAKE) $(MFLAGS) install
	cd ntpdate ; $(MAKE) $(MFLAGS) install
	cd xntpres ; $(MAKE) $(MFLAGS) install
	cd util ; $(MAKE) $(MFLAGS) install

$(CONF).sed:	$(CONF) Makefile
	sed -n -e 's:^\([^ 	]*\)=[ 	]*\(.*\):s~^\1=.*~&~:p' < $(CONF) > $@

depend:
	find . -name Makefile.tmpl -print > eddep
	sed -e 's:^\./::' -e 's:\.tmpl$$::' -e '/^Makefile/d' < eddep | \
		awk '/./	{ for (i = 0; i < NF; ++i) \
					if (length(line) == 0) \
						line = $$i ; \
					else { \
						if (length(line " " $$i) < 60) \
							line = line " " $$i ; \
						else { \
							print line ; \
							line = $$i ; \
						} ; \
					} \
				} \
			END	{ if (length(line) > 0) print line }' | \
	    sed -e 's/^/makefiles: /' > makedep
	echo >> makedep
	sed -e 's:^\./::' -e '/^Makefile/d' \
	    -e 's/^\(.*\)\.tmpl$$/\1:	\1.tmpl $${CONF}.sed/' -e x \
	    -e 's/.*/	sed -f $${CONF}.sed < $$@.tmpl > $$@/' \
	    -e x -e G < eddep >> makedep
	echo '/^# DO NOT DELETE THIS LINE/+1,$$d' >eddep
	echo '$$r makedep' >>eddep
	echo 'w' >>eddep
	cp Makefile Makefile.bak
	/bin/ed - Makefile < eddep
	rm eddep makedep

undepend:
	echo '/DO NOT DELETE THIS LINE/,$$d' > ${CONF}.sed
	$(MAKE) $(MFLAGS) makefiles
	rm -f ${CONF}.sed

makefiles: makeconfig

# DO NOT DELETE THIS LINE -- It is used by 'make depend' to update this file
makefiles: adjtime/Makefile authstuff/Makefile clockstuff/Makefile
makefiles: gizmo/Makefile lib/Makefile ntpdate/Makefile ntpq/Makefile
makefiles: util/Makefile xntpd/Makefile xntpdc/Makefile
makefiles: xntpres/Makefile ntptrace/Makefile

adjtime/Makefile:	adjtime/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
authstuff/Makefile:	authstuff/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
clockstuff/Makefile:	clockstuff/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
gizmo/Makefile:	gizmo/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
lib/Makefile:	lib/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
ntpdate/Makefile:	ntpdate/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
ntpq/Makefile:	ntpq/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
util/Makefile:	util/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
xntpd/Makefile:	xntpd/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
xntpdc/Makefile:	xntpdc/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
xntpres/Makefile:	xntpres/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
ntptrace/Makefile:	ntptrace/Makefile.tmpl ${CONF}.sed
	sed -f ${CONF}.sed < $@.tmpl > $@
