#! /bin/sh -
#
#    initialization/shutdown script for @install_name@ package

prefix="@prefix@"
exec_prefix="@exec_prefix@"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=@sbindir@/@install_name@
NAME=@install_name@
pidfile=@mylockfile@

test -f $DAEMON || exit 0

safe_pidof () {
    pidval=0
    if [ -f $pidfile ] ; then
       pidval=`cat $pidfile | awk '{ print $1 }'`
       echo "${pidval}" | grep ['[^0123456789]'] >/dev/null 2>&1 && pidval=0
    fi
    echo $pidval
    if [ x"$pidval" = x0 ] ; then
	return 1
    else
	return 0
    fi
}

log_stat_msg () {
case "$1" in
	0)
	echo "Service $NAME: Running";
	break;
	;;
	1)
	echo "Service $NAME: Stopped and @mylockfile@ pid file exists";
	break;
	;;
	3)
	echo "Service $NAME: Stopped";
	break;
	;;
	*)
	echo "Service $NAME: Status unknown";
	break;
	;;
esac
}

case "$1" in
  start)
	#
	# Remove a stale lockfile, if found
	#
	if test -f @mylockfile@; then
	    rm -f @mylockfile@
	fi
        /bin/rm -f @mylockdir@/${NAME}.sock
        $DAEMON -D && echo -n " $NAME"
        ;;
  stop)
        PID=`safe_pidof`
	if test "x$PID" != x0
	then
		kill $PID && echo -n " $NAME"
        fi
        ;;
  reload|force-reload)
        PID=`safe_pidof`
        if test "x$PID" != x0
        then
		echo " Reloading $NAME configuration file."
                kill -s HUP $PID && echo -n " $NAME"
	else
		echo " Cannot determine PID of process $NAME."
		exit 1
        fi
        ;;
  restart)
	$0 stop
        sleep 3
        $0 start
        ;;
    status)
        $DAEMON  status
	ERRNUM=$?
	log_stat_msg ${ERRNUM}
	exit ${ERRNUM}
        ;;
  *)
        echo " Usage: $0 {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac

