#!/bin/sh

services="/"

mkexe()
{
exe=$0
while true; do
	cd $(dirname $exe)
	exe=$(basename $exe)
	[ -L $exe ] || break
	exe=$(readlink $exe)
done
echo $(pwd)/$exe
}
exe=$(mkexe)

services_arg()
{
for i in $services ; do
	echo -n "-s '$i:conspy:conspy:/tmp:LINES=25 /bin/sh -c \"$exe service $i "
	echo -n "\${peer} \${url} \${columns} \${lines}\"' "
done
}

pidfile=/var/run/shellinaboxd.pid
case "$1" in
service)
	case "$2" in
	/)
		ulimit -t 60
		exec /bin/nice -n 19 /usr/bin/conspy -f -v 0 ;;
	esac
	;;
start)
	if ! grep -q ^conspy: /etc/passwd; then
		adduser -h /tmp -S -D -H conspy
		addgroup conspy tty
	fi
	dir=$(dirname $exe)
	eval shellinaboxd --background=$pidfile --disable-ssl \
		--css=/usr/share/shellinabox/object.css \
		--user-css 10px:-/usr/share/shellinabox/10px.css,12px:-/usr/share/shellinabox/12px.css,14px:-/usr/share/shellinabox/14px.css,16px:+/usr/share/shellinabox/16px.css \
		--localhost-only --no-beep -s /:LOGIN
	;;
stop)
	[ -s $pidfile ] && kill $(cat $pidfile)
	;;
esac
