#!/bin/sh
#
#
# Description:	wrapper of OCF RA IPaddr2, based on original heartbeat RA.
#		See OCF RA IPaddr2 for more information.
#
# Author:	Xun Sun <xunsun@cn.ibm.com>
# Support:      linux-ha@lists.linux-ha.org
# License:      GNU General Public License (GPL)
# Copyright:	(C) 2005 International Business Machines
#
#	This script manages IP alias IP addresses
#
#	It can add an IP alias, or remove one.
#
#	usage: $0 ip-address[/netmaskbits[/interface[:label][/broadcast]]] \
#	    {start|stop|status|monitor}
#
#	The "start" arg adds an IP alias.
#
#	Surprisingly, the "stop" arg removes one.	:-)
#
unset LANG; export LANG
LC_ALL=C
export LC_ALL

. @HB_RA_DIR@/hto-mapfuncs

# We need to split the argument into pieces that IPaddr OCF RA can
# recognize, sed is prefered over Bash specific builtin functions 
# for portability.

usage() {
  echo "usage: $0 ip-address[/netmaskbits[/interface[:label][/broadcast]]] $LEGAL_ACTIONS"
}

if [ $# != 2 ]; then
    usage
    exit 1
fi

BASEIP=`echo $1 | sed 's%/.*%%'`
OCF_RESKEY_ip=$BASEIP; export OCF_RESKEY_ip

str=`echo $1 | sed 's%^'$BASEIP'/*%%'`
if [ ! -z "$str" ]; then
    NETMASK=`echo $str | sed 's%/.*%%'`
    OCF_RESKEY_cidr_netmask=$NETMASK; export OCF_RESKEY_cidr_netmask
    
    str=`echo $str | sed 's%^'$NETMASK'/*%%'`
    NIC=`echo $str | sed 's%/.*%%'`
    case $NIC in
	[0-9]*)	BROADCAST=$NIC
	    OCF_RESKEY_broadcast=$BROADCAST; export OCF_RESKEY_broadcast
	    NIC=
	    ;;
	"")	;;
	*)	BROADCAST=`echo $str | sed -e 's%^'$NIC'/*%%' -e 's%/.*%%'`
	    OCF_RESKEY_nic=$NIC; export OCF_RESKEY_nic
	    OCF_RESKEY_broadcast=$BROADCAST; export OCF_RESKEY_broadcast
	    ;;
    esac
fi

OCF_TYPE=IPaddr2
OCF_RESKEY_lvs_support=1
OCF_RESOURCE_INSTANCE=${OCF_TYPE}_$BASEIP
export OCF_TYPE OCF_RESOURCE_INSTANCE OCF_RESKEY_lvs_support

ra_execocf $2
