#!/bin/sh
#
# chkconfig: 2345 87 13
# description: ARC HED service
# processname: arched

### BEGIN INIT INFO
# Provides:          arched
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ARC HED service
# Description:       ARC's Hosting Environment Deamon
### END INIT INFO

# Get functions
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    success() {
	log_success_msg "$@"
    } 
    failure() {
	log_failure_msg "$@"
    }
    daemon() {
	start_daemon "$@"
    }
    status() {
	pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
	if test "x$pid" != "x"; then
	    echo "$1 (pid $pid) is running..."
	    return 0
	fi
	if test -f "$PID_FILE"; then
	    read pid < "$PID_FILE"
	    if test "$pid" != ""; then
		echo "$1 stopped but pid file exists"
		return 1
	    fi
	fi
	if test -f $LOCKFILE; then
	    echo "$1 stopped but lockfile exist"
	    return 2
	fi
	echo "$1 is stopped"
	return 3
    }
else
    echo Found neither RedHat nor LSB initscript functions
    exit 1
fi

RUN=yes

if [ -f /etc/sysconfig/arched ]; then
    . /etc/sysconfig/arched
fi
if [ -f /etc/default/arched ]; then
    . /etc/default/arched
fi

if [ `id -u` = 0 ] ; then
    # Debian does not have /var/lock/subsys
    if test -d /var/lock/subsys; then
	LOCKFILE=/var/lock/subsys/arched
    else
	LOCKFILE=/var/lock/arched
    fi
    PID_FILE=/var/run/arched.pid
else
    LOCKFILE=$HOME/arched.lock
    PID_FILE=$HOME/arched.pid
fi

RETVAL=0

start() {
	if [ "$RUN" != "yes" ] ; then
	    echo $"arched disabled in configuration"
	    return 0
	fi
        # Check if it is already running
        if [ ! -f $LOCKFILE ]; then
 	    echo -n $"Starting ARC HED service: "
	    daemon arched
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch $LOCKFILE
            echo
        fi
        return $RETVAL
}	

stop() {
	echo -n $"Stopping ARC HED service: "
	killproc arched
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	return $RETVAL
}

restart() {
	stop
	start
}	

# See how we were called.
case "$1" in
 start)
	start
	;;
 stop)
	stop
	;;
 status)
	status arched
	;;
 restart | force-reload)
	restart
	;;
 reload)
	;;
 condrestart | try-restart)
	[ -f $LOCKFILE ] && restart || :
	;;
 *)
	echo $"Usage: $0 {start|stop|status|restart|force-reload|reload|condrestart|try-restart}"
	exit 1
	;;
esac

exit $?
