#!@shebangdir@/execlineb -WS2

# ISC license.

multisubstitute {
	define CONFIGDIR ${1}
	define LIVEDIR ${2}
}

backtick -ED" " OLDDB { realpath ${CONFIGDIR}/compiled }
backtick -E DATE { date +%s }
elglob SOURCES ${CONFIGDIR}/src/*

if { s6-rc-compile -v2 ${CONFIGDIR}/compiled-${DATE} ${SOURCES} }

if {
	if -x0 { eltest -d ${LIVEDIR} }
	if -n -x0 { s6-rc-update -v2 -l ${LIVEDIR} ${CONFIGDIR}/compiled-${DATE} }
		if { s6-rc-update -v2 -l ${LIVEDIR} ${CONFIGDIR}/compiled }
		if { rm -Rf ${CONFIGDIR}/compiled-${DATE} }
		exit 1
}

if { ln -sf compiled-${DATE} ${CONFIGDIR}/../compiled }
if { mv -f ${CONFIGDIR}/../compiled ${CONFIGDIR}/ }

if -x0 { eltest ${OLDDB} != ${CONFIGDIR}/compiled }
rm -Rf ${OLDDB}
