#!/bin/sh

# Page a cell phone, pager, or mail address
# Requires mail

# Last changed by jlc: Sun Aug 29, 2010

# input is 5 lines obtained from ncid
# input: DATE\nTIME\nNUMBER\nNAME\nLINE\n
#
# input is 5 lines if a message was sent
# input: \n\n\nMESSAGE\n\n
#
# ncid usage:
#   ncid --no-gui [--ring 4] [--message] --program ncid-page

ConfigDir=/etc/ncid
ConfigFile=$ConfigDir/ncidmodules.conf

# set ADDRESS to a pager or cell phone email address
PageTo=

# default mail user is user running script
PageFrom=mail

[ -f $ConfigFile ] && . $ConfigFile

[ -z "$PageTo" ] && {
    echo "Set 'PageTo' to a pager or cell phone email address"
    exit 1
}

read CIDDATE
read CIDTIME
read CIDNMBR
read CIDNAME
read CIDLINE

if [ -n "$CIDNMBR" ]
then
    MailMsg="\nNAME: $CIDNAME\nNMBR: $CIDNMBR\nTIME: $CIDTIME\nDATE: $CIDDATE\n"
    MailSubject="$CIDNAME $CIDNMBR"
else
    MailMsg="$CIDNAME"
    MailSubject="Message"
fi

# if line indicator found, include it
[ -n "$CIDLINE" ] && MailMsg="${MailMsg}LINE: $CIDLINE\n"

# if a mail user specified and script ID is root, set rootID
[ -n "$PageFrom" ] && [ "`id -nu`" = "root" ] && rootID=1

if [ -n "$rootID" ]
then
    # send mail as user $PageFrom
    echo -e $MailMsg |
        su -s /bin/sh -c "mail -s \"$MailSubject\" $PageTo" $PageFrom
else
    # send mail as user running script
    echo -e $MailMsg | mail -s "$MailSubject" $PageTo
fi

exit 0
