#!/bin/sh
# description: UTM Billing System Radius Safe start script
trap '' 1 2 3 15

pid_file=/var/run/utm5_radius.pid
cfg_file=/netup/utm5/radius5.cfg
utm_exec=utm5_radius
err_log=/netup/utm5/log/radius5.log
exec_dir=/netup/utm5/bin
root_email=root

radius_login=radius
radius_password=radius
core_host=127.0.0.1
core_port=11758

if [ -r $cfg_file ]
then
  . $cfg_file
fi  

#core_ping=$exec_dir/core_ping
#ping_args="-h $core_host -P $core_port -l $radius_login -p $radius_password -i 2 -c 1"



case "$1" in
start)
        if [ -x $exec_dir/$utm_exec ]; then
            echo "Starting utm5_radius"
			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_radius process
					echo "A utm5_radius process already exists"
					echo "A utm5_radius 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_radius not started"
					exit 1
				fi
			fi
            touch $pid_file >>$err_log 2>&1
			while true
			do
			  rm -f $pid_file      
    		  $exec_dir/$utm_exec -c $cfg_file >>$err_log 2>&1 
			  if test ! -f $pid_file
			  then
				break
			  fi
				echo "utm5_radius process exited ... Check it" | mail $root_email
				echo "utm5_radius process exited ... mail to admin sent. Sleeping 10 sec!"
				sleep 10
		    done
        fi
        ;;
stop)
        echo "Stopping utm5_radius"
        if [ -r $pid_file ]
        then
          radius_pid=`cat $pid_file` >>$err_log 2>&1
		  rm -f $pid_file
          kill -9 $radius_pid >>$err_log 2>&1
        else  
		  rm -f $pid_file >>$err_log 2>&1
          killall -9 utm5_radius >>$err_log 2>&1
        fi
        exit 0
        ;;
*)
        echo "Usage: `basename $0` {start|stop}" >&2
        exit 64
        ;;
esac
