#!/bin/sh
#
# config.rc sample with defaults :
# service haproxy
#   config /etc/haproxy/haproxy.cfg
#   maxconn 1024
#
config="/etc/haproxy/haproxy.cfg"
maxconn=1024

bin=/usr/sbin/haproxy
cmdline='$bin -D'

. $ROOT/sbin/init.d/default 

if [ -e "$config" ]; then
   maintfd=`grep '^\([^#]*\)\(listen\|server\)' $config|wc -l`
else
   maintfd=0
fi

maxfd=$[$maxconn*2 + $maintfd]
if [ $maxfd -lt 100 ]; then
   maxfd=100;
fi
cmdline="$cmdline -n $maxconn -f $config"
ulimit -n $maxfd

# to get a core when needed, uncomment the following :
# cd /var/tmp
# ulimit -c unlimited

# soft stop
function do_stop {
   pids=`pidof -o $$ -- $PNAME`
   if [ ! -z "$pids" ]; then
      echo "Asking $PNAME to terminate gracefully..."
      kill -USR1 $pids
      echo "(use kill $pids to stop immediately)."
   fi
}

# dump status
function do_status {
   pids=`pidof -o $$ -- $PNAME`
   if [ ! -z "$pids" ]; then
      echo "Dumping $PNAME status in logs."
      kill -HUP $pids
   else
      echo "Process $PNAME is not running."
   fi
}

main $*

