#!/bin/sh
#
#  cntsd  Start Contents Delivering Services
#
# chkconfig: 2345 98 40
# description: Startup Contents Delivering Services
#

HA_DIR=/etc/iplb_contents; export HA_DIR
CONFIG=$HA_DIR/contents.conf

RHFUNCS=/etc/rc.d/init.d/functions
SUBSYS=cntsd

#
#	Places to find killproc or killall in...
#

KILLPROCS="/sbin/killproc /usr/bin/killall"
INSMOD=/sbin/insmod
US=`uname -n`

# Set this to a 1 if you want to automatically load kernel modules
USE_MODULES=0

#
#	Source in Red Hat's function library.
#
if
  [ ! -x $RHFUNCS ]
then
  daemon() {
	$*
  }
  status() {
	/usr/lib/iplb_contents/cntsd -s
  }
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()

#
#	Install /proc/ha module, if it's not already installed
#
#

init_proc_ha() {
  if
    [ ! -d /proc/ha ]
  then
    for module in $PROC_HA
    do
      if
        $INSMOD $module
      then
        : $PROC_HA Module loaded OK
        return 0
      fi
    done
  fi
}


#
#	Start the iplbdaemon daemon...
#

start_iplbdaemon() {
  if
    daemon /usr/lib/iplb_contents/cntsd  # -d
  then
    : OK
  else
    return $?
  fi
}


#
#	Start Linux-HA
#

StartHA() {
  if
    [ $USE_MODULES = 1 ]
  then
    #	Create /dev/watchdog and load module if we should
    init_watchdog
    #	Load /proc/ha module
    #	NOTE: Current version of proc_ha module kills iplbdaemon processes!
    # init_proc_ha
  fi
  rm -f /var/run/ppp.d/*
  if
    [  -f $HA_DIR/ipresources -a ! -f $HA_DIR/haresources ]
  then
    mv $HA_DIR/ipresources $HA_DIR/haresources
  fi
  #	Start iplbdaemon daemon
  if
    start_iplbdaemon
  then
    : OK
  else
    RC=$?
    echo "cntsd did not start [rc=$RC]"
    return $RC
  fi
}

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

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

  if
    /usr/lib/iplb_contents/cntsd  -k
  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 /usr/lib/iplb_contents/cntsd
    else
      echo "No killproc/killall"
      exit 1
    fi
  fi
  lrc=$?
  return $lrc
}


RC=0
# See how we were called.

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

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

  status)
	status cntsd
	RC=$?
	;;

  restart|reload)
	StopHA
	StartHA
	RC=$?
	;;

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

exit $RC

