#!/bin/sh

### BEGIN INIT INFO
# Provides:     sockso
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    3 5
# Default-Stop:     0 1 2 6
# Short-Description:    Sockso Media Server
# Description:      Starts the Sockso Media Server
### END INIT INFO

# function javaps "shameless stolen" from
# Copyright (c) 2000 Alexander V. Konstantinou <akonstan@acm.org>
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation. Alexander V. Konstantinou makes no
# representations about the suitability of this software for any
# purpose.  It is provided "as is" without express or implied warranty.
# 
# Description: prints out the java processes running on a Linux host.
#              Optional "-noflags" argument controls verbosity
#
# Requirements: Linux host with a /proc filesystem
#
# Updates: http://www.cs.columbia.edu/~akonstan/javaps
function javaps()
{
	NOFLAGS=0
	if [ -n "$1" -a "$1" = "-noflags" ]; then
		NOFLAGS=1
	fi

	echo "PID	Process"

	# For every file in the /proc file system
	FILES=`/bin/ls -1 /proc`

	for FILE in $FILES; do
		PROCESS_ID=$FILE
  		STATUS_FILE=/proc/$FILE/status
  		CMDLINE_FILE=/proc/$FILE/cmdline

  		# Check if it is a process directory and that we have read access
  		if [ -d "/proc/$FILE" -a -r $STATUS_FILE -a -r $CMDLINE_FILE -a "$FILE" != "/proc/self" ]; then
			PROCESS_NAME=`grep 'Name:' $STATUS_FILE | awk '{print $2}'`

    		# Only interested in java processes
    		if [ "$PROCESS_NAME" = "java" ]; then
      			PARENT_PID=`grep 'PPid' $STATUS_FILE | awk '{print $2}'`

      			# Figure out if process has a parent that is a java process
      			ISROOT=0
      			if [ $PARENT_PID -eq 1 ]; then
        			ISROOT=1
      			else
        			PARENT_NAME=`grep 'Name:' /proc/$PARENT_PID/status | awk '{print $2}'`
        			if [ "$PARENT_NAME" != "java" ]; then
          				ISROOT=1
        			fi
      			fi

      			# If root java process print out
      			if [ $ISROOT -ne 0 ]; then
        			PROCESS_CMDLINE="`cat $CMDLINE_FILE | tr '\000' ' '`"
					IGNORE_ARG=0
        			DESCR=
        			for ARG in $PROCESS_CMDLINE; do
          				if [ $NOFLAGS -eq 0 ]; then
             				DESCR="$DESCR $ARG"
          				else
            				if [ $IGNORE_ARG -eq 0 ]; then
              					if [ $ARG = "-classpath" -o $ARG = "-cp" ]; then
                					IGNORE_ARG=1
              					elif [ -z "`echo $ARG | grep '^-D'`" -a -z "`echo $ARG | grep '^-X'`" ]; then
                					DESCR="$DESCR $ARG"
              					fi
            					else 
             						IGNORE_ARG=0
            				fi
          				fi
        			done
        			echo "$PROCESS_ID	$DESCR"
      			fi
    		fi
  		fi
	done
}


#
# Determine the base and follow a runlevel link name.
#
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

PIDFILE=/var/run/sockso.pid

### Please do not edit anything below this line! ########################
#                                                                       #

. /etc/rc.status

rc_reset

return=$rc_done
case "$1" in
	start)
		echo -n "Starting Sockso "
		/usr/bin/sockso.sh --nogui > /dev/null 2>&1 &
		sleep 1
		PID=`javaps -noflags | grep sockso | awk '{print $1}'`
		rc_status -v
		echo $PID > $PIDFILE
		;;
	stop)
		if test -e $PIDFILE ; then
			echo -n "Shutting down Sockso "
			kill `cat $PIDFILE`
			rm -f $PIDFILE
			rc_status -v
		else
			echo "Not running"
		fi
		;;
	reload|restart)
		$0 stop
		sleep 1
		$0 start
		;;
	status)
		echo -n "Checking for Sockso "
		if test -e $PIDFILE ; then
			echo " seems running"
		else
			echo " not running"
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
		;;
esac
rc_exit
