#!/bin/sh

# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

usage() {
	echo "usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
}

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

	<longdesc lang="en">
		Balanced router configuration
	</longdesc>
	<shortdesc lang="en">Routing magic</shortdesc>
	<parameters>
		<parameter name="config" unique="0" required="1">
			<longdesc lang="en">
				interface:ip:gateway:table[ interface:ip:gateway:table[ interface:ip:gateway:table]]
			</longdesc>
			<shortdesc lang="en">Config</shortdesc>
			<content type="string" />
		</parameter>
	</parameters>
	<actions>
		<action name="start" timeout="20s" />
		<action name="stop" timeout="20s" />
		<action name="monitor" depth="0" timeout="20s" interval="10s" />
		<action name="validate-all" timeout="5s" />
		<action name="meta-data" timeout="5s" />
	</actions>
</resource-agent>
END
}

monitor() {
	local expected=""

	for i in $OCF_RESKEY_config; do
		local INTERFACE=$(echo "${i}" | cut -d ":" -f 1)
		local IP=$(echo "${i}" | cut -d ":" -f 2)
		local GATEWAY=$(echo "${i}" | cut -d ":" -f 3)
		local TABLE=$(echo "${i}" | cut -d ":" -f 4)

		ocf_log debug "Checking for existance lookup role for ${IP} to ${TABLE}"
		if ! ip rule show from "${IP}" lookup "${TABLE}" | grep -q .; then
			return $OCF_NOT_RUNNING
		fi

		cof_log debug "Checking the default route in ${TABLE}"
		if [ "$(ip route show exact default table "${TABLE}")" != "default via ${GATEWAY} dev ${INTERFACE} " ]; then
			return $OCF_NOT_RUNNING
		fi

		expected="${expected} nexthop via ${GATEWAY} dev ${INTERFACE} weight 1"
	done

	got=" $(ip route show exact 0.0.0.0/0 | tail -n +2 | xargs echo)"

	ocf_log debug "expected: [${expected}] - got: [${got}]"

	if [ "$got" != "$expected" ]; then
		return $OCF_NOT_RUNNING
	fi

	ip route flush cache

	return $OCF_SUCCESS;
}

stop() {
	if monitor; then
		for i in $OCF_RESKEY_config; do
			INTERFACE=$(echo "${i}" | cut -d ":" -f 1)
			IP=$(echo "${i}" | cut -d ":" -f 2)
			GATEWAY=$(echo "${i}" | cut -d ":" -f 3)
			TABLE=$(echo "${i}" | cut -d ":" -f 4)

			ip route del default via "${GATEWAY}" table "${TABLE}"
			ip rule del from "${IP}"
		done

		ip route del default
	fi

	return $OCF_SUCCESS
}

start() {
	if ! monitor; then
		local rcmd=""

		for i in $OCF_RESKEY_config; do
			INTERFACE=$(echo "${i}" | cut -d ":" -f 1)
			IP=$(echo "${i}" | cut -d ":" -f 2)
			GATEWAY=$(echo "${i}" | cut -d ":" -f 3)
			TABLE=$(echo "${i}" | cut -d ":" -f 4)

			ip route add default via "${GATEWAY}" table "${TABLE}"
			if ip rule show from "${IP}" | grep -q .; then
				ip rule del from "${IP}"
			fi
			ip rule add from "${IP}" table "${TABLE}"

			rcmd="${rcmd} nexthop via ${GATEWAY} dev ${INTERFACE} weight 1"
		done

		if ip route show exact 0.0.0.0/0 | grep -q .; then
			ip route del default
		fi

		ip route add default scope global $rcmd

		monitor
		return $?
	fi

	return $OCF_SUCCESS;
}

validate() {
	check_binary ip

	if [ $? != 0 ]; then
		return $OCF_ERR_ARGS
	fi

	return $OCF_SUCCESS
}

case $__OCF_ACTION in
	meta-data)
		meta_data
		exit $OCF_SUCCESS
	;;
	start) start;;
	stop) stop;;
	monitor) monitor;;
	validate-all) validate;;
	usage|help)
		usage
		exit $OCF_SUCCESS
		;;
	*)
		usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac

rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc