#!/bin/sh
#
#	$Id: CSwatch,v 1.4 2011/04/26 10:57:06 higashi Exp $
#
# CSwatch     Start high-availability services
#
# Author:       Alan Robertson	<alanr@henge.com>
#
#		it should run under SuSE and Debian also...
#
# chkconfig: 2345 91 99
# description: Startup script high-availability services.
# processname: CSwatch

RHFUNCS=/etc/rc.d/init.d/functions
SUBSYS=CSwatch
#
#	Places to find killproc or killall in...
#
KILLPROCS="/sbin/killproc /usr/bin/killall"
INSMOD=/sbin/insmod

#
#	Source in Red Hat's function library.
#
if
  [ ! -x $RHFUNCS ]
then
  daemon() {
	$*
  }
  status() {
	/opt/nec/CSwatch/bin/CSwatch
  }
else
  . $RHFUNCS
fi

#
#	Install the softdog module if we need to
#
init_watchdog() {
#
# 	We need to install it if watchdog is specified in $CONFIG, and
#	/dev/watchdog refers to a softdog device, or it /dev/watchdog
#	doesn't exist at all.
#
#	If we need /dev/watchdog, then we'll make it if necessary.
#
#	Whatever the user says we should use for watchdog device, that's
#	what we'll check for, use and create if necessary.  If they misspell
#	it, or don't put it under /dev, so will we.
#	Hope they do it right :-)
#
#
  insmod=no
  # What do they think /dev/watchdog is named?
  MISCDEV=`grep ' misc$' /proc/devices | cut -c1-4`
  MISCDEV=`echo $MISCDEV`
  WATCHDEV=`grep -v '^#' $CONFIG | grep watchdog |
	sed s'%^[ 	]*watchdog[ 	]*%%'`
  WATCHDEV=`echo $WATCHDEV`
  if
    [ "X$WATCHDEV" != X ]
  then
    : Watchdog requested by $CONFIG file
  #
  #	We try and insmod the module if there's no dev or the dev exists
  #	and points to the softdog major device.
  #
    if
      [ ! -c "$WATCHDEV" ]
    then
      insmod=yes
    else
      case `ls -l "$WATCHDEV" 2>/dev/null` in
      *$MISCDEV,*)
	    insmod=yes;;
      *)	: "$WATCHDEV isn't a softdog device (wrong major)" ;;
      esac
    fi
  else
    : No watchdog device specified in $CONFIG file.
  fi
  case $insmod in
    yes)
      if
        grep softdog /proc/modules >/dev/null 2>&1 
      then
        : softdog already loaded
      else
        $INSMOD softdog >/dev/null 2>&1
      fi;;
  esac
  if
    [ "X$WATCHDEV" != X -a ! -c "$WATCHDEV" -a $insmod = yes ]
  then
    minor=`cat /proc/misc | grep watchdog | cut -c1-4`
    mknod -m 600 $WATCHDEV c $MISCDEV $minor
  fi
} # init_watchdog()

#
#	Start the CSwatch daemon...
#

start_CSwatch() {
  if
    daemon /opt/nec/CSwatch/bin/CSwatch # -d
  then
    : OK
  else
    return $?
  fi
}


#
#	Start Linux-HA
#

StartHA() {
  #	Start CSwatch daemon
  if
    start_CSwatch
  then
    : OK
  else
    RC=$?
    echo "CSwatch did not start [rc=$RC]"
    return $RC
  fi
}

#
#	Ask CSwatch to stop.  It will give up it's resources...
#
StopHA() {

  MGR=$HA_BIN/ResourceManager
  echo "$SUBSYS: shutdown in progress."

  if
    /opt/nec/CSwatch/bin/CSwatch -k	# Kill it
  then
    return 0
  fi

  KILLPROC=""

  #	Use the Red Hat killproc function if it's there...

  if
    [ -x $RHFUNCS ]
  then
    killproc $SUBSYS
  else
    for j in $KILLPROCS
    do
      if
        [ -x $j ]
      then
        KILLPROC=$j;
        break;
      fi
    done
    if
      [ "X$KILLPROC" != X ]
    then
      $KILLPROC /opt/nec/CSwatch/bin/CSwatch
    else
      echo "No killproc/killall"
      exit 1
    fi
  fi
  lrc=$?
  return $lrc
}


RC=0
# See how we were called.

case "$1" in
  start)
	echo -n "Starting ROMA Watching services: "
	StartHA
	RC=$?
	echo
	[ $RC -eq 0 ] && touch /var/lock/subsys/$SUBSYS
	;;

  stop)
	echo -n "Stopping ROMA Watching services: "
	StopHA
	RC=$?
	echo
	[ $RC -eq 0 ] && rm -f /var/lock/subsys/$SUBSYS
	;;

  restart|reload)
	touch /var/log/roma/redundant_chk_flg
	StopHA
	StartHA
	RC=$?
	sleep 4
	rm -rf /var/log/roma/redundant_chk_flg
	;;

  *)
	echo "Usage: CSwatch {start|stop|restart|reload}"
	exit 1
esac

exit $RC

