#!/bin/sh

#
# Start/stop T.Rex daemons
#

PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

getpid()
{
  if [ "$1" = "webgate" ]; then
    pid="`cat /etc/firewall/webgate.pid 2>/dev/null`"
    if [ ! -z "$pid" ]; then
      if [ -z "`ps -p $pid | grep webgate`" ]; then
        pid=""
      fi
    fi
  if [ "$1" = "aproxy" ]; then
    pid="`cat /etc/firewall/aproxy.pid 2>/dev/null`"
    if [ ! -z "$pid" ]; then
      if [ -z "`ps -p $pid | grep aproxy`" ]; then
        pid=""
      fi
    fi
  elif [ "$1" = "httpd" ]; then
    pid="`cat /etc/apache/httpd.pid 2>/dev/null`"
    if [ ! -z "$pid" ]; then
      if [ -z "`ps -p $pid | grep httpd`" ]; then
        pid=""
      fi
    fi
  elif [ "$1" = "padminsvr" ]; then
    pid="`cat /etc/firewall/padmin.pid 2>/dev/null`"
    if [ ! -z "$pid" ]; then
      if [ -z "`ps -p $pid | grep padminsvr`" ]; then
        pid=""
      fi
    fi
  elif [ "$1" = "sockd" ]; then
    pid="`cat /etc/sockd.pid 2>/dev/null`"
    if [ ! -z "$pid" ]; then
      if [ -z "`ps -p $pid | grep sockd`" ]; then
        pid=""
      fi
    fi
  else
    prog=`echo $1 | cut -c 1-10`
      pid=`ps -e | grep -w $prog | awk '{print $1}'`
  fi
}

if [ "`id | awk '{print $1}'`" != "uid=0(root)" ]; then
  echo "$0 must be run as root."
  exit 1
fi

case "$1" in
'start')
  echo "starting T.Rex daemons..."
  #
  # Start fwmon
  #
  getpid fwmon
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/fwmon ]; then
      echo "starting fwmon."
      /usr/local/etc/fwmon
    else
      echo "/usr/local/etc/fwmon not found."
      exit 1
    fi
  else
    echo "fwmon already started."
  fi
  #
  # Start smwrapd
  #
  getpid smwrapd
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/smwrapd ]; then
      echo "starting smwrapd."
      /usr/local/etc/smwrapd
    else
      echo "/usr/local/etc/smwrapd not found."
    fi
  else
    echo "smwrapd already started."
  fi
  #
  # Start fwpulsed
  #
  getpid fwpulsed
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/fwpulsed ]; then
      grep -v "^[ 	]*#" /etc/firewall/fwpulse.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting fwpulsed."
        /usr/local/etc/fwpulsed
      fi
    else
      echo "/usr/local/etc/fwpulsed not found."
    fi
  else
    echo "fwpulsed already started."
  fi
  #
  # Start fwpulse
  #
  getpid fwpulse
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/fwpulse ]; then
      grep -v "^[ 	]*#" /etc/firewall/fwpulse.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting fwpulse."
        /usr/local/etc/fwpulse
      fi
    else
      echo "/usr/local/etc/fwpulse not found."
    fi
  else
    echo "fwpulse already started."
  fi
  #
  # Start rpcproxy
  #
  getpid rpcproxy
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/rpcproxy ]; then
      grep "^[ 	]*permit[ 	]" /etc/firewall/rpcproxy.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting rpcproxy."
        /usr/local/etc/rpcproxy
      fi
    else
      echo "/usr/local/etc/rpcproxy not found."
    fi
  else
    echo "rpcproxy already started."
  fi
  #
  # Start webgate
  #
  getpid webgate
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/webgate ]; then
      grep "^[ 	]*permit[ 	]" /etc/firewall/webgate.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting webgate."
        /usr/local/etc/webgate
      fi
    else
      echo "/usr/local/etc/webgate not found."
    fi
  else
    echo "webgate already started."
  fi
  #
  # Start aproxy
  #
  getpid aproxy
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/aproxy ]; then
      grep "^[ 	]*permit[ 	]" /etc/firewall/aproxy.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting aproxy."
        /usr/local/etc/aproxy
      fi
    else
      echo "/usr/local/etc/aproxy not found."
    fi
  else
    echo "aproxy already started."
  fi
  #
  # Start sockd
  #
  getpid sockd
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/sockd ]; then
      if [ -f /etc/sockd.conf ]; then
        echo "starting sockd."
        /usr/local/etc/sockd -lD -f /etc/sockd.conf
      else
        echo "/etc/sockd.conf not found."
      fi
    else
      echo "/usr/local/etc/sockd not found."
    fi
  else
    echo "sockd already started."
  fi
  #
  # Start httpd
  #
  getpid httpd
  if [ -z "$pid" ]; then
    if [ -f /usr/local/etc/httpd ]; then
      if [ -f /etc/apache/httpd.conf ]; then
        echo "starting httpd."
        /usr/local/etc/httpd
      else
        echo "/etc/apache/httpd.conf not found."
      fi
    else
      echo "/usr/local/etc/httpd not found."
    fi
  else
    echo "httpd already started."
  fi
  #
  # Start padmin
  #
  getpid padminsvr
  if [ -z "$pid" ];then
    if [ -f /usr/local/etc/padminsvr ]; then
      grep "^[  ]*permit[       ]" /etc/firewall/padmin.conf >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "starting padmin."
        /usr/local/etc/padminsvr
      fi
    else
      echo "/usr/local/etc/padminsvr not found."
    fi
  else
    echo "padmin already started."
  fi
  ;;

'stop')
  echo "stopping T.Rex daemons..."
  getpid fwpulse
  if [ -n "$pid" ]; then
    echo "stopping fwpulse."
    kill $pid
  fi
  getpid rpcproxy
  if [ -n "$pid" ]; then
    echo "stopping rpcproxy."
    kill $pid
  fi
  getpid smwrapd
  if [ -n "$pid" ]; then
    echo "stopping smwrapd."
    kill $pid
  fi
  getpid fwmon
  if [ -n "$pid" ]; then
    echo "stopping fwmon."
    kill $pid
  else
    echo "fwmon not running."
  fi
  getpid webgate
  if [ -n "$pid" ]; then
    echo "stopping webgate."
    kill $pid
  fi
  getpid aproxy
  if [ -n "$pid" ]; then
    echo "stopping aproxy."
    kill $pid
  fi
  getpid sockd
  if [ -n "$pid" ]; then
    echo "stopping sockd."
    kill $pid
  fi
  getpid httpd
  if [ -n "$pid" ]; then
    echo "stopping httpd."
    kill $pid
  fi
  getpid padminsvr
  if [ -n "$pid" ]; then
    echo "stopping padmin."
    kill $pid
  fi
  ;;

*)
  echo "Usage: /etc/rc.d/init.d/TREX { start | stop }"
  ;;

esac
exit 0
