#!/bin/bash
#
# Rsync Service Script
#
### BEGIN INIT INFO
# X-UnitedLinux-Should-Start: $network
# Provides: qsync
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Simple rsync service
### END INIT INFO

VCSBIN="/opt/VRTSvcs/bin/"
HA_CURRENT_SYSTEM=`echo $(hostname -f) | awk -F'.' '{print $1}'`
HA_ALT_SYSTEM=(`$VCSBIN/hastatus -sum | grep "A  " | awk '{ print $2 }' | grep -v $HA_CURRENT_SYSTEM`)
SLEEPINT=5
SERVICE=`echo $0 | awk -F"/" '{print $NF}'`
CMD_START="/opt/tandbergtv/cms/scripts/veritas/rsyncloop.sh $HA_CURRENT_SYSTEM $HA_ALT_SYSTEM $SLEEPINT &"
STATUSMSG="   Updates are being sent from $HA_CURRENT_SYSTEM to $HA_ALT_SYSTEM in $SLEEPINT seconds intervals"

############ VCS Check Logic [START] ############
#VCSBIN="/opt/VRTSvcs/bin/"
HA_CURRENT_SYSTEM=`hostname -s`
#SERVICE=`echo $0 | awk -F"/" '{print $NF}'`
sleep 3 # Any of the resources need to be up and running before the command loop will work correctly.
if [[ $2 == "--VCSOverride" ]] || [[ -z `/etc/init.d/vcs status | grep running...` ]] ; then
############# VCS Check Logic [END] #############
#
########## Original Case Logic [START] ##########

    case "$1" in
    start)
        pid=$(ps axww | grep "rsyncloop.sh"| grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [ ! -z "${pid}" ] ; then
            echo "$SERVICE is already running..."
            echo "$STATUSMSG"
        else
            echo "Starting $SERVICE..."
            echo "$STATUSMSG"
            sleep $SLEEPINT # delay the start up for a few seconds for the service to startup.  
            $CMD_START > /dev/null 2>&1 &
        fi
        echo `ps axww | grep "rsyncloop.sh"| grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1` > /etc/init.d/$SERVICE.pid
        ;;
    stop)
        pid=$(ps axww | grep "rsyncloop.sh" | grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [ ! -z "${pid}" ] ; then
            echo -e "Stopping $SERVICE"
            kill -9 $pid # PURE MASSACRE
        fi
        ;;
    status)
        pid=$(ps axww | grep "rsyncloop.sh" | grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [ -z "${pid}" ] ; then
            echo "$SERVICE is stopped"
        else
        echo "$SERVICE (pid "$pid") is running..."
        echo "$STATUSMSG"
        fi
        ;;
    restart)
        $0 stop
        sleep 5
        $0 start
        ;;
    *)
        echo "usage: $SERVICE (start|stop|status|restart)"
    esac

########### Original Case Logic [END] ###########
#
############# VCS Case Logic [START] ############
else
    case "$1" in
        start)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -online $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        stop)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -offline -ignoreparent $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        status)
            service $SERVICE status --VCSOverride
            ;;
        restart)
            echo "VCS is currently managing the $SERVICE service and restart is not available, please stop and start the service manually."
    esac
fi
############## VCS Case Logic [END] #############
