#!/bin/sh
#
# BIG BROTHER WEB BASED MANUAL NOTIFICATION ACKNOWLEDGEMENT SCRIPT
# Sean MacGuire
# Version 1.9c
# May 15th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#

#
# ************* NOTICE!!! ************
# BBHOME MUST BE SET CORRECTLY FOR THIS TO WORK

BBHOME="&&BBHOME"
export BBHOME
if [ "$BBHOME" = "" -o ! -d "$BBHOME" ]
then
	echo "Content-type: text/html


		<HTML><BODY>
		<H1>ERROR!<BR>BBHOME IS INVALID IN bb-ack.sh</H1></BODY>
		</HTML>"
	exit 1
fi

# Save the arguments for later use
ARG1="$1"
ARG2="$2"
ARG3="$3"

. $BBHOME/etc/bbdef.sh


# Are we called from a web page ?
if [ "$QUERY_STRING" != "" ]
then


    set -f
    QUERY_STRING=`echo "$QUERY_STRING" | $SED 's/[^A-Za-z0-9_,+.&=:%-]//g' | $SED 's/[&=][.][.]*//g' | $SED 's/\.\.//g'`
    set `echo "$QUERY_STRING" | $SED "s/[&=]/\ /g"` >/dev/null 2>&1
    while [ "$#" -ne 0 ]
    do
        case $1 in
	ACTION )	ACTION=`echo "$2" | $SED 's/[^A-Za-z]//g'`  
			shiftnum=2
			;;
	NUMBER )	NUMBER=`echo "$2" | $SED 's/[^0-9]//g'`
			if test "$NUMBER" != "DELAY"
			then
				shiftnum=2
			else 
				NUMBER=""
				shiftnum=1
			fi
			;;
	DELAY )		DELAY=`echo "$2" | $SED 's/[^0-9]//g'`
			if test "$DELAY" = ""
			then
				DELAY=60	# DUMB DEFAULT
			fi
			shiftnum=1
			;;
	MESSAGE )
		# MESSAGE MIGHT BE NULL
		case "$2" in
		ACTION | SUB | NUMBER | DELAY )
			MESSAGE=""
			shiftnum=1
			;;
		* )
			# I don't think the 1st SED is even required anymore
			MESSAGE=`echo "$2" | $SED 's/[${}\\]//g' | $SED 's/+/ /g'`
			shiftnum=2
			;;
		esac
		;;
	*)		shiftnum=1
			;;
	esac

	if [ "$#" -lt "$shiftnum" ]
	then  
		shift
	else
		shift $shiftnum
	fi

    done

    if [ "$ACTION" = "" ]
    then
	echo "Content-type: text/html


                <HTML><BODY>
                <H1>ERROR!<BR>bb-ack.sh called with invalid arguments</H1>"
	echo "</BODY></HTML>"   
	exit 1
    fi


    if [ "$ACTION" = "Ack" -o "$ACTION" = "Page" ]
    then

	echo "Content-type: text/html

<HTML>
<HEAD>

<!-- 
        Big Brother - 1.9c  (Wed, 15 May 2002 11:00:00 GMT)
        (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
        From: http://bb4.com/
        Contact: info@bb4.com
        BODY WAS FFFFCC
-->

<TITLE>Big Brother - Contact</TITLE>
</HEAD>

<BODY BGCOLOR=\"red\" BACKGROUND=\"$BBSKIN/bkg-red.gif\" FONT=\"Arial, Helvetica\" TEXT=\"silver\" LINK=\"#00FFAA\" VLINK=\"#FFFF44\">

<CENTER>

<TABLE WIDTH=100% BORDER=0>
<TR>
<TD VALIGN=TOP ALIGN=LEFT>
<A HREF=\"http://bb4.com\"><IMG SRC=\"$BBSKIN/bb.gif\" ALT=\"Big Brother\"
BORDER=0></A><BR>
<A HREF=\"$BBWEB/bb.html\">
<IMG SRC=\"$BBSKIN/bbnav2.gif\" ALT=\"View\" BORDER=0></A>
<A HREF=\"$BBWEB/help/bb-help.html\">
<IMG SRC=\"$BBSKIN/bbnav3.gif\" ALT=\"Help\" BORDER=0></A>
</TD>

<TD VALIGN=TOP ALIGN=RIGHT>
<FONT FACE=\"Arial, Helvetica\" SIZE=+1 COLOR=\"silver\">
<B>contact</B><BR>
<IMG SRC=\"$BBSKIN/bbnav1.gif\" ALT=\"Ack\" BORDER=0>
</TD></TR></TABLE>
<FONT FACE=\"Arial, Helvetica\" SIZE=+1 COLOR=\"silver\">
<BR><BR><BR><BR>"

    elif [ "$ACTION" = WapAck ]
    then

	# Change from 1 echo to 3 has it seems to fix Douwe.Dijkstra@I-Pay.com
	# WAP problem ...  Don't know why, and he doesn't know why either ...
	echo "Content-type: text/vnd.wap.wml"
	echo "
"
	echo "<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">
<wml>
<head>
<meta http-equiv=\"Cache-Control\" content=\"max-age=0\"/>
</head>
<card id=\"acked1\" title=\"BigBrother\">
<p align=\"center\">
<anchor title=\"BB\">Overall<go href=\"$BBWEB/wml/bb.wml\"/></anchor><br/>
<anchor title=\"BB2\">Summary<go href=\"$BBWEB/wml/bb2.wml\"/></anchor><br/>
</p>
<p align=\"center\" mode=\"nowrap\">"

    fi

else
    if [ "$#" -lt 2 -o "$#" -gt 3 ]
    then
	echo "usage: bb-ack.sh <ticket #> <delay> [message]"
	exit 1
    fi
    NUMBER=`echo "$ARG1" | $SED 's/[^0-9]//g'`
    DELAY=`echo "$ARG2" | $SED 's/[^0-9]//g'`
    MESSAGE="$ARG3"
    ACTION="Ack"
fi

if [ "$ACTION" = "Ack" -o "$ACTION" = "WapAck" ]
then
	if [ "$NUMBER" != "" ]
	then
		CHKNUM=`$EXPR "$NUMBER" + 0 2>/dev/null`
	else
		CHKNUM=""
	fi
	if [ "$NUMBER" != "$CHKNUM"  -o "$CHKNUM" = "" ]
	then
		if [ "$ACTION" = "WapAck" ]
		then
        		echo "Invalid<br/>security code<br/><br/>Try again."
        		echo "</p></card></wml>"
		else
		    	# Are we called from a web page ?
    			if [ "$QUERY_STRING" != "" ]
    			then
        			echo "Invalid security code<BR><BR>Try again."
        			echo "</I></H2></BODY></HTML>"
    			else
        			echo "usage: bb-ack.sh <ticket #> <delay> [message]"
    			fi
		fi
    		exit 1
	fi

	if [ "$DELAY" != "" ]
	then	
		CHKDELAY=`$EXPR "$DELAY" + 0 2>/dev/null`
	else
		CHKDELAY=""
	fi
	if [ "$DELAY" != "$CHKDELAY" -o "$CHKDELAY" = "" ]
	then
		if [ "$ACTION" = "WapAck" ]
		then
        		echo "Invalid delay<br/><br/>Try again."
        		echo "</p></card></wml>"
		else
	    		# Are we called from a web page ?
    			if [ "$QUERY_STRING" != "" ]
    			then
				echo "Invalid delay<BR>"
				echo "</I></H2></BODY></HTML>"
    			else
        			echo "usage: bb-ack.sh <ticket #> <delay> [message]"
    			fi
		fi
    		exit 1
	fi
	
    	if [ "$QUERY_STRING" != "" ]
	then
		if [ "$ACTION" = "WapAck" ]
		then
			BREAK="<br/>"
		else
			BREAK="<BR>"
		fi
	else
		BREAK=""
	fi

	# ack_event must go to all BBPAGERS...  RAC
	# hack because of the BBDISP/BBDISPLAYS BBPAGE/BBPAGERS crap in bb.c
	OLDBBDISPLAYS=$BBDISPLAYS
	BBDISPLAYS=$BBPAGERS
	$BB $BBPAGE "ack ack_event ${NUMBER} ${DELAY} ${MESSAGE}"
	BBDISPLAYS=$OLDBBDISPLAYS

	echo "Acknowledgement Sent. ${BREAK}${BREAK}Notification event ${NUMBER} ${BREAK} ${BREAK}${DELAY} minutes delay"

elif [ "$ACTION" = "Page" ]
then
	if test "$MESSAGE" = ""
	then
		echo "Empty message.<BR><BR>No page sent"
	else
		# Don't change the notify-admin.pagehelp name or suffer the consequences !
		# ack_event must go to all BBPAGERS...  RAC
		# hack because of the BBDISP/BBDISPLAYS BBPAGE/BBPAGERS crap in bb.c
		OLDBBDISPLAYS=$BBDISPLAYS
		BBDISPLAYS=$BBPAGERS
		$BB $BBPAGE "page notify-admin.pagehelp ${MESSAGE} User has requested your help.  Call ${MESSAGE}"
		BBDISPLAYS=$OLDBBDISPLAYS
		echo "Page sent.<BR><BR>You will be contacted shortly!"
	fi
else
	if [ "$QUERY_STRING" != "" ]
	then
		echo "Invalid ACTION type"
	fi
fi

if [ "$QUERY_STRING" != "" ]
then
	if [ "$ACTION" = "WapAck" ]
	then
		echo "</p></card></wml>"
	else
		echo "</BODY></HTML>"
	fi
fi

exit 0
