#!/bin/sh
#
#	Multicast Send OCF RA. 
#
# Copyright (c) 2009 Adtec Digital, Inc.
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

declare -r CMD="/usr/local/bin/cmd "

declare -r  MM="${CMD} *.ECMD MM 0 "
declare -r  MSEND="02"
declare -r  MSTOP="00"
declare -ri CMD_OK=0

declare -r  VID_DETECT="${CMD} *.ECMD VD"
declare -r VID_OK="01"

declare -r SIGNAL_CMD="/usr/bin/pkill -USR1 highAvail"

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="MutlicastSend" version="1.0">
<version>1.0</version>

<longdesc lang="en">
This is a Multicast Send agent
</longdesc>
<shortdesc lang="en">Multicast resource agent</shortdesc>

<actions>
<action name="start"        timeout="5" />
<action name="stop"         timeout="5" />
<action name="monitor"      timeout="5" interval="15" depth="0" start-delay="0" />
<action name="meta-data"    timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
END
}

#######################################################################

MutlicastSend_usage() {
	cat <<END
usage: $0 {start|stop|monitor|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

MutlicastSend_start() {
    # first check to see if we have video source
    local DETECT=$($VID_DETECT) 
#    ocf_log info "MPG:$DETECT:"
    if [ "$DETECT" = "$VID_OK" ]; then
        # turn on multicast send
	${MM} ${MSEND} &> /dev/null 
        ${SIGNAL_CMD} &> /dev/null
	if [ $? -eq $CMD_OK ]; then
	    return $OCF_SUCCESS
	else
	    # failed
	    ocf_log err "Failed to start multicast send."
	    return $OCF_ERR_GENERIC
	fi
    else
        ocf_log err "Video Source not detected."
	return $OCF_ERR_GENERIC
    fi
    
}

MutlicastSend_stop() {
    ${MM} ${MSTOP} &> /dev/null 
    if [ $? -eq $CMD_OK ]; then
	return $OCF_SUCCESS
    else
	return $OCF_ERR_GENERIC
    fi
}

MutlicastSend_monitor() {
    local DETECT=$($VID_DETECT)

#    ocf_log info "MPG1:$DETECT:"
    if [ "$DETECT" = "$VID_OK" ]; then
        local RES=$(${MM})
#        ocf_log info "MPG2:$RES:"
        if [ "$RES" = "$MSEND" ]; then
	    return $OCF_SUCCESS
        else
            return $OCF_NOT_RUNNING
        fi
    else
	#return $OCF_ERR_GENERIC
        return $OCF_NOT_RUNNING
    fi
}

MutlicastSend_validate_all() {
	return $OCF_SUCCESS
}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		MutlicastSend_start;;
stop)		MutlicastSend_stop;;
status)		MutlicastSend_monitor;;
monitor)	MutlicastSend_monitor;;
validate-all)	MutlicastSend_validate_all;;
usage|help)	MutlicastSend_usage
		exit $OCF_SUCCESS
		;;
*)		MutlicastSend_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc

