#!/bin/bash
#
# chkconfig: 2345 25 90
#
# description: Shoreline Firewall (6) is a high-level tool for configuring Netfilter.

# Source function library
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Detect our basename
TMP_NAME=$0
TMP_PWD=$PWD
while [ -L $TMP_NAME ]; do
  if [ ${TMP_NAME/\/} != $TMP_NAME ]; then
    cd ${TMP_NAME%/*}
  fi
  TMP_NAME=$(readlink ${TMP_NAME##*/})
done
cd $TMP_PWD
declare -r BASENAME=${TMP_NAME##*/}
unset TMP_NAME
unset TMP_PWD

# Source service configuration.
if [ -f /etc/sysconfig/${BASENAME} ]; then
  . /etc/sysconfig/${BASENAME}
else
  echo "${BASENAME}: configfile /etc/sysconfig/${BASENAME} does NOT exist!"
  exit 6
fi

if [ -n "$RESTARTOPTIONS" ]; then
  echo "${BASENAME}: usage of \$RESTARTOPTIONS is not supported!"
  exit 6
fi

start() {
  echo -n $"Starting ${BASENAME}: "
  daemon $BASENAME $OPTIONS start $STARTOPTIONS > /dev/null
  RETVAL=$?
  [ $RETVAL -eq 0 ] && echo_success || echo_failure
  echo
  return $RETVAL
}

stop() {
  echo -n $"Shutting down ${BASENAME}: "
  daemon $BASENAME $OPTIONS stop $STOPOPTIONS > /dev/null
  RETVAL=$?
  [ $RETVAL -eq 0 ] && echo_success || echo_failure
  echo
  return $RETVAL
}

restart() {
  stop
  start
}

reload() {
  echo -n $"Reloading ${BASENAME}: "
  daemon $BASENAME $OPTIONS reload $RELOADOPTIONS > /dev/null
  RETVAL=$?
  [ $RETVAL -eq 0 ] && echo_success || echo_failure
  echo
  return $RETVAL
}

condrestart() {
  [ -f /var/lock/subsys/${BASENAME} ] && restart || :
}

clear() {
  echo -n $"Clearing ${BASENAME}: "
  daemon $BASENAME $OPTIONS clear > /dev/null
  RETVAL=$?
  [ $RETVAL -eq 0 ] && echo_success || echo_failure
  echo
  return $RETVAL
}

svcstatus() {
  $BASENAME $OPTIONS status
  RETVAL=$?
  return $RETVAL
}

RETVAL=0

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  clear)
    clear
    ;;
  condrestart)
    condrestart
    ;;
  status)
    svcstatus
    ;;
  *)
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|clear|status}"
    RETVAL=1
esac

exit $RETVAL
