#!/bin/bash
#
# ummon-server  Startup script for ummon-server for Red Hat 6.
#
# chkconfig: 2345 80 30
# description: Ummon-server manages and monitors tasks.
### BEGIN INIT INFO
# Provides: ummon-server
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Task manager
# Description: Ummon-server manages and monitors tasks.
### END INIT INFO

# Source function library.
. /etc/init.d/functions

USER=ummon
PIDDIR=/var/run/ummon
PIDFILE="$PIDDIR/ummon.pid"
CONFDIR=/etc/ummon
CONFIG="$CONFDIR/config.json"
DATADIR=/var/lib/ummon
LOGDIR=/var/log/ummon

retval=0
prog=ummon
exec=/usr/bin/ummon-server
lockfile=/var/lock/subsys/$prog

# Make sure those directories are created and owned
mkdir -p "$CONFDIR" "$PIDDIR" "$DATADIR" "$LOGDIR"
chown $USER "$CONFDIR" "$PIDDIR" "$DATADIR" "$LOGDIR"

start() {
        [ -x $exec ] || exit 5

        umask 077

        echo -n $"Starting $prog: "
        daemon --pidfile="$PIDFILE" --user="$USER" $exec --pidfile="$PIDFILE" --config="$CONFIG" --daemon
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
}
stop() {
        echo -n $"Shutting down $prog: "
        killproc -p "$PIDFILE" $exec
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
}
rhstatus() {
        status -p "$PIDFILE" -l $prog $exec
}
restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        exit 3
        ;;
  force-reload)
        restart
        ;;
  status)
        rhstatus
        ;;
  condrestart|try-restart)
        rhstatus >/dev/null 2>&1 || exit 0
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status}"
        exit 3
esac

exit $?
