#!/bin/sh
# description: UTM Billing System DHCP Safe start script

pid_file=/var/run/utm5_dhcpdv6.pid
safe_pid_file=/var/run/safe_utm5_dhcpdv6.pid
utm_exec=utm5_dhcpd6
err_log=/netup/utm5/log/dhcpv6_main.log
exec_dir=/netup/utm5/bin
dhcp_flags=""
cfg_file=/netup/utm5/dhcpdv6.cfg

case "$1" in
start)
        if [ -x $exec_dir/$utm_exec ]; then
            echo "Starting utm5_dhcpd6"
                        if test -f $pid_file
                        then
                                PID=`cat $pid_file`
                                if /bin/kill -9 $PID > /dev/null 2> /dev/null
                                then
                                        if /bin/ps p $PID | grep $utm_exec > /dev/null
                                        then    # The pid contains a utm5_dhcpd6 process
                                        echo "A utm5_dhcpd6 process already exists"
                                        echo "A utm5_dhcpd6 process already exists at " `date` >> $err_log
                                        exit 1
                                        fi
                                fi
                                rm -f $pid_file
                                if test -f $pid_file
                                then
                                        echo "Fatal error: Can't remove the pid file: $pid_file"
                                        echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
                                        echo "Please remove it manually and start $0 again"
                                        echo "utm5_dhcpd6 not started"
                                        exit 1
                                fi
                        fi
                    touch $pid_file >>$err_log 2>&1
                    touch $safe_pid_file && echo $$ > $safe_pid_file

                    while true; do
                        if ! kill -0 "${PID}" 2>/dev/null; then
                            $exec_dir/$utm_exec $dhcp_flags -c $cfg_file 2>&1 &
                            echo $! > ${pid_file}
                            PID=$!
                            sleep 5
                        fi
                        wait
                    done
        fi
        ;;
stop)
        echo "Stopping safe_utm5_dhcpd6"
        if  [ -r $safe_pid_file ]
        then
            SAFE_PID=`cat $safe_pid_file`
            kill -9 $SAFE_PID
            echo "safe_utm5_dhcpd6 stopped"
            rm -rf $safe_pid_file
        fi

        if [ -r $pid_file ]
        then
          echo "Stopping utm5_dhcpd6"
          dhcpdv6_pid=`cat $pid_file` 2>&1
                  rm -f $pid_file
                  kill -2 $dhcpdv6_pid
                  echo "waiting for 3 sec..."
                  sleep 3
                  if [ -r ${dhcpdv6_pid} ] && kill -0 ${pid_file} 2>/dev/null
                  then
                      echo "Kill process"
                      kill -9 $dhcpdv6_pid 2>&1
                  fi

        else
                  rm -f $pid_file 2>&1
          killall -9 utm5_dhcpd6 2>&1
        fi
        rm -rf $pid_file
        exit 0
        ;;
*)
        echo "Usage: `basename $0` {start|stop}" >&2
        exit 64
        ;;
esac
