#!/bin/sh

if [ -z "$ZEUSHOME" ]
then
  cd `dirname $0`/..
  ZEUSHOME=`pwd`
  export ZEUSHOME
fi

PIDFILE=$ZEUSHOME/admin/internal/pid
COMMAND=$ZEUSHOME/admin/bin/zeus.admin
PROGRAM="Stingray Administration Server"
PLATFORM=`uname`

# The best available approximation to a portable way to get ps to say
# whether a process ID is currently live ... only MacOSX needs a tweak.
case $PLATFORM in
    Darwin|MacOSX|Rhapsody) dashp='-w -w -w -p';;
    Linux|FreeBSD) dashp='-w -w -w -f -p';;
    *) dashp='-f -p';;
esac

# Usage: live_pid pid [prog_name]
live_pid()
{
    PID=$1
    PROCNAME=`basename $2`
    ps $dashp $PID 2>/dev/null | sed -e "s@.*$PID .*$PROCNAME@MATCH@" |grep '^MATCH' >/dev/null 2>&1
    if [ $? = 0 ] ; then
	return 0;
    fi

    # On Solaris, try without the -f
    if [ $PLATFORM = "SunOS" ] ; then
	# without the -f we only get 8 chars of the program name
	PROC8NAME=`echo $PROCNAME | sed -e 's/\(........\).*/\1/'`;
	ps -p $PID 2>/dev/null | sed -e "s@.*$PID .*$PROC8NAME@MATCH@" |grep '^MATCH' >/dev/null 2>&1
	if [ $? = 0 ] ; then
	    return 0;
	fi
    fi

    return 1
}

unrun() # unrun pidfile program-name
{
    # Succeeds iff the process isn't running.
    if [ -f "$1" ]
    then
        PID=`sed -e 's/ .*//' < "$1"`
        PROGNAME=`sed -e 's/[0-9]* //' < "$1"`
        if [ "x$PROGNAME" = "x$PID" ]; then
            # Old style, PID-only pidfile
            PROGNAME=
        fi
        if live_pid $PID $PROGNAME
        then
            echo "$2 already running: $PID"
            return 1
        fi
        rm -f "$1"
    fi
    return 0
}

stop_pid() # stop pidfile process-name
{
    # Succeeds if no process running on return.
    if [ -f "$1" ]
    then
        PID=`sed -e 's/ .*//' < "$1"`
        PROGNAME=`sed -e 's/[0-9]* //' < "$1"`
        if [ "x$PROGNAME" = "x$PID" ]; then
            # Old style, PID-only pidfile
            PROGNAME=
        fi

        if live_pid $PID $PROGNAME
        then
            # if $PID will listen to our signals, kill it.
            if kill -0 $PID 2>/dev/null
            then
                # Have a go at killing $PID
                kill $PID
                # Now wait for it to die, or fail because it didn't:
                # but keep the wait brief if at all possible ...

                for secs in 1 1 2 3 5 8 5 3 2 1 0
                do
                    if kill -0 $PID 2>/dev/null
                    then [ $secs -eq 0 ] || sleep $secs # process still alive
                    else
                        echo $2 stopped
                        rm -f $1
                        return 0
                    fi
                done

                echo Failed to kill process $PID.
                echo It is still running your $2.
                return 1
            else
                echo I cannot send signals to process $PID.
                echo It is still running your $2.
                return 1
            fi
        else
            echo "$2 process $PID was already dead."
            rm -f "$1"
        fi
    fi
    return 0
}

case "$1" in 
  'start') 
       unrun "$PIDFILE" "$PROGRAM" || exit 0
       $COMMAND
       exit $?
       ;;

  'stop')
       if stop_pid "$PIDFILE" "$PROGRAM"
       then exit 0
       fi
       echo $PROGRAM not stopped.
       ;;

  'restart')
       if stop_pid "$PIDFILE" "$PROGRAM"
       then
            $COMMAND && echo $PROGRAM restarted.
            exit $?
       fi
       echo $PROGRAM not restarted.
       ;;

  *)
    echo "usage: $0 '{start|stop|restart}"
    ;; 
esac

exit 1
