#!/bin/sh
#
# start/stop osstech-watchdir script.
#
# chkconfig: 2345 99 50
# description: osstech-watchdir script is monitoring a specified directory.
# processname: osstech-watchdir
# config: none
# pidfile: /var/run/osstech-watchdir.pid

progname="osstech-watch_csvuploads"
progcmd="/opt/osstech/sbin/${progname}.sh"

test -f $progcmd || exit 0
test -d /opt/osstech/var/nda/csvuploads || exit 0
test -d /usr/local/nda/densan/if || exit 0

PIDFILE="/var/run/osstech-csvuploads.pid"
STARTSTOPD="/opt/osstech/sbin/start-stop-daemon"

. /etc/rc.d/init.d/functions

# Source watch target
[ -f /etc/sysconfig/osstech-watch ] && . /etc/sysconfig/osstech-watch

start() {
	echo -n $"Starting $progname: "
	if [ -e /var/lock/subsys/$progname ]; then
	   if [ -e "$PIDFILE" ] && [ -e /proc/`cat $PIDFILE` ]; then
	      echo -n $"cannot start $progname: $progname already running.";
	      failure $"cannot start $progname: $progname already running.";
	      echo	   
	      return 1	
	    fi
	fi	 

        $STARTSTOPD --start --background --make-pidfile --pidfile "$PIDFILE" --exec "$progcmd" "$TARGET"
	RETVAL=$?
	[ $RETVAL -eq 0 ] && (success ; touch /var/lock/subsys/$progname) || failure;
	echo 
	return $RETVAL
}	

stop() {
       echo -n $"Stopping $progname: "
       if [ ! -e /var/lock/subsys/$progname ]; then
      	   echo -n $"cannot stop $progname: $progname is not running."
	   failure $"cannot stop $progname: $progname is not running."
	   echo
	   return 1;
       fi
       if [ -e "$PIDFILE" ]; then
	   pid=`cat $PIDFILE`
	   kill -TERM -- -"$pid"
	   RETVAL=$?
       fi
       [ "$RETVAL" -eq 0 ] && (success; rm -f /var/lock/subsys/$progname) || failure;
       echo 
       return $RETVAL
}

rhstatus() {
	   status $progname
}

restart() {
	  stop
	  start
}

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

