#!/bin/sh
#
# chkconfig: - 85 15
# description: Pound is a reverse proxy, load balancer and HTTPS front-end
# processname: pound
# config: /etc/pound/pound.cfg
# pidfile: /var/run/pound.pid

# Script Author: Simon Matter <simon.matter@invoca.ch>
# Version: 2007012300

# 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

# This is our service name
BASENAME=$(basename $0)
if [ -L $0 ]; then
  BASENAME=$(find $0 -name $BASENAME -printf %l)
  BASENAME=$(basename $BASENAME)
fi

RETVAL=0

start() {
  /usr/bin/perl -p -i -e "s/^main_sslacc_status\s+1/main_sslacc_status 0/" /opt/nec/roma/etc/roma.conf
  echo -n $"Starting $BASENAME: "
  chmod +x /etc/${BASENAME}/mkpoundcfg.sh
  /etc/${BASENAME}/mkpoundcfg.sh > "/etc/${BASENAME}/${BASENAME}.cfg"
  sleep 1
  CFG_FILE="/etc/${BASENAME}/${BASENAME}.cfg"
  [ -f $CFG_FILE ] || exit 1
  POUNDHOSTPORT=`grep -E "main_sslacc_host_port[[:blank:]]+[0-9.]+[[:blank:]]+[0-9]+[[:blank:]]+(1[[:blank:]]+){3}([01][[:blank:]]+){3}[0-9.]+:[0-9]+" /opt/nec/roma/etc/roma.conf | head -n 1 | awk '{printf("%s %s",$2,$3)}'`
  if [ "$POUNDHOSTPORT" == "" ];then
    exit 1
  fi
  eval ROOTJAIL=$(grep -si "^RootJail" $CFG_FILE | cut -d " " -f 2)
  [ "$ROOTJAIL" = "/" ] && ROOTJAIL=""
  /usr/sbin/${BASENAME} -c > /dev/null 2>&1
  RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    if [ -n "$ROOTJAIL" ]; then
      [ -d ${ROOTJAIL}/lib ] || mkdir -p ${ROOTJAIL}/lib
      cp -af /lib/libgcc* ${ROOTJAIL}/lib/ 2> /dev/null
    fi
    daemon /usr/sbin/${BASENAME}
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      /bin/rm -f /tmp/crontab.chk 2> /dev/null
      if [ -s /etc/crontab ]; then
          grep ssl-health-check-sh /etc/crontab > /tmp/crontab.chk
      fi
      if ( ! [ -s /tmp/crontab.chk ] ) ;then
              echo "#pound health check every 1 minutes." >> /etc/crontab
              echo "* * * * * root /bin/sh /etc/pound/ssl-health-check-sh 2> /dev/null" >> /etc/crontab
              /bin/kill -HUP `/sbin/pidof crond`
      fi
      if [ -s /etc/logrotate.d/syslog ]; then
          GREPPOUND=`grep -E /usr/bin/killall[[:blank:]]+-HUP[[:blank:]]+pound /etc/logrotate.d/syslog`
          if [ "$GREPPOUND" != "" ];then
	      /usr/bin/perl -p -i -e "s/\/usr\/bin\/killall\s+-HUP\s+pound/\/bin\/kill -HUP \`cat \/var\/run\/syslogd.pid 2>\/dev\/null\` 2>\/dev\/null || true/g" /etc/logrotate.d/syslog
              /bin/kill -HUP `/sbin/pidof syslogd`
          fi
      fi
      /bin/rm -f /etc/pound/ssl_health_check.conf 2> /dev/null
      if [ "$POUNDHOSTPORT" != "" ];then
          echo "$POUNDHOSTPORT" > /etc/pound/ssl_health_check.conf
      fi
    fi
  else
    echo -n $"config error in $CFG_FILE"
    failure $"checking $BASENAME config file $CFG_FILE"
  fi
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${BASENAME}
  return $RETVAL
}

stop() {
  echo -n $"Shutting down $BASENAME: "
  killproc $BASENAME
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/${BASENAME}
  /bin/rm -f /etc/pound/ssl_health_check.conf 2> /dev/null
  return $RETVAL
}

restart() {
  stop
  start
}

rhstatus() {
  status $BASENAME
}

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

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