#!/bin/sh

##############################################################################
#                                                                            #
# Copyright (c) 2011 by Idera All rights reserved                            #
#                                                                            #
##############################################################################
#
# chkconfig: 2345 99 99
# description: Righteous Software Linux Backup Agent
# processname: cdp
# config: /etc/r1soft/agent_config

### BEGIN INIT INFO
# Provides:          cdp-agent
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts R1Soft CDP Agent
# Description:       Starts R1Soft CDP Agent
### END INIT INFO

OPTS="-s"
VARRUN=/var/run
BASE=/usr/sbin/r1soft
ETC=${BASE}/conf
BIN=${BASE}/bin

# cdp control script

# the path to the PID file
if [ -f $ETC/agent_config ]; then
	PIDFILE=`awk -F'=' 'tolower($1) ~ /pidfile/ { print $2 }' $ETC/agent_config`
fi

if [ "x$PIDFILE" = "x" ]; then
	PIDFILE=$VARRUN/cdp.pid
fi

# path to the config file (default: /etc/cdp/agent_config)
CDP_CONFIG=$ETC/agent_config

if [ -f $BASE/.recovery-mode ]; then
	OPTS="$OPTS -r"
fi

# options to cdp
CDP_OPTS="$OPTS -c "

# path to cdp binary (default: /usr/bin/cdp)
CDP=$BIN/cdp

RUN_CDP="${CDP} ${CDP_OPTS} ${CDP_CONFIG}"
umask 077

# DON'T EDIT PAST HERE #######################################################

ERROR=0
STATUSCODE=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    # check for pidfile
    if [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
	    STATUS="cdp (pid $PID) running"
	    RUNNING=1
	    STATUSCODE=0
	else
	    STATUS="cdp (pid $PID?) not running"
	    RUNNING=0
	    STATUSCODE=1
	fi
    else
	STATUS="cdp (no pid file) not running"
	RUNNING=0
	STATUSCODE=3
    fi

    case $ARG in
    start)
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: cdp (pid $PID) already running"
	    continue
	fi
		if [ ! -d /lib/modules/r1soft ]; then
			mkdir -p /lib/modules/r1soft;
		fi

        if cd ${BASE} > /dev/null; then
                if $RUN_CDP > /dev/null ; then
                        echo "$0 $ARG: cdp started"
                else
                        echo "$0 $ARG: cdp could not be started"
                        ERROR=1
                fi
        else
                echo "$0 $ARG: could not change working directory to: ${BASE}"
                echo "$0 $ARG: cdp could not be started"
                ERROR=1
        fi
	;;
    stop)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: $STATUS"
	    continue
	fi
# kill the hcp session
	if kill $PID ; then
		[ -f $PIDFILE ] && /bin/rm -f $PIDFILE
		PSOUT=0
		ECHO=1

		RUN_TIMES=0
		while [ -d "/proc/$PID" ] && [ ${RUN_TIMES} -le 300 ]; do

 			 if [ $ECHO -eq "1" ]; then
				 echo "Waiting for cdp-agent to stop, this can take up to 300 seconds..."
				 ECHO=0
 			 else
 			 	echo -n "."
 			 fi
 			 RUN_TIMES=$(($RUN_TIMES + 1))
 			 sleep 1;
 		done

 		echo ""

 		if [ -d "/proc/$PID" ]; then
			 echo "$0 $ARG: cdp could not be stopped after 60 seconds"
			 exit 1
 		fi

	    echo "$0 $ARG: cdp agent stopped"
	else
	    echo "$0 $ARG: cdp could not be stopped"
	    ERROR=1
	fi
	if [ -f /usr/sbin/r1soft/bin/cdp ] && [ -z "$ERROR" ]; then
		/usr/sbin/r1soft/bin/cdp -u > /dev/null 2>&1
		if [ $? =! "0" ]; then
			echo "Failed to shutdown hcpdriver. Check that all hcp sessions are closed"
			echo "Run hcp --help for more information"
			exit 1
		fi
		sync
	fi
# attempt to remove the driver
 	/sbin/rmmod hcpdriver > /dev/null 2>&1
	if [ -x /usr/sbin/hcp ]; then
		if [ -x /bin/grep ]; then
			if [ -x /sbin/lsmod ]; then
				for x in 1 2 3 4 5 6 7 8 9 10; do
 					/sbin/rmmod hcpdriver > /dev/null 2>&1;
					T="`/sbin/lsmod | /bin/grep hcpdriver`";
					if [ -z "$T" ]; then
						break;
					fi
					sleep 1;
				done
			fi
		fi
	fi
	;;
    restart)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: cdp not running, trying to start"
	    if $RUN_CDP > /dev/null ; then
		echo "$0 $ARG: cdp started"
	    else
		echo "$0 $ARG: cdp could not be started"
		ERROR=1
	    fi
	else
		if [ -f /bin/sh ]; then
			S="/bin/sh"
		elif [ -f /bin/bash ]; then
			S="/bin/bash"
		elif [ -f /bin/dash ]; then
			S="/bin/dash"
		fi
		$S $0 stop
		if [ $? != "0" ]; then
			echo "failed to stop agent"
			exit 1
		fi
		$S $0 start
		if [ $? != "0" ]; then
			echo "failed to stop agent"
			exit 1
		fi
	fi
	;;
    status)
        echo "$0 $ARG: $STATUS"
	exit $STATUSCODE
	    continue

	;;
    *)
	echo "usage: $0 (start|stop|restart|status|help)"
	cat <<EOF

start      - start cdp
stop       - stop cdp
restart    - stop cdp if running then start again,
             if cdp is not running it is started
status     - print status
help       - this screen

EOF
	ERROR=1
    ;;

    esac

done

exit $ERROR

