#!/bin/sh
#
# @(#)UNIX Notifier 1.1
# Copyright 2002, American Power Conversion, Inc.
#
# NAME: notifier
#
# REVISION: 1.0
#
# LAST MODIFIED: 07/10/2002
#
# DESCRIPTION: Sends messages to different users within a UNIX system.
#
# USAGE: notifier [message] [ALL|SPECIFIC] [user]
#        Where [message] must contain "PowerChute" or "PowerChute in it.
#        Where [user] can contain more than one user.
#
# EXAMPLE: notifier Com Failed "PowerChute Serial Shutdown" SPECIFIC admin
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/etc:/usr/ccs/bin
export path

# Send the message to a single user
SendSingle() {
    USER=`echo $USER | sed s/\"//g`
    TITLE=`echo $TITLE | sed s/\"//g`
    MSG=`echo $MSG | sed s/\"//g`
    printf "\n\n\t $TITLE: $MSG \n\n" | write $USER
}

# Send the message to all users
SendAll() {
    TITLE=`echo $TITLE | sed s/\"//g`
    MSG=`echo $MSG | sed s/\"//g`
    printf "\n\n\t $TITLE: $MSG \n\n" | wall 2>/dev/null
}

# The message passed to the script may be separated by spaces. This part of
# the script puts them back together again, until it hits the word 
# "PowerChute".

SPACE=" "
while [ $1 != \"PowerChute ] && [ $1 != \"PowerChute\" ]
do
    MSG=`echo $MSG$SPACE$1`
    shift
done

# There could be more characters after "PowerChute" and before the key word
# SPECIFIC or ALL. Pump all the words until we hit one of the keywords.

while [ $1 != SPECIFIC ] && [ $1 != ALL ]
do
    TITLE=`echo $TITLE$SPACE$1`
    shift
done

# Decide if message is to ALL users or to a SPECIFIC user.

if [ $1 = SPECIFIC ]
then 

    # Send message to specific users only
    shift
 
    until [ -z $1 ]
    do
        USER=$1
        SendSingle
        shift
    done
    exit 0;

else

    # Send message to all users
    SendAll

fi

exit 0;
