#!/bin/bash
#
# startscript for applejuice-core
# Copyright (c) 2004-2009 oc2pus <oc2pus@arcor.de>
#
# Changelog:
# 2006-02-10: ajcore.jar location changed to /usr/share/java/applejuice

# 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
}

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active        checks whether a service is activated by symlinks
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

PROGNAME=`basename $0`
export LD_LIBRARY_PATH=/usr/lib/$PROGNAME:$LD_LIBRARY_PATH

AJ_DIR=$HOME/appleJuice
if [ ! -d $AJ_DIR ]; then
	mkdir -p $AJ_DIR
fi
AJ_LOG=$AJ_DIR/$PROGNAME.log
AJ_CORE=$PROGNAME.jar
AJ_PATH=/usr/share/java/applejuice
PID_FILE=$AJ_DIR/$PROGNAME.pid

# Check for missing binaries
if [ ! -x /usr/bin/$PROGNAME ] ; then
	echo -n "program, /usr/bin/$PROGNAME not found! "
	rc_status -s
	exit 5
fi

case "$1" in
	'start')
		if [ -f $PID_FILE ]; then
			echo -n "$AJ_PATH/$AJ_CORE already running";
		else
			echo -n "Starting core $AJ_PATH/$AJ_CORE"
			java -jar $AJ_PATH/$AJ_CORE > $AJ_LOG 2>&1 &
			
			PID=`javaps -noflags | grep $PROGNAME | awk '{print $1}'`
			echo $PID > $PID_FILE
		fi
		rc_status -v
		;;
	
	'stop')
		if [ -f $PID_FILE ]; then
			echo -n "Stopping core $AJ_PATH/$AJ_CORE"
			kill `cat $PID_FILE` 2>/dev/null
	        rm $PID_FILE 2> /dev/null
		else
			echo -n "$AJ_PATH/$AJ_CORE not running"
		fi
		rc_status -v
		;;
	
	'restart')
		$0 stop
		$0 start
		rc_status
		;;
	
	'status')
    	    echo "Checking for $AJ_PATH/$AJ_CORE ..."
			javaps | grep $PROGNAME
        	rc_status -v1
        	;;
			
	*)
		echo "Usage: $0 { start | stop | restart | status }"
		exit 1
		;;
esac
rc_exit
