# include $(top_srcdir)/Makefile.paths

# script locations
armascriptdir=$(scriptdir)

# Handle which scripts to build
basercScripts=rcd_config rcd_startstop

# include autogenerated makefile that generates 
# the universal variable replacement sed script
../universal_variable_values_makefile:
	cd ..; ./config.status universal_variables
@silent_inc@ ../universal_variable_values_makefile

if BUILDREGULAR
regScripts=
regrcScripts=
regrcScriptLN=
endif

if BUILDMASTER
masterScripts=masterstarter
masrcScripts=rcd_master
endif

if BUILDDEDICATED
dedScripts=serverstarter
dedrcScripts=rcd_server
endif

scriptsToBuild=$(basercScripts) $(masterScripts) $(dedScripts) $(regScripts) sysinstall relocate
rcScripts=$(regrcScripts) $(masrcScripts) $(dedrcScripts)

armascript_SCRIPTS=$(scriptsToBuild) $(rcScripts)
#armainitscript_SCRIPTS=

# What goes in a distribution

EXTRA_DIST=\
	docker-entrypoint.sh.in\
	install.in\
	masterstarter.in\
	rcd_config.in\
	rcd_master.in\
	rcd_server.in\
	rcd_startstop.in\
	serverstarter.in\
	sysinstall.in\
	starter.in\
	stat.in\
    make/uninstall\
	make/releasetag\
	make/sortresources.py\
	make/sortresources\
	make/regenerate_fingerprint

# The command for building scripts
buildscript = sed -f ${top_builddir}/universal_variable_values_sed\
        -e 's,@version\@,$(VERSION),g' \
        -e 's,@executable_suffix\@,$(executable_suffix),g' \
	    -e "s,/bin/bash,`which bash`,g"\
		-e 's,@LN_S\@,$(LN_S),g'\
        -e 's,@progname\@,@progname@,g' \
		-e 's,@prognamebase\@,@prognamebase@,g' \
		-e 's,@progtitle\@,@progtitle@,g' \
		-e 's,@progid\@,@progid@,g' \
		-e 's,@enable_useradd\@,@enable_useradd@,g' \
		-e 's,@enable_etc\@,@enable_etc@,g' \
		-e 's,@enable_initscripts\@,@enable_initscripts@,g' \
		-e 's,@enable_desktop\@,@enable_desktop@,g' \
		-e 's,@enable_restoreold\@,@enable_restoreold@,g'\
		-e 's,@progdir_suffix\@,@progdir_suffix@,g'

%: %.in Makefile ${top_builddir}/universal_variable_values_sed
#	just replace the @foo@s with the variable values
	$(buildscript) < $< > $@
	chmod 755 $@

sysinstall: sysinstall.in Makefile ${top_builddir}/universal_variable_values_sed relocate
#	do almost the same as for regular scripts...
	$(buildscript) < $< > $@.proto
#	only replace the occurence of RELOCATECOMMANDS with the
#	contents of the relocation script. We need to inject it
#	rather than just call it because the relocate script relies on
#	a shell function being available.
#   Some sed's don't have T, so we have to make it a little more complex.
	sed -e "s,RELOCATECOMMANDS,\\\," -e "t rel" -e "b" -e ": rel" -e "r relocate" < $@.proto > $@
	rm $@.proto
	chmod 755 $@

clean-local:
	rm -f $(armascript_SCRIPTS)
