# SRC is the location of the xntp3 sources -- may wee be a symbolic link.
# CMDS is a directory containing various shell commands.
SRC	= src
CMDS	= shell
VERSION	= 0.4

# TEST files -- a sample file to test to see if something has been done.
TEST_SRC = $(SRC)/COPYRIGHT
TEST_FIX = $(SRC)/.fixed_srcs
TEST_CONF = $(SRC)/Config
TEST_MKF = $(SRC)/xntpres/Makefile
TEST_OBJ = $(SRC)/util/tickadj
RCONF_DIST= ntp.rconf.dist
RCONF_LOCL= ntp.rconf
CONF_LOCL= ntp.conf
KEYS_LOCL= ntp.keys
ICONF	= /etc/ntp.conf
IKEYS	= /etc/ntp.keys
NTPDATE	= $(SRC)/ntpdate/ntpdate
NTPDATEOPT =
XNTPD	= $(SRC)/xntpd/xntpd
IPSERV	= 128.232.0.6
NSERV	= ntp-test.ja.net
SMKF	= $(SRC)/ntpdate/Makefile
AUTHC	= $(SRC)/authstuff/authcert
TICKADJ	= $(SRC)/util/tickadj
AUTHD	= $(SRC)/authstuff/certdata
TARS	= README.FIRST README.quick README.steps README.slow README.files \
	  README.S3 README.S4 \
	  MANIFEST Makefile \
	  shell/fix_src shell/how_to_get_src shell/setupconfig shell/checkroot \
	  shell/makeconf shell/makekeys shell/checkservices shell/checkxntpdc \
	  shell/explainrc \
	  au.authdes.c ntp.rconf.dist form
#	  patches/loopfilter-neg
TARE	= src

# "all" is the complete set of steps .. may as well get as far as possible.
all:	step0 step1 step2 step3 step4 step5 step6 step7 step8 step9 step10 step11 step19 step20 step21 step22 step23 step24 step30
user:	step0 step1 step2 step3 step4 step5 step6 step7 step8 step9 step10 step11 step19
root:	step0 step20 step21 step22 step23 step24 step30 step35

help:
	@sed -n 's/^#h//p' < Makefile

#h	step0	Identify the version, etc
step0:
	@echo ""
	@echo "step0	Starting the 'JIPS NTP starter pack' version $(VERSION)"
	@echo ""

#h	step1	Check the sources are available
step1:	$(TEST_SRC)
$(TEST_SRC):
	@echo "step1		Explaining how to get the sources ..."
	@echo ""
	$(CMDS)/how_to_get_src

undo2:; rm -rf $(TEST_FIX)
#h -	step2	Test basic fixes have been applied
step2: $(TEST_FIX)
$(TEST_FIX):
	@echo "step2		Checking that some basic fixes have been applied"
	@echo ""
	$(CMDS)/fix_src `pwd` $(SRC) $(TEST_FIX)

undo3:;	rm -f $(TEST_CONF)
#h -	step3	Check that the Config file exists
step3: $(TEST_CONF)
$(TEST_CONF):
	@echo "step3		Checking that Config has been set up"
	@echo ""
	$(CMDS)/setupconfig `pwd` $(SRC)

undo4:;	rm -f $(SRC)/*/Makefile
#h -	step4	Check that the makefiles have been built
step4: $(TEST_MKF)
$(TEST_MKF):
NOT$(TEST_MKF):	
	@echo "step4		Checking that the Makefiles have been built"
	@echo ""
	cd $(SRC); make makefiles

undo5:;	cd $(SRC); make clean
#h -	step5	Check that the binaries have been built
step5: $(TEST_OBJ)
$(TEST_OBJ):
	@echo "step5		Checking that the binaries have been built"
	@echo ""
	@echo " (three lines of 'ar: filename authmd5encrypt.o truncated to authmd5encrypt.'"
	@echo "  and a couple of 'ranlib: warning: libntp.a(hpux.o): no symbol table' are OK)"
	@echo ""
	cd $(SRC); make

#h	step6	Test the auth code
step6:
	@echo "step6		Testing the auth code works correctly"
	@$(AUTHC) < $(AUTHD) | grep -v ' OK$$' | cat
	@$(AUTHC) < $(AUTHD) | sh -c "if grep -v ' OK$$';then false;else true;fi"
	@$(AUTHC) < $(AUTHD) > /dev/null

#h	step7	Check that ntpdate can find a server
step7:
	@echo "step7		Checking ntpdate can find a server"
	@echo ""
	@echo "Should end with something like:"
	@echo "	ntpdate: adjust time server 128.232.0.6 offset 0.0328031"
	@echo "or"
	@echo "	ntpdate: step time server 128.232.0.6 offset 0.7243485"
	@echo ""
	$(NTPDATE) -d $(IPSERV) $(NTPDATEOPT)

undo8:;	rm -f $(RCONF_LOCL)
#h -	step8	Create a local raw config file (using an IP address)
step8:	$(RCONF_LOCL)
$(RCONF_LOCL):
	@echo "step8		Creating local raw config file ($@)"
	@echo ""
	cp $(RCONF_DIST) $@

undo9:; rm -f$(CONF_LOCL)
#h -	step9	Create a local config file (using names)
step9:	$(CONF_LOCL)
$(CONF_LOCL):
	@echo "step9		Creating local config file ($@)"
	@echo ""
	$(MAKE) $(MFLAGS) -f Makefile -f $(SMKF) X-$@
	mv X-$@ $@
X-$(CONF_LOCL):
	NSERV=$(NSERV) BINDIR=$(BINDIR) $(CMDS)/makeconf > $@-t
	mv $@-t $@

undo10:; rm -f $(KEYS_LOCL)
#h -	step10	Create a local keys file -- needs user input
step10:	$(KEYS_LOCL)
$(KEYS_LOCL):
	@echo "step10		Creating local keys file ($@) -- needs user input"
	@echo ""
	$(CMDS)/makekeys > $@-t
	chmod g-rw,o-rw $@-t
	mv $@-t $@

#h	step11	Check that ntp/udp is in /etc/services
step11:
	@echo "step11		Check that ntp/udp is in /etc/services"
	@echo ""
	@echo "	expect something like:"
	@echo "ntp             123/udp"
	@echo ""
	$(CMDS)/checkservices

step19:
	@echo ""
	@echo "step19		Rest of the commands must be run as root (e.g. su -c make root)"
	@echo "		========================================"
	@echo ""

#h	step20	Check that ntpdate can set the clock (run as root)
step20:
	@echo "step20		Checking ntpdate can set the clock (run as root)"
	@echo ""
	@$(CMDS)/checkroot set the clock -- e.g. \  su -c make root
	@echo "Should end with something like:"
	@echo "	ntpdate: adjust time server 128.232.0.6 offset 0.0328031"
	@echo ""
	$(NTPDATE) $(IPSERV) $(NTPDATEOPT)

#h	step21	Start a raw xntpd in debug mode (run as root)
step21:
	@echo "step21		Starting raw xntpd in debug mode (must be root)"
	@echo ""
	@$(CMDS)/checkroot run xntpd
	@echo "	Should say something like:"
	@echo "kernel vars: tickadj = 1000, tick = 10000"
	@echo "adj_precision = 1000, tvu_maxslew = 99000, tsf_maxslew = 0.19581058"
	@echo "init_io: maxactivefd 6"
	@echo "peer_config: addr 128.232.0.6 mode 3 version 3 key 0"
	@echo "sendpkt(128.232.0.6, 0.0.0.0, 48)"
	@echo "clock_filter(128.232.0.6, 0.017942, 0.00485, 0.01563)"
	@echo "clock_update(128.232.0.6)"
	@echo "combine: offset 0.017942"
	@echo "	then wurble a bit more each minute"
	@echo ""
	@echo "	Hit return to stop when you have had enough"
	@echo ""
	@$(CMDS)/checkroot run xntpd
	$(XNTPD) -d -c $(RCONF_LOCL)& kill=$$!; read x; kill $$kill

#h	step22	Check xntpdc	(run as root)
step22:
	@echo "step22		Checking xntpdc (must be root)"
	@echo ""
	@$(CMDS)/checkroot run xntpd
	$(CMDS)/checkxntpdc $(SRC) $(RCONF_LOCL)

#h	step23	Check tickadj	(run as root)
step23:
	@echo "step23		Checking tickadj (must be root)"
	@echo ""
	@$(CMDS)/checkroot run tickadj
	$(TICKADJ)
	@echo ""
	$(TICKADJ) -A > /dev/null
	@echo ""
	@echo "$(TICKADJ) -s > /dev/null"
	@sh -c "if $(TICKADJ) -s > /dev/null; then echo ''; echo '++	Include -s in tickadj args in /etc/rc'; \
		else echo ''; echo '++	Dont include -s in tickadj args in /etc/rc'; fi"
	@echo ""
	$(TICKADJ)

undo24:; $(MAKE) $(MFLAGS) -f $(SMKF) -f Makefile unmkbindir
unmkbindir:
	@$(CMDS)/checkroot delete $(BINDIR)
	-rmdir $(BINDIR)
#h -	step24	Check the BINDIR exists (run as root)
step24:
	@echo "step24		Checking that BINDIR exists (nust be root)"
	@echo ""
	$(MAKE) $(MFLAGS) -f $(SMKF) -f Makefile mkbindir
mkbindir: $(BINDIR)
unused $(BINDIR):
	@$(CMDS)/checkroot create $(BINDIR)
	mkdir -p $@

#h	step30	Install binaries (run as root)
step30:	$(TEST_OBJ) $(CONF_LOCL) $(KEYS_LOCL) step30-1 step30-2 step30-3 step30-4
step30-1:
	@echo "step30-1		Installing binaries and tailor files (must be root)"
	@echo ""
	@echo "	Note that tickadj and man pagers are *not* installed ....."
	@echo ""
	@$(CMDS)/checkroot install things
step30-2:
	cd $(SRC); make install
step30-3: $(ICONF)
$(ICONF):
	cp $(CONF_LOCL) $(ICONF)
step30-4: $(IKEYS)
$(IKEYS):
	cp $(KEYS_LOCL) $(IKEYS)

#h	step35	Explain how to set up etc/rc (etc)
step35:	
	@echo "step35		Explaining how to set up etc/rc (etc)"
	@echo ""
	@$(CMDS)/explainrc

tar:	$(TARS)
	tar cf $@-t $(TARS) && mv $@-t $@

tars:	$(TARS) $(TARE)
	tar cf $@-t $(TARS) $(TARE) && mv $@-t $@

MANIFEST:	Makefile
	echo $(TARS) | tr ' ' '\12' > $@-t && mv $@-t $@
