#!/usr/bin/sh

# Release 0.03
# January 31, 2001
# Steve Lemieux, Hewlett-Packard

# Cut txt

#
# Script to print templates
#
# Usage:
#	templatereport.sh <templatename/groupname>
# 

debug=0
level=0

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

usage()
{
	echo "Usage: $1 [-l <level from 0 to 5>] <templatename/groupname>" >&2
	exit 2
}

level=""

while [ $# -gt 0 ]
do
	case "$1" in
	-l)
		shift
		level=$1
		;;
	*)
		break
		;;	
	esac
	shift
done

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

templatename="$1"

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

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

printservicefile()
{
	file=$1

	awk -v level=$level '
function getlastpos(a) {
	string=""
	for(x = a; x <= NF; x++)
	{
		if (string == "")
			string=$x
		else
			string=string " " $x
	}

	return string
}
function printfmt(lev,header,text)
{
	tabs=lev*4+length(header)+1
	pars=79-tabs
	texts=length(text)

	tab=""
	for(x=0;x < lev;x++)
		tab=tab "    "
	printf "%s%s ",tab,header
	for(x=0;x < length(header)+1;x++)
		tab=tab " "

	for(x = texts;x > 0;x=x-pars)
	{
		if (x != texts)
			printf tab
		printf "%s\n",substr(text,texts-x+1,pars)
	}
}
BEGIN {
	inset=0
	inhelp=0
	tmpltype=""
	inglobal=0
}
{
	if (level > 4)
	{
		print $0
		continue
	}

	if ($1 == "HELPTEXT")
		inhelp=1

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

	if (inhelp == 1)
		continue

	if (level > 3)
	{
		print $0
		continue
	}

	if ($1 == "OPCMSG" || $1 == "MONITOR" || $1 == "LOGFILE" || $1 == "SNMP" || $1 == "ECS" || $1 == "CONSOLE" || $1 == "TEMPLATE_GROUP")
	{
		tmpltype=$1
		printf "---------------------------------\n%s\t%s\n",$1,getlastpos(2)
		inglobal=1
	}

	if ($1 == "MSGCONDITIONS")
	{
		printf "CONDITIONS:\n"
		inglobal=0
	}
	if ($1 == "SUPPRESSCONDITIONS")
	{
		inglobal=0
		printf "SUPPRESS CONDITIONS:\n"
	}
	if ($1 == "SUPP_UNM_CONDITIONS")
	{
		inglobal=0
		printf "SUPPRESS UNMATCHED CONDITIONS:\n"
	}

	if (index($1,"MEMBER_") == 1)
		printfmt(1,"MEMBER:",getlastpos(2))

	if ($1 == "SET")
		inset=1

	if ($1 == "DESCRIPTION")
	{
		printfmt(1,"DESCRIPTION:",getlastpos(2))
		inset=0
	}

	if (level > 0)
	{
		if ($1 == "INTERVAL")
			printfmt(2,"INTERVAL:",getlastpos(2))
		if ($1 == "LOGPATH")
			printfmt(2,"LOGFILE:",getlastpos(2))
		if ($1 == "THRESHOLD")
			printfmt(2,"THRESHOLD:",getlastpos(2))
	}

	if (level > 1)
	{
		if ($1 == "TEXT" && inset == 0 && inglobal == 0)
			printfmt(2,"TEXT MATCH:",getlastpos(2))
		if ($1 == "SEVERITY" && inglobal == 0)
			printfmt(2,"SEVERITY:",getlastpos(2))
	}

	if (level > 2)
	{
		if ($1 == "TEXT" && inset == 1 && inglobal == 0)
			printfmt(2,"TEXT DISPLAYED:",getlastpos(2))
		if ($1 == "AUTOACTION")
			printfmt(2,"ACTION:",getlastpos(2))
	}
}
END {
	printf "\n"
}' ${file}
}

files="$tmpdir/download/*/TEMPLATES/TEMPLGROUP/*.dat $(ls $tmpdir/download/*/TEMPLATES/*/*.dat 2>/dev/null | grep -v TEMPLGROUP)"

for file in $files
do
	echo "Processing file ${file}..."
	printservicefile $file
done

#rm -Rf $tmpdir

exit 0

