#!/bin/sh
#
# chkconfig: 2345 50 40
# description: Startup script LoadBalancer Agent services.
#

HA_DIR=/etc/ha4.d; export HA_DIR
CONFIG=$HA_DIR/lbhost4.conf

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

#
#	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() {
	/usr/lib/lbhost4d/lbhost4d -s
  }
else
  . $RHFUNCS
fi

#
#	Start the iplbdaemon daemon...
#

start_iplbdaemon() {

  if
    /sbin/iptables -nL >/dev/null 2>&1
  then
    : OK
  elif
    /sbin/ipchains -nL >/dev/null 2>&1
  then
    : OK
  elif [ -e /opt/nec/tmp/hidden ]
  then
    : OK
  elif [ -e /sbin/ipchains ]
  then
    $INSMOD ipchains >/dev/null 2>&1
    : OK
  else
    echo "Linux Firewall (ipchains or iptables) is require."
    return $?
  fi

  if
    daemon /usr/lib/lbhost4d/lbhost4d $OPT
  then
    : OK
  else
    return $?
  fi
}

#
# Start
#

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

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

  echo "$SUBSYS: shutdown in progress."

  if
    /usr/lib/lbhost4d/lbhost4d -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 /usr/lib/lbhost4d/lbhost4d
    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 LoadBalancer Agent services: "
	StartHA
	RC=$?
	echo
	[ $RC -eq 0 ] && touch /var/lock/subsys/$SUBSYS
	;;

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

  status)
	status lbhost4d
	RC=$?
	;;

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

  debugstart)
	echo -n "Debug Starting LoadBalancer Agent services: "
	OPT="-d"
	StartHA
	RC=$?
	echo
	[ $RC -eq 0 ] && touch /var/lock/subsys/$SUBSYS
	;;

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

exit $RC

