#!/bin/sh

# set SVDIR
svrundir=/etc/sv

# either a valid basename or allsv
service="${1}"

case $service in
   allsv)
	allsv=1
	;;
   *)
	if [ ! -d "$svrundir/$service" ]; then
		echo " error: $service directory not found "
		exit 1
	fi
	;;
esac

make_supervise_links () {
	if [ ! -h "$svrundir/$service/supervise" ] && [ ! -d "$svrundir/$service/supervise" ]; then
		ln -s /run/runit/supervise/"$service" "$svrundir/$service/supervise"
	fi
	if [ -d "$svrundir/$service/log" ]; then
		if [ ! -h "$svrundir/$service/log/supervise" ] && [ ! -d "$svrundir/$service/log/supervise" ]; then
			ln -s /run/runit/supervise/"$sv".log "$svrundir/$service"/log/supervise
		fi
	fi
}

if [ -z $allsv ]; then
	make_supervise_links
else
	for path in $svrundir/* ; do
		service=$(basename "$path")
		make_supervise_links
	done
fi
