#!/bin/sh -e
# xmksv [-l] [NEWSVDIR...] - create new runit service templates

if [ "$1" = "-l" ]; then
	MKLOG=1
	shift
fi

cd ${SVDIR:-/etc/sv}

for SRV; do
	mkdir "$SRV"
	touch "$SRV/down"
	echo "#!/bin/sh" | install -m755 /dev/stdin "$SRV/run"
	if [ -w /run/runit ]; then
		ln -s /run/runit/supervise."$(printf %s "$SRV" | tr / -)" \
			"$SRV/supervise"
	fi
	if [ -n "$MKLOG" ]; then
		mkdir "$SRV/log"
		cat <<EOF | install -m755 /dev/stdin "$SRV/log/run"
#!/bin/sh
exec vlogger -t $SRV -p daemon
EOF
		if [ -w /run/runit ]; then
			ln -s /run/runit/supervise."$(printf %s "$SRV/log" | tr / -)" \
				"$SRV/log/supervise"
		fi
	fi
done
