#!/bin/sh
#
# start/stop sbrsh daemons.

if ! [ -x `which sbrshd` ]; then
    exit 0
fi

SBRSHD=/sbin/sbrshd
PIDFILE=/var/run/sbrshd.pid

LONGNAME="Scratchbox Remote Shell daemon"

case "$1" in
    start)
        echo -n "Starting $LONGNAME: "

        if [ -f $PIDFILE ]; then
            PID=$(cat $PIDFILE)

            if [ "$PID" ] && [ -d /proc/$PID ]; then
		echo "already running!"
		exit 1
            else
                rm $PIDFILE
            fi
        fi

        if [ -e /etc/shells ]; then
	    PID=$($SBRSHD)
	else
	    PID=$($SBRSHD -S /bin/sh:/bin/bash:/bin/zsh)
        fi

        if [ "$PID" ]; then
	    echo "done."
	    echo $PID > $PIDFILE
	else
	    exit 1
        fi
        ;;

    stop)
        echo -n "Stopping $LONGNAME: "

        if [ -f $PIDFILE ]; then
            PID=$(cat $PIDFILE)

            if [ "$PID" ] && [ -d /proc/$PID ]; then
                if kill $PID; then
                    echo "done."
                fi
            else
                echo "was not running.."
            fi

            rm $PIDFILE
        else
            echo "pid file not found!"
        fi
        ;;

    restart)
        $0 stop
	sleep 1
        $0 start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
