[Main]
Type = oneshot
Description = "rc-local user service"
User = ( user )
MaxDeath = 3
Notify = 4
TimeoutStart = 3000
Version = 0.0.2.1

[Start]
Execute = ( 
	importas -DS "0" UID
	importas -S HOME
	define LOG_DIR /var/tmp/log/${UID}/66/rc-local
	define SCRIPT_DIR ${HOME}/.66/script/rc-local
	backtick -n TIMESTAMP { date +%Y%m%d-%H%M%S }
	importas -S TIMESTAMP
	backtick -n SCRIPTS { ls -1 ${SCRIPT_DIR} }
	importas -s SCRIPTS SCRIPTS
	
	foreground { mkdir -p ${LOG_DIR} }
	foreground {
		foreground { ${HOME}/.66/rc.local }
		redirfd -a 1 ${LOG_DIR}/rc.local.log
		importas -u EXIT_CODE ?
		echo @${TIMESTAMP}: ${HOME}/.66/rc.local: script executed with exit code ${EXIT_CODE}
	}
	forx -E script { ${SCRIPTS} }
		if { pipeline { echo ${script} } grep -q "\.start$" }
	foreground {
		foreground { ${SCRIPT_DIR}/${script} }
		redirfd -a 1 ${LOG_DIR}/${script}.log
		importas -u EXIT_CODE ?
		echo @${TIMESTAMP}: ${SCRIPT_DIR}/${script}: script executed with exit code ${EXIT_CODE}
	}
)

[Stop]
Execute = (
	importas -DS "0" UID
	importas -S HOME
	define LOG_DIR /var/tmp/log/${UID}/66/rc-local
	define SCRIPT_DIR ${HOME}/.66/script/rc-local
	backtick -n TIMESTAMP { date +%Y%m%d-%H%M%S }
	importas -S TIMESTAMP
	backtick -n SCRIPTS { ls -1 ${SCRIPT_DIR} }
	importas -s SCRIPTS SCRIPTS
	
	foreground { mkdir -p ${LOG_DIR} }
	forx -E script { ${SCRIPTS} }
		if { pipeline { echo ${script} } grep -q "\.stop$" }
	foreground {
		foreground { ${SCRIPT_DIR}/${script} }
		redirfd -a 1 ${LOG_DIR}/${script}.log
		importas -u EXIT_CODE ?
		echo @${TIMESTAMP}: ${SCRIPT_DIR}/${script}: script executed with exit code ${EXIT_CODE}
	}
)

