#! /bin/bash
#
# functions:	This file contains functions to be used for autostart.sh
#               in WindowMaker
#    ported from /etc/init.d/functions 23-Dec-1998
#    added checkStart                  24-Dec-1998
#    added path manipulation           11-Jul-2000
#  by Jun Nishii <jun@vinelinux.org>
#                    Time-stamp: <2000-07-11 12:15:23 vine>
#
#  (original source, /etc/init.d/functions, is written by
#    Miquel van Smoorenburg, Greg Galloway and Marc Ewing.

# A function to stop a program.
killproc() {
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi

        # Save basename.
        base=`basename $1`

        # Find pid.
        pid=`pidofproc $base`

        # Kill it.
        if [ "$pid" != "" ] ; then
                echo -n "$base "
		if [ "$notset" = 1 ] ; then
			# TERM first, then KILL if not dead
			kill -TERM $pid
			usleep 100000
			if ps h $pid >/dev/null 2>&1 ; then
				sleep 3
				kill -KILL $pid
			fi
		# use specified level only
		else
	                kill $killlevel $pid
		fi
	fi

        # Remove pid file if any.
        rm -f /var/run/$base.pid
}

# A function to stop a user's program.
killmyproc() {
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killmyproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi

        # Save basename.
        base=`basename $1`

        # Find pid.
        pid=`pidofmyproc $base`

        # Kill it.
        if [ "$pid" != "" ] ; then
                echo "killing $base ..."
		if [ "$notset" = 1 ] ; then
			# TERM first, then KILL if not dead
			kill -TERM $pid
			usleep 100000
			if ps h $pid >/dev/null 2>&1 ; then
				sleep 3
				kill -KILL $pid
			fi
		# use specified level only
		else
	                kill $killlevel $pid
		fi
	fi
}

# A function to find the pid of a program.
pidofproc() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: pidofproc {program}"
		return 1
	fi

	# First try "/var/run/*.pid" files
	if [ -f /var/run/$1.pid ] ; then
	        pid=`head -1 /var/run/$1.pid`
	        if [ "$pid" != "" ] ; then
	                echo $pid
	                return 0
	        fi
	fi

	# Next try "pidof"
	pid=`/sbin/pidof $1`
	if [ "$pid" != "" ] ; then
	        echo $pid
	        return 0
	fi

	# Finally try to extract it from ps
	ps auxw | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
			   { if ((prog == $11) || (("(" prog ")") == $11) ||
			   ((prog ":") == $11)) { print $2 ; exit 0 } }' $1
}


pidofmyproc() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: ownofproc {program}"
		return 1
	fi

	# Next try "pidof"
	pid=`pidofproc $1`
	if [ "$pid" != "" ] ; then
	        for i in $pid; do
		    user=`ps h -o user --pid $i`
		    if [ "${user}" = "${USER}" ]; then
			echo -n $i" "
		    fi
		done
		echo
	        return 0
	fi
}


checkStart(){
    pid=`pidofproc $1`
    if [ -z "$pid" ] ; then
	$@ >& /dev/null &
    fi
}

isXRunning(){
    pid=`pidofproc X`

    if [ -z "$pid" ] ; then
	# for Xpmac server
	pid=`pidofproc Xpmac.bin`

	# for sparc
	if [ -z "$pid" ] ; then
		pid=`pidofproc Xsun`
	fi
	if [ -z "$pid" ] ; then
		pid=`pidofproc Xsun24`
	fi

	if [ -z "$pid" ] ; then
	    return -1
	else
	    return 0
	fi
    fi
    return 0
}

