#!/bin/sh -e

os="$(cat "${__global}/explorer/os")"
case "${os}" in
	debian|devuan)
		svdir="/etc/service"
	;;
	*bsd)
		svdir="/var/service"
	;;
	*)
		echo "Your OS '${OS}' is currently not supported." >&2
		exit 1
	;;
esac

sv="${__object_id}"
state="$(cat "${__object}/parameter/state")"
run_file="${svdir}/${sv}/run"

source="$(cat "$__object/parameter/source")"
if [ "$source" = "-" ]; then
	source="$__object/stdin"
fi

# Create this service's directory
__directory --state "${state}" "${svdir}/${sv}"

export require="__directory${svdir}/${sv}"

if [ "${state}" != "present" ]; then
	# We are done here, the service gets removed
	exit
fi

if [ -f "${__object}/parameter/log" ]; then
	# Setup logger if requested
	__directory --parents "${svdir}/${sv}/log/main"
	export require="${require} __directory${svdir}/${sv}/log/main"
	__file "${svdir}/${sv}/log/run" \
		--state "${state}" \
		--mode 0755 \
		--source "-" <<EOF
#!/bin/sh
exec svlogd -tt ./main
EOF
fi

# Setup run file
__file --state "${state}" --mode 0755 --source "${source}" "${run_file}"
