NULL=

bin_PROGRAMS=	$(NTPD_DB) $(NTPDSIM_DB)
libexec_PROGRAMS=	$(NTPD_DL) $(NTPDSIM_DL)
sbin_PROGRAMS=	$(NTPD_DS) $(NTPDSIM_DS)

noinst_LIBRARIES=	libntpd.a

AM_CFLAGS = $(CFLAGS_NTP)
AM_CFLAGS += $(NTP_HARD_CFLAGS)

AM_CPPFLAGS  = $(NTP_INCS)
AM_CPPFLAGS += $(LIBOPTS_CFLAGS)
AM_CPPFLAGS += $(CPPFLAGS_NTP)
AM_CPPFLAGS += $(NTP_HARD_CPPFLAGS)

AM_LDFLAGS = $(LDFLAGS_NTP)
AM_LDFLAGS += $(NTP_HARD_LDFLAGS)

# LDADD might need RESLIB and ADJLIB.
LDADD = libntpd.a $(LIBPARSE)
AM_YFLAGS = -d -t -r all

if SAVECONFIG_ENABLED
if NTP_CROSSCOMPILE
CHECK_SAVECONFIG=
else
CHECK_SAVECONFIG= check-saveconfig check-pslsaveconfig
endif
else !SAVECONFIG_ENABLED
CHECK_SAVECONFIG=
endif

#
# VPHACK and VPHACK_AFTER are enabled on non-GNU makes (such as
# BSD make) to work around issues specific to compiling
# ntp_parser.y into ntp_parser.h and ntp_parser.c in a VPATH
# configuration where we would like (for a change) the output
# files ntp_parser.[ch] to be placed in the source directory,
# as opposed to the build directory.  This allows a single
# host of a flock configured with Bison to update ntp_parser.[ch]
# used by the rest.
#

if VPATH_HACK
VPHACK=	vphack
VPHACK_AFTER= vphack_after
else
VPHACK=
VPHACK_AFTER=
endif

vphack:
	$(AM_V_at)test -e ntp_parser.c || ln -s $(srcdir)/ntp_parser.c .
	$(AM_V_at)test -e ntp_parser.h || ln -s $(srcdir)/ntp_parser.h .

#
# ylwrap script which invokes Bison replaces ntp_parser.h
# symlink with the updated file, when ntp_parser.h changes.
# vphack_after detects this and copies the updated file to srcdir
# and re-creates the ntp_parser.h symlink in its place.
#

vphack_after:
	$(AM_V_at)test -L ntp_parser.h || ( \
		mv ntp_parser.h $(srcdir)/ntp_parser.h && \
		ln -s $(srcdir)/ntp_parser.h . \
		)

# BUILT_SOURCES which should also be in EXTRA_DIST
B_S_DIST=			\
	$(srcdir)/ntpd-opts.c	\
	$(srcdir)/ntpd-opts.h	\
	$(NULL)

BUILT_SOURCES=		\
	$(VPHACK)	\
	$(LIBPARSE)	\
	ntp_parser.c	\
	ntp_parser.h	\
	$(VPHACK_AFTER)	\
	$(B_S_DIST)	\
	$(NULL)

man1_MANS=
man5_MANS=	ntp.conf.5 ntp.keys.5
man8_MANS=
man_MANS=	ntpd.$(NTPD_MS)

LDADD_NTPD_COMMON  = $(LDADD_LIBNTP) $(LIBOPTS_LDADD) $(PTHREAD_LIBS)
LDADD_NTPD_COMMON += $(LIBM) $(LDADD_NTP) $(LSCF)
ntpd_LDADD = $(LDADD) ../libntp/libntp.a $(LDADD_NTPD_COMMON) $(LDADD_LIBUTIL)
ntpdsim_LDADD = $(LDADD) ../libntp/libntpsim.a $(LDADD_NTPD_COMMON)
ntpdsim_CPPFLAGS = $(AM_CPPFLAGS) -DSIM
check_y2k_LDADD = $(LDADD) ../libntp/libntp.a $(LDADD_LIBNTP) $(LIBM) $(LDADD_LIBNTP) $(PTHREAD_LIBS)
## we don't want $(LDADD) in keyword_gen_LDADD
keyword_gen_LDADD = ../libntp/libntp.a $(LDADD_LIBNTP) $(LIBM) $(PTHREAD_LIBS)

DISTCLEANFILES =		\
	keyword-gen		\
	config.log		\
	$(man5_MANS)		\
	$(man_MANS)		\
	$(NULL)

CLEANFILES =			\
	check-psl0		\
	check-psl1		\
	check-psl2		\
	check-pslsaveconfig	\
	check-saveconfig	\
	compsave.conf		\
	compsave.conf+		\
	k-g-u-submake		\
	psl0save.conf		\
	psl0save.conf+		\
	psl1save.conf		\
	psl1save.conf+		\
	psl2save.conf		\
	psl2save.conf+		\
	.version		\
	version.c		\
	ntpd-version.c		\
	sim-version.c		\
	$(EXTRA_PROGRAMS)	\
	$(NULL)

EXTRA_DIST =			\
	complete.conf.in	\
	invoke-ntp.conf.menu	\
	invoke-ntp.conf.texi	\
	invoke-ntp.keys.menu	\
	invoke-ntp.keys.texi	\
	invoke-ntpd.menu	\
	invoke-ntpd.texi	\
	keyword-gen-utd		\
	ntp.conf.5man		\
	ntp.conf.5mdoc		\
	ntp.conf.def		\
	ntp.conf.man.in		\
	ntp.conf.mdoc.in	\
	ntp.conf.html		\
	ntp.conf.texi		\
	ntp.keys.5man		\
	ntp.keys.5mdoc		\
	ntp.keys.def		\
	ntp.keys.man.in		\
	ntp.keys.mdoc.in	\
	ntp.keys.html		\
	ntp.keys.texi		\
	ntpd-opts.def		\
	ntpd.1ntpdman		\
	ntpd.1ntpdmdoc		\
	ntpd.man.in		\
	ntpd.mdoc.in		\
	ntpd.html		\
	ntpd.texi		\
	ntpdbase-opts.def	\
	psl0.conf		\
	psl1.conf		\
	psl2.conf		\
	refclock_msfees.c	\
	$(B_S_DIST)		\
	$(NULL)

###							Y2Kfixes
check_PROGRAMS = @MAKE_CHECK_Y2K@
EXTRA_PROGRAMS = check_y2k keyword-gen ntpd ntpdsim

html_DATA=				\
	$(srcdir)/ntp.conf.html		\
	$(srcdir)/ntp.keys.html		\
	$(srcdir)/ntpd.html		\
	$(NULL)

noinst_DATA =				\
	$(srcdir)/invoke-ntp.conf.menu	\
	$(srcdir)/invoke-ntp.conf.texi	\
	$(srcdir)/invoke-ntp.keys.menu	\
	$(srcdir)/invoke-ntp.keys.texi	\
	$(srcdir)/invoke-ntpd.menu	\
	$(srcdir)/invoke-ntpd.texi	\
	$(srcdir)/ntp.conf.man.in	\
	$(srcdir)/ntp.conf.mdoc.in	\
	$(srcdir)/ntp.keys.man.in	\
	$(srcdir)/ntp.keys.mdoc.in	\
	$(srcdir)/ntpd.man.in		\
	$(srcdir)/ntpd.mdoc.in		\
	$(NULL)

noinst_HEADERS =			\
	declcond.h			\
	ntp_clockdev.h			\
	ntp_leapsec.h			\
	$(NULL)

install-data-local: install-html

run_ag=		cd $(srcdir) &&	env PATH="$(abs_builddir):$(PATH)" AUTOGEN_DNE_DATE=-D	\
		autogen -L ../sntp/include -L ../sntp/ag-tpl --writable

std_def_list =							\
	$(top_srcdir)/sntp/include/debug-opt.def		\
	$(top_srcdir)/sntp/include/autogen-version.def		\
	$(top_srcdir)/sntp/include/copyright.def		\
	$(top_srcdir)/sntp/include/homerc.def			\
	$(top_srcdir)/sntp/include/ntp.lic			\
	$(top_srcdir)/sntp/include/version.def			\
	$(NULL)

check-local: $(MAKE_CHECK_Y2K) $(CHECK_SAVECONFIG)
	test -z "$(MAKE_CHECK_Y2K)" || ./$(MAKE_CHECK_Y2K)

# ntpd_ntpdsim_SRCS use #ifdef SIM and are included in both

ntpd_ntpdsim_SRCS =		\
	ntp_config.c		\
	ntp_keyword.h		\
	ntp_io.c		\
	ntp_parser.y		\
	ntp_scanner.c		\
	ntp_scanner.h		\
	ntpd.c			\
	ntpd-opts.c		\
	ntpd-opts.h		\
	$(NULL)

ntpd_SOURCES =			\
	$(ntpd_ntpdsim_SRCS)	\
	$(NULL)

nodist_ntpd_SOURCES =		\
	ntpd-version.c		\
	$(NULL)

ntpdsim_SOURCES =		\
	$(ntpd_ntpdsim_SRCS)	\
	ntp_prio_q.c		\
	ntpsim.c		\
	$(NULL)

nodist_ntpdsim_SOURCES =	\
	sim-version.c		\
	$(NULL)

# libntpd_a_SOURCES do not use #ifdef SIM

libntpd_a_SOURCES =		\
	cmd_args.c		\
	jupiter.h		\
	ntp_clockdev.c		\
	ntp_control.c		\
	ntp_crypto.c		\
	ntp_filegen.c		\
	ntp_leapsec.c		\
	ntp_loopfilter.c	\
	ntp_monitor.c		\
	ntp_peer.c		\
	ntp_proto.c		\
	ntp_ppsdev.c		\
	ntp_refclock.c		\
	ntp_request.c		\
	ntp_restrict.c		\
	ntp_signd.c		\
	ntp_timer.c		\
	ntp_util.c		\
	ppsapi_timepps.h	\
	rc_cmdlength.c		\
	refclock_acts.c		\
	refclock_arbiter.c	\
	refclock_arc.c		\
	refclock_as2201.c	\
	refclock_atom.c		\
	refclock_bancomm.c	\
	refclock_chronolog.c	\
	refclock_chu.c		\
	refclock_conf.c		\
	refclock_datum.c	\
	refclock_dumbclock.c	\
	refclock_fg.c		\
	refclock_gpsdjson.c	\
	refclock_gpsvme.c	\
	refclock_heath.c	\
	refclock_hopfser.c	\
	refclock_hopfpci.c	\
	refclock_hpgps.c	\
	refclock_irig.c		\
	refclock_jjy.c		\
	refclock_jupiter.c	\
	refclock_leitch.c	\
	refclock_local.c	\
	refclock_mx4200.c	\
	refclock_neoclock4x.c	\
	refclock_nmea.c		\
	refclock_oncore.c	\
	refclock_palisade.c	\
	refclock_palisade.h	\
	refclock_parse.c	\
	refclock_pcf.c		\
	refclock_pst.c		\
	refclock_ripencc.c	\
	refclock_shm.c		\
	refclock_tpro.c		\
	refclock_true.c		\
	refclock_tt560.c	\
	refclock_ulink.c	\
	refclock_wwv.c		\
	refclock_wwvb.c		\
	refclock_zyfer.c	\
	refclock_tsyncpci.c	\
	$(NULL)

k-g-u-submake: keyword-gen
	$(AM_V_at)./keyword-gen $(srcdir)/ntp_parser.h > k-g.out
	$(AM_V_at)grep -v diff_ignore_line < k-g.out > cmp1
	$(AM_V_at)grep -v diff_ignore_line < $(srcdir)/ntp_keyword.h > cmp2
	$(AM_V_at)cmp cmp1 cmp2 > /dev/null || \
		{ mv -f k-g.out $(srcdir)/ntp_keyword.h && \
		  echo 'Generated changed ntp_keyword.h.' ;}
	$(AM_V_at)[ ! -f k-g.out ] || \
		{ rm k-g.out && echo 'ntp_keyword.h is up to date.' ;}
	@rm cmp1 cmp2
	@echo 'keyword-gen and ntp_keyword.h are up to date.' > $@

$(srcdir)/keyword-gen-utd: $(srcdir)/keyword-gen.c $(srcdir)/ntp_parser.h
	$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) k-g-u-submake	# avoid explicit dependency
	$(AM_V_at)grep diff_ignore_line $(srcdir)/ntp_keyword.h > k-g-u
	$(AM_V_at)mv -f k-g-u $@

$(srcdir)/ntp_keyword.h: $(srcdir)/keyword-gen-utd
	@: do-nothing action to avoid default SCCS get
	@: .h updated if needed by k-g-u-submake rule

$(srcdir)/ntpd-opts.h: $(srcdir)/ntpd-opts.c
	@: do-nothing action to avoid default SCCS get, .h built with .c

$(srcdir)/ntpd-opts.c: $(srcdir)/ntpd-opts.def $(srcdir)/ntpdbase-opts.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) ntpd-opts.def

###

$(srcdir)/ntpd.1ntpdman: $(srcdir)/ntpd-opts.def $(srcdir)/ntpdbase-opts.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=1ntpdman -Tagman-cmd.tpl ntpd-opts.def

$(srcdir)/ntpd.man.in: $(srcdir)/ntpd.1ntpdman $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntpd.1ntpdman > $(srcdir)/ntpd.man.in+
	mv $(srcdir)/ntpd.man.in+ $(srcdir)/ntpd.man.in

###

$(srcdir)/ntpd.1ntpdmdoc: $(srcdir)/ntpd-opts.def $(srcdir)/ntpdbase-opts.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=1ntpdmdoc -Tagmdoc-cmd.tpl ntpd-opts.def

$(srcdir)/ntpd.mdoc.in: $(srcdir)/ntpd.1ntpdmdoc $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntpd.1ntpdmdoc > $(srcdir)/ntpd.mdoc.in+
	mv $(srcdir)/ntpd.mdoc.in+ $(srcdir)/ntpd.mdoc.in

###

ntpd.$(NTPD_MS): $(srcdir)/ntpd.$(MANTAGFMT).in $(top_builddir)/config.status
	$(top_builddir)/config.status --file=ntpd.$(NTPD_MS)+:$(srcdir)/ntpd.$(MANTAGFMT).in
	mv ntpd.$(NTPD_MS)+ ntpd.$(NTPD_MS)

###

$(srcdir)/invoke-ntp.conf.menu: $(srcdir)/invoke-ntp.conf.texi
	@: do-nothing action to avoid default SCCS get, .menu built with .texi

$(srcdir)/invoke-ntp.conf.texi: $(srcdir)/ntp.conf.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -Tagtexi-file.tpl -DLEVEL=section ntp.conf.def

$(srcdir)/invoke-ntp.keys.menu: $(srcdir)/invoke-ntp.keys.texi
	@: do-nothing action to avoid default SCCS get, .menu built with .texi

$(srcdir)/invoke-ntp.keys.texi: $(srcdir)/ntp.keys.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -Tagtexi-file.tpl -DLEVEL=section ntp.keys.def

$(srcdir)/ntp.conf.html: $(srcdir)/ntp.conf.texi $(srcdir)/invoke-ntp.conf.texi $(top_srcdir)/sntp/include/version.texi
	cd $(srcdir) && ( makeinfo --force --html --no-split -o ntp.conf.html ntp.conf.texi || true )

$(srcdir)/ntp.keys.html: $(srcdir)/ntp.keys.texi $(srcdir)/invoke-ntp.keys.texi $(top_srcdir)/sntp/include/version.texi
	cd $(srcdir) && ( makeinfo --force --html --no-split -o ntp.keys.html ntp.keys.texi || true )

$(srcdir)/ntpd.html: $(srcdir)/ntpd.texi $(srcdir)/invoke-ntpd.texi $(top_srcdir)/sntp/include/version.texi
	cd $(srcdir) && ( makeinfo --force --html --no-split -o ntpd.html ntpd.texi || true )

###

$(srcdir)/ntp.conf.5man: $(srcdir)/ntp.conf.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=5man -Tagman-cmd.tpl ntp.conf.def

$(srcdir)/ntp.conf.man.in: $(srcdir)/ntp.conf.5man $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntp.conf.5man > $(srcdir)/ntp.conf.man.in+
	mv $(srcdir)/ntp.conf.man.in+ $(srcdir)/ntp.conf.man.in

###

$(srcdir)/ntp.conf.5mdoc: $(srcdir)/ntp.conf.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=5mdoc -Tagmdoc-cmd.tpl ntp.conf.def

$(srcdir)/ntp.conf.mdoc.in: $(srcdir)/ntp.conf.5mdoc $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntp.conf.5mdoc > $(srcdir)/ntp.conf.mdoc.in+
	mv $(srcdir)/ntp.conf.mdoc.in+ $(srcdir)/ntp.conf.mdoc.in

###

ntp.conf.5: $(srcdir)/ntp.conf.$(MANTAGFMT).in $(top_builddir)/config.status
	$(top_builddir)/config.status --file=ntp.conf.5+:$(srcdir)/ntp.conf.$(MANTAGFMT).in
	mv ntp.conf.5+ ntp.conf.5

###

$(srcdir)/ntp.keys.5man: $(srcdir)/ntp.keys.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=5man -Tagman-file.tpl ntp.keys.def

$(srcdir)/ntp.keys.man.in: $(srcdir)/ntp.keys.5man $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntp.keys.5man > $(srcdir)/ntp.keys.man.in+
	mv $(srcdir)/ntp.keys.man.in+ $(srcdir)/ntp.keys.man.in

###

$(srcdir)/ntp.keys.5mdoc: $(srcdir)/ntp.keys.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -DMAN_SECTION=5mdoc -Tagmdoc-file.tpl ntp.keys.def

$(srcdir)/ntp.keys.mdoc.in: $(srcdir)/ntp.keys.5mdoc $(top_srcdir)/sntp/scripts/mansec2subst.sed
	sed -f $(top_srcdir)/sntp/scripts/mansec2subst.sed $(srcdir)/ntp.keys.5mdoc > $(srcdir)/ntp.keys.mdoc.in+
	mv $(srcdir)/ntp.keys.mdoc.in+ $(srcdir)/ntp.keys.mdoc.in

###

ntp.keys.5: $(srcdir)/ntp.keys.$(MANTAGFMT).in $(top_builddir)/config.status
	$(top_builddir)/config.status --file=ntp.keys.5+:$(srcdir)/ntp.keys.$(MANTAGFMT).in
	mv ntp.keys.5+ ntp.keys.5

###

$(srcdir)/invoke-ntpd.menu: $(srcdir)/invoke-ntpd.texi
	@: do-nothing action to avoid default SCCS get, .menu built with .texi

$(srcdir)/invoke-ntpd.texi: $(srcdir)/ntpd-opts.def $(srcdir)/ntpdbase-opts.def $(std_def_list)
	$(AM_V_GEN)$(run_ag) -Tagtexi-cmd.tpl -DLEVEL=section ntpd-opts.def
	$(top_srcdir)/scripts/build/check--help $@

check-saveconfig: complete.conf compsave.conf
	-diff -u complete.conf compsave.conf
	cmp complete.conf compsave.conf && echo stamp > $@

compsave.conf: ntpd complete.conf
	./ntpd --configfile complete.conf --saveconfigquit $@+
	grep -v '^#' $@+ > $@

check-pslsaveconfig: check-psl0 check-psl1 check-psl2
	echo stamp > $@

check-psl0: psl0.conf psl0save.conf
	-diff -u $(srcdir)/psl0.conf psl0save.conf
	cmp $(srcdir)/psl0.conf psl0save.conf && echo stamp > $@

psl0save.conf: psl0.conf ntpd
	./ntpd --configfile $(srcdir)/psl0.conf --saveconfigquit $@+
	grep -v '^#' $@+ > $@

check-psl1: psl1.conf psl1save.conf
	-diff -u $(srcdir)/psl1.conf psl1save.conf
	cmp $(srcdir)/psl1.conf psl1save.conf && echo stamp > $@

psl1save.conf: psl1.conf ntpd
	./ntpd --configfile $(srcdir)/psl1.conf --saveconfigquit $@+
	grep -v '^#' $@+ > $@

check-psl2: psl2.conf psl2save.conf
	-diff -u $(srcdir)/psl2.conf psl2save.conf
	cmp $(srcdir)/psl2.conf psl2save.conf && echo stamp > $@

psl2save.conf: psl2.conf ntpd
	./ntpd --configfile $(srcdir)/psl2.conf --saveconfigquit $@+
	grep -v '^#' $@+ > $@

../libntp/libntpsim.a:
	cd ../libntp && $(MAKE) $(AM_MAKEFLAGS) libntpsim.a

../libparse/libparse.a:
	cd ../libparse && $(MAKE) $(AM_MAKEFLAGS) check-libparse

ntpd-version.c: Makefile $(top_srcdir)/sntp/scm-rev
	$(AM_V_GEN)env CSET=`cat $(top_srcdir)/sntp/scm-rev` $(top_builddir)/scripts/build/mkver ntpd
	$(AM_V_at)mv -f version.c $@

sim-version.c: Makefile $(top_srcdir)/sntp/scm-rev
	$(AM_V_GEN)env CSET=`cat $(top_srcdir)/sntp/scm-rev` $(top_builddir)/scripts/build/mkver ntpdsim
	$(AM_V_at)@mv -f version.c $@

$(srcdir)/Makefile.am:
	@: do-nothing

include $(top_srcdir)/bincheck.mf
include $(top_srcdir)/check-libopts.mf
include $(top_srcdir)/check-libntp.mf
include $(top_srcdir)/check-scm-rev.mf
include $(top_srcdir)/depsver.mf
include $(top_srcdir)/includes.mf
