#!/usr/bin/sh

# Release 0.10
# February 2, 2001
# Steve Lemieux, Hewlett-Packard

#
# Script fixing templates
#
# Usage:
#	fixtemplates.sh -n [-d <oldprefix>] [-p] <templategroupname> <prefix>
#	fixtemplates.sh -s [-d <oldservice>] [-f] [-p] [-v pattern]
#	                   <templatename/groupname> [<servicename>]
#	fixtemplates.sh -t [-p] [-v pattern] <templatename/groupname>
#	fixtemplates.sh -o [-p] [-v pattern] <templatename/groupname>
# 
# Required parameters:
# -n: Copy a group under a new name
# -s: Set default service name under each conditions of a template
# -t: Set trouble ticket flag to true
# -o: Set notification flag to true
#
# Optional parameters:
# -p: Preview mode. Always test with this option first
# -f: Only valid with -s. Force service name in conditions to be
#	overwriten is they already exist
# -d: Preview prefix or service name. Replace operation will be done
#	instead
# -v: Pattern matching condition names
#
# Note: Prefix should contain a separator at the end but the :
#	is not supported if group caintains monitors
#

debug=0

export PATH=$PATH:/opt/OV/bin/OpC:/opt/OV/bin/OpC/utils

usage()
{
	echo "Usage: $1 -n [-d <oldprefix>] [-p] <templategroupname> <prefix>" >&2
	echo "Usage: $1 -s [-d <oldservice>] [-f] [-p] [-v pattern]" >&2
	echo "       \t\t\t<templatename/groupname> [<servicename>]" >&2
	echo "Usage: $1 -t [-p] [-v pattern] <templatename/groupname>" >&2
	echo "Usage: $1 -o [-p] [-v pattern] <templatename/groupname>" >&2
	exit 2
}

force=0
fixservice=0
fixname=0
fixticket=0
fixnotif=0
numfix=0
preview=0
pattern=""
oldparam=""

while [ $# -gt 0 ]
do
	case "$1" in
	-n)
		let numfix=$numfix+1
		fixname=1
		;;
	-f)
		force=1
		;;
	-s)
		let numfix=$numfix+1
		fixservice=1
		;;
	-p)
		preview=1
		;;
	-t)
		let numfix=$numfix+1
		fixticket=1
		;;
	-o)
		let numfix=$numfix+1
		fixnotif=1
		;;
	-d)
		shift
		oldparam="$1"
		;;
	-v)
		shift
		pattern="$1"
		;;
	*)
		break
		;;	
	esac
	shift
done

if [ "$1" = "" ]; then
	usage $0
fi

if [ "$debug" = 1 ]; then
	preview=1
fi

templatename="$1"

if [ "$numfix" = 0 ]; then
	echo "At least one action must be specified" >&2
	usage $0
fi

if [ "$numfix" != 1 ]; then
	echo "Please specify only one action" >&2
	usage $0
fi

if [ "$fixname" = 1 -a "$2" = "" ]; then
	echo "This action requires a name" >&2
	usage $0
fi

addnparam="$2"

#tmpdir=`mktemp -p fixtemplate`
tmpdir=/tmp/fixdir

mkdir -p $tmpdir

if [ $? != 0 ]; then
	echo "Cannot create temporary directory $tmpdir"
	exit 1
fi

fixtemplatetype()
{
	type=$1

	case $type in
	SCHED*)
		type="SCHEDULE"
		;;
	OPCMSG*)
		type="INTERFACE"
		;;
	SNMP*)
		type="TRAP"
		;;
	ECS*)
		type="EC"
		;;
	esac

	echo "${type}_TEMPLATE"
}

gettemplates()
{
	typeset groupname="$1"

	tmpfile=`mktemp -d $tmpdir`
	opctmplrpt "$groupname" | grep MEMBER_ | cut -d_ -f 2- >$tmpfile

	echo "TEMPLATE_GROUP \"$groupname\" ;"

	while read grptype grpname
	do
		if [ "$grptype" = "TEMPLATE_GROUP" ]; then
			newgrpname=`echo $grpname | sed 's/"//g'`
			gettemplates "$newgrpname"
		else
			grptype=`fixtemplatetype $grptype`
			echo "$grptype $grpname ;"
		fi
	done <$tmpfile
}

dwnfile="$tmpdir/specfile"

opctmplrpt "$templatename" | grep -q TEMPLATE_GROUP
if [ $? = 0 ]; then
	gettemplates "$templatename" >$dwnfile
else
	result=`opctmplrpt "$templatename" | grep "$templatename" | grep -v "^#" | head -1`
	grptype=`fixtemplatetype $result`
	echo "$grptype \"$templatename\" ;" >$dwnfile
fi

opccfgdwn -force -silent $dwnfile $tmpdir/download
if [ "$?" != 0 ]; then
	echo "Configuration download failed" >&2
	rm -Rf $tmpdir
	exit 1
fi

fixservicefile()
{
	file=$1

	awk -v force=$force -v debug=$debug -v previousservice="$oldparam" -v fixservice=$fixservice -v fixnotif=$fixnotif -v fixticket=$fixticket -v globalservice="$addnparam" -v pattern="$pattern" '
function getlastpos(a) {
	string=""
	for(x = a; x <= NF; x++)
	{
		if (string == "")
			string=$x
		else
			string=string " " $x
	}

	return string
}
function printsvcline() {
	if (ismatch == 0)
		return
	if (globalservice != "")
		print "\t\t\tSERVICE_NAME",globalservice
	else if (defaultservice != "")
		print "\t\t\tSERVICE_NAME",defaultservice
}
function printservice(a) {
	if (debug == 1)
		printf "%d:%d:%d:%d:%d:%d:%s:%s:%d:%d:",intmpl,inmsgconds,indescr,incondition,inset,lastset,defaultservice,setservice,setnotif,setticket

	if (lastset == 1 && incondition == 1)
	{
		if (inset == 0)
			print "\t\tSET"

		if (!setservice && fixservice == 1)
			printsvcline()
		if (setticket == 0 && fixticket == 1)
			print "\t\t\tTROUBLETICKET"
		if (setnotif == 0 && fixnotif == 1)
			print "\t\t\tNOTIFICATION"

		setnotif=0
		setticket=0
		setservice=""
		inset=0
		incondition=0
		lastset=0
		if (inmsgconds == 0) defaultservice=""
	}
	if (lastset == 1)
	{
		ismatch=1
		lastset=0
	}
}
BEGIN {
	intmpl=0
	lastset=0
	lastdesc=0
	incondition=0
	inmsgconds=0
	inset=0
	inhelp=0
	ismatch=1
	defaultservice=""
	if (globalservice != "")
		globalservice="\"" globalservice "\""
}
{
	if ($1 == "HELPTEXT")
		inhelp=1

	if ($1 == "HELP")
		inhelp=0

	if (inhelp == 1)
	{
		print $0
		continue
	}

	if ($1 == "OPCMSG" || $1 == "MONITOR" || $1 == "LOGFILE" || $1 == "SNMP")
	{
		intmpl=1
		inmsgconds=0
		indescr=0
		lastset=1
	}

	if ($1 == "MSGCONDITIONS" && intmpl == 1)
	{
		inmsgconds=1
		indescr=0
		lastset=1
	}

	if (($1 == "SUPPRESSCONDITIONS"  || $1 == "SUPP_UNM_CONDITIONS") && intmpl == 1)
	{
		inmsgconds=0
		indescr=0
		lastset=1
	}

	if ($1 == "DESCRIPTION" && inmsgconds == 1)
	{
		indescr=1
		lastset=1
	}

	if ($1 == "CONDITION_ID" && indescr == 1)
	{
		incondition=1
	}

	if ($1 == "SET" && incondition == 1)
	{
		inset=1
		setservice=""
		setnotif=0
		setticket=0
		lastset=0
	}

	if ($1 == "NOTIFICATION")
		setnotif=1
	if ($1 == "TROUBLETICKET")
		setticket=1

	if ($1 == "SERVICE_NAME")
	{
		if (intmpl == 1 && inmsgconds == 0)
			defaultservice=getlastpos(2)
		if (inset == 1)
		{
			if (force == 1 && ismatch > 0)
			{
				printsvcline()
				setservice=defaultservice
				continue
			}

			setservice=getlastpos(2)
			if (setservice == "\"" previousservice "\"")
			{
				printsvcline()
				continue
			}
		}
	}

	printservice()

	print $0

	if ($1 == "DESCRIPTION" && inmsgconds == 1)
	{
		ismatch=1
		if (pattern != "")
		{
			# Strip quotes
			tmpstr=substr($2,2,length($2)-2)
			ismatch=match(tmpstr,pattern)
			if (debug == 1)
				print "DESCRIPTION MATCH ",tmpstr,ismatch
		}
	}
}
END {
	lastset=1
	printservice()
}' ${file} >${file}.new

	mv ${file}.new $file
	if [ "$preview" = 1 ]; then
		cat ${file}
	fi
}

fixnamefile()
{
	file=$1

	awk -v prefix="$addnparam" -v previousprefix="$oldparam" '
BEGIN {
	monname=""
}
function getlastpos(a) {
	string=""
	for(x = a; x <= NF; x++)
	{
		if (string == "")
			string=$x
		else
			string=string " " $x
	}

	return string
}
function stripstr(a) {
	len=length(a)
	if (len > 34)
	{
		newstring=substr(a,1,33) "\""
#		printf "CUT: %s:%s:\n",a,newstring
	}
	else
		newstring=a
	return newstring
}
{
	if ($1 == "TEMPLATE_GROUP" || index($1,"MEMBER_") == 1 || $1 == "SCHEDULE" || $1 == "OPCMSG" || $1 == "MONITOR" || $1 == "LOGFILE" || $1 == "SNMP" || $1 == "ECS" || $1 == "CONSOLE")
	{
		tmplname=getlastpos(2)

		if ($1 == "MONITOR")
		{
			monname=tmplname
			gsub("\"","",monname)
		}
		else
			monname=""

		if (previousprefix == "")
			sub("\"","\"" prefix,tmplname)
		else
			sub("\"" previousprefix,"\"" prefix,tmplname)

		if (index($1,"GROUP") != 0)
			tmplname=stripstr(tmplname)

		print $1,tmplname
	}
	else if ($1 == "MONPROG" && monname != "")
	{
		line=$0

		if (previousprefix == "")
			sub(monname,prefix monname,line)
		else
			sub(previousprefix monname,prefix monname,line)

		print line
	}
	else
		print $0
}' $file >${file}.new

	mv ${file}.new $file
	if [ "$preview" = 1 ]; then
		cat ${file}
	fi
}

files="$tmpdir/download/*/TEMPLATES/*/*.dat"

upldparam="-replace"
if [ "$fixname" = 1 ]; then
	upldparam="-add"
fi

for file in $files
do
	echo "Processing file ${file}..."
	if [ "$fixname" = 1 ]; then
		fixnamefile $file
	else
		fixservicefile $file
	fi
done

if [ "$preview" = 1 ]; then
	opccfgupld -check $tmpdir/download
#	rm -Rf $tmpdir
	exit 0
fi

opccfgupld $upldparam $tmpdir/download

#rm -Rf $tmpdir

exit 0

