#!/bin/sh
#
#	@author Shane Chang
#

# reinit.d path prefix
RT_CB_PATH=/sbin

usage() {
	echo "usage: $0 <link> <wan|lan> <up|down>"
	echo "usage: $0 <ip|ip6> <wan|lan> <up|down|chg>"	
	exit 1
}

# $1 is the event class, i.e. the directory contains call-back scripts
# $2 is the event type, i.e. the wan/lan etc. for call-back scripts
# $3 is the event command, i.e. the operation options for call-back scripts
[ -n "$1" ] && RT_CB_CLASS=$1 || usage
[ -n "$2" ] && RT_CB_TYPE=$2 || usage
[ -n "$3" ] && RT_CB_CMD=$3 || usage

case $RT_CB_CLASS in
	"link"|"ip"|"ip6")
		;;
	*)
		usage
		;;
esac

case $RT_CB_TYPE in
	"wan")
		;;
	"lan")
		;;
	*)
		usage
		;;
esac

if [ "$RT_CB_CMD" != "up" ] && [ "$RT_CB_CMD" != "down" ] && [ "$RT_CB_CMD" != "chg" ]; then
	usage
fi

if [ "$RT_CB_CLASS" == "ip6" ] ; then
	RT_CB_CLASS2="ip"
else
	RT_CB_CLASS2="$RT_CB_CLASS"
fi

RT_CB_SCRIPTS=`ls $RT_CB_PATH/$RT_CB_CLASS".d"/$RT_CB_TYPE | sort`

# debug area 1
#echo "RT_CB_CLASS = [$RT_CB_CLASS]"
#echo "RT_CB_TYPE = [$RT_CB_TYPE]"
#echo "RT_CB_CMD = [$RT_CB_CMD]"
#echo "RT_CB_SCRIPTS = [$RT_CB_SCRIPTS]"

# execute every script with RT_CB_OPTION in the given directory
for RT_CB in $RT_CB_SCRIPTS
do
	$RT_CB_PATH/$RT_CB_CLASS".d"/$RT_CB_TYPE/$RT_CB $RT_CB_CLASS2$RT_CB_CMD
done
