#! /bin/sh
# Copyright (c) 2002-2005 Aconus.com, Tokyo.
# All rights reserved.
#
# Author: Oyaji <oyaji@mail.aconus.com>
#
# /etc/init.d/swatch
#
### BEGIN INIT INFO
# Provides:       swatch
# Required-Start: $syslog
# Required-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:
# Description:    Start Simple WATCHer is an automated monitoring tool (Swatch).
### END INIT INFO

# First reset status of this service
. /etc/rc.status
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

CHECK_LOG="/var/log/messages"
SWATCH_CONF="/etc/swatch/swatchrc"

SWATCH_BIN="/usr/bin/swatch"
SWATCH_LOG="/var/log/swatch"
SWATCH_SCRIPT="/var/run/swatch"

# Check that swatch exists.
if [ ! -x $SWATCH_BIN ]; then
     echo -n "Swatch, $SWATCH_BIN not installed!"
     rc_status -s
     exit 5
fi

# Check that swatch configuration file exists.
if [ ! -f $SWATCH_CONF ]; then
     echo -n "Swatch configuration file, $SWATCH_CONF does not exist."
     # Tell the user this has skipped
     rc_status -s
     exit 6
fi

pid_check() {
   SWATCH_PID=`pidofproc swatch`
}

case "$1" in
   start)
     pid_check
     if [ "$SWATCH_PID" != "" ]; then
           echo -n "Warning: Swatch System already running!"
           rc_status -s
           exit 7
     else
           echo -n "Swatch System startup: "
           $SWATCH_BIN -c $SWATCH_CONF -t $CHECK_LOG --script-dir=$SWATCH_SCRIPT >> $SWATCH_LOG &
           rc_status -v
     fi
     ;;
   stop)
     pid_check
     if [ "$SWATCH_PID" = "" ]; then
           echo -n "Warning: Swatch System not running! "
           rc_status -s
           exit 7
     else
           echo -n "Swatch System shutdown: "
           killproc tail
           rc_status -v
     fi
     ;;
   restart)
     $0 stop
     $0 start
     rc_status
     ;;
   *)
     echo "Usage: $0 {start|stop|restart}"
     exit 1
     ;;
esac
rc_exit