#!/bin/sh
#
# chkconfig: - 80 20
# description: Starts and stops RoyaltyShare local job scheduler
#

prog='/app/tools/job/bin/job_scheduler.pl';
NAME="RoyaltyShare Job Scheduler"

#export PERL5LIB=/usr/local/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi:/usr/local/lib/perl5/site_perl:/usr/local/lib64/perl5/5.8.8/x86_64-linux-thread-multi:/usr/local/lib/perl5/5.8.8

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

RETVAL=0

start() {
    echo -n $"Starting $NAME services: "
    if [ ! -f "/app/shared/I am shared" ] ; then
        echo "Attempting to mount /app/shared... "
        mount -a
    fi
    if [ ! -f "/app/shared/I am shared" ] ; then
        echo -n "/app/shared not mounted "
        failure
    else
        $prog
        RETVAL=$?
        if [ "$RETVAL" = 0 ]; then
            success
        else 
            failure
        fi
    fi
    echo
    return $RETVAL
}	

stop() {
	echo -n $"Shutting down $NAME services: "
	$prog -k
    RETVAL=$?
    if [ "$RETVAL" = 0 ]; then
        success
    else 
        failure
    fi
    echo
	return $RETVAL
}	

restart() {
	stop
	start
}	

status() {
    $prog -t
    RETVAL=$?
    if [ "$RETVAL" = 0 ]; then
        echo $"$NAME is running..."
    else
        echo $"$NAME is not running..."
    fi
    return $RETVAL
}


case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  status)
        status
        ;;
  *)
	echo $"Usage: `basename $0` {start|stop|restart|status}"
	exit 2
esac

exit $?
