#!/bin/sh
#****************************************************************
# *	Copyright (C) 2003, Wistron Inc.  All Rights Reserved.
# *	File Name:		evtconf
# *	Author:			Jerry Zhou
# *	Create Time: 	2003-06-27 11:16:09
# *	Last Update: 	2003-06-30 17:24:47 by Jerry Zhou 
#****************************************************************
wconf_path=/usr/local/share/asmagent/wconfig
trapconf_file=$wconf_path/trap.conf
eventconf_file=$wconf_path/event.conf

show_usage()
{
	echo ""
	echo "Usage: evtconf [options] parameters"
	echo "  -h                     This small usage guide"
	echo "  -l                     List current configuration"
	echo "  -a ip                  Add a new target ip address"
	echo "  -d ip                  delete a target ip address"
	echo "  --autoreboot [on|off]  Enable/disable automatically reboot OS when temperature is out of rage"
	echo "  --autoclean [on|off]   Enable/disable automatically clean event log when buffer is nearly full"
}

show_config()
{
	echo "Current Configuration:"
	echo "Trap target:"
	cat $trapconf_file | grep "^[a-zA-Z0-9]" | sed "s/trapsink//g"
	echo ""
###	echo "Event config:"
###	cat $eventconf_file
}

validate_ip()
{
	if [ $# != 1 ] ; then 
		echo "Error: validate(): No input parameter!"
		return 1 
	fi
	rst=`echo $1 | awk -F. '{ 
					if ($1 >= 0 && $1 <= 255 && $1~"^[0-9]+$" &&
						$2 >= 0 && $2 <= 255 && $2~"^[0-9]+$" &&
						$3 >= 0 && $3 <= 255 &&	$3~"^[0-9]+$" &&
						$4 >= 0 && $4 <= 255 && $4~"^[0-9]+$")
					{ print 0 }
					else
					{ print 1 }
				}'`
	if [ $rst = 1 ] ; then
		echo "Error: invalid IP: "$1
	fi
	return $rst
}

grep_ip()
{
	if [ $# != 1 ] ; then 
		echo "Error: grep(): No input parameter!"
		return 1 
	fi
	patternip="trapsink[ ]*"$1
	if cat $trapconf_file | grep "^[^#]" | grep ^"$patternip"$ 1>/dev/null 2>/dev/null ; then
		return 1
	fi
	return 0
}

add_ip()
{
###	validate_ip $1
###	if [ $? = 0 ] ; then
###		grep_ip $1
###		if [ $? = 0 ] ; then
			echo "trapsink "$1 >> $trapconf_file
###		else
###			echo "IP "$1" had been added into trap config file! Abort!"
###		fi
###	fi
}

delete_ip()
{
	grep_ip $1
	if [ $? = 1 ] ; then
		patterndel="/"$1"/d"
		cat $trapconf_file | sed "$patterndel" > /tmp/trapconffile.tmp
		mv -f /tmp/trapconffile.tmp $trapconf_file
	else
		echo "IP "$1" is not in trap config file! Abort!"
	fi
}

auto_reboot()
{
	if [ $# != 1 ] ; then 
		echo "Error: auto_reboot(): No input parameter!"
		return 1 
	fi
	if [ $1 = "on" ] || [ $1 = "off" ] ; then
		cat $eventconf_file | sed "/autoreboot/d" > /tmp/eventconffile.tmp
		echo "autoreboot="$1 >> /tmp/eventconffile.tmp
		mv -f /tmp/eventconffile.tmp $eventconf_file
	else
		echo "Error: Invalid parameter!"
		show_usage
	fi
}

auto_clean()
{
	if [ $# != 1 ] ; then 
		echo "Error: auto_clean(): No input parameter!"
		return 1 
	fi
	if [ $1 = "on" ] || [ $1 = "off" ] ; then
		cat $eventconf_file | sed "/autoclean/d" > /tmp/eventconffile.tmp
		echo "autoclean="$1 >> /tmp/eventconffile.tmp
		mv -f /tmp/eventconffile.tmp $eventconf_file
	else
		echo "Error: Invalid parameter!"
		show_usage
	fi
}


tmp=`getopt -o a:d:hl -l autoreboot:,autoclean: -n 'evtconf' -- "$@"`
if [ $? != 0 ] ; then
	show_usage
	exit 1
fi

eval set -- "$tmp"
while true; do
	case "$1" in
		-h)
			show_usage
			shift
			;;
		-l)
			show_config
			shift
			;;
		-a)
			add_ip $2
			shift 2
			;;
		-d)
			delete_ip $2
			shift 2
			;;
		--autoreboot)
			auto_reboot $2
			shift 2
			;;
		--autoclean)
			auto_clean $2
			shift 2
			;;
		--)
			shift
			break
			;;
		*)
			echo "Invalid input: "$1
			show_usage
			exit 1
			;;
	esac
done
exit 0