#!/usr/bin/bash

# ncid-speak
# usage: ncid --no-gui --module ncid-speak

# Last edited: Mar 9, 2019

# Copyright (c) 2001-2018 by
#   John L. Chmielewski <jlc@users.sourceforge.net>

# Announce the Caller ID
# Requires festival

# most of this program is taken from nciduser by Mace Moneta
# requires festival: http://www.cstr.ed.ac.uk/projects/festival

# WARNING: module ncid-kpopup may call ncid-speak
#          be sure ncid-kpopup passes stdin correctly to ncid-speak

ConfigDir=/etc/ncid/conf.d
ConfigFile=$ConfigDir/ncid-speak.conf

### defaults if not using config file ###
###  see $ConfigFile for description  ###
allowed_types="CID PID MSG NOT"
SpeakThisCall='$NAME'
SpeakThisMessage='$MESG'
SpeakInputCall="echo $SpeakThisCall | festival --tts"
SpeakInputMessage="echo $SpeakThisMessage | festival --tts"
SpeakTimes=1
SpeakDelay=2
Trigger=
AreaCodeLength=3

[ -f $ConfigFile ] && . $ConfigFile

# backward compatibility for pre-NCID 1.8 config files
  [ -n "$SpeakTypes" ] && allowed_types=$SpeakTypes
   [ -n "$SpeakThis" ] && SpeakThisCall=$SpeakThis
  [ -n "$SpeakInput" ] && SpeakInputCall=$SpeakInput

# === Start of common module script routines - ncid-modules-common.sh

# Last edited: Jun 30, 2022

# Before this code is executed:
#    - set $allowed_types as desired
#
# After this code is executed:
#    - stdin will have been read into $DATE, $TIME, $NMBR, $NAME, etc.
#    - $found=1 if $TYPE is in $allowed_types; otherwise $found will be null
#    - $type_cat(egory) will be "CALLTYPE", "MSGTYPE", or "UNKNOWNTYPE"
#    - $type_smart_phone is "Y" for both smart phone calls and messages,
#      otherwise $type_smart_phone will be null
#    - $type_desc(ription) will be set to human readable verbiage

# $allowed_types can be:
#    - any mix of zero or more individual call/line types separated by spaces
#    - any mix of zero or more type categories separated by spaces
#    - the special category "ALLTYPES"
#    - null, which defaults to "ALLTYPES"
#
# If $allowed_types includes spaces, you must surround the list with quotes.
#
# Note that "ALLTYPES" causes **any** $TYPE to be accepted.
#
# Examples:
#    allowed_types="CALLTYPE"
#    allowed_types="CALLTYPE MSGTYPE"
#    allowed_types="CID MSGTYPE HUP"

# input is always 12 lines
INPUT="DATE TIME NMBR NAME LINE TYPE MESG MTYPE FNMBR CTRY LOCA CARI"
for i in $INPUT; do read $i; done

# if input is from a call:
# input: DATE\nTIME\nNMBR\nNAME\nLINE\nTYPE\n""\n""\nFNMBR\nCTRY\nLOCA\nCARI\n
#
# if input is from a message
# input: DATE\nTIME\nNMBR\nNAME\nLINE\nTYPE\nMESG\nMTYPE\nFNMBR\nCTRY\nLOCA\nCARI\n

# determine $TYPE category and description
  type_cat="UNKNOWNTYPE"
  type_desc="Unknown Call Type ($TYPE)"
  type_smart_phone=

case "$TYPE" in

  BLK) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Blacklisted Call Blocked";;
  CID) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Incoming Call";;
  HUP) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Blacklisted Call Hangup";;
  MWI) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Voicemail Message Waiting";;
  OUT) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Outgoing Call";;
  PID) type_cat=CALLTYPE;  type_smart_phone=Y; type_desc="Caller ID from a smart phone";;
  PUT) type_cat=CALLTYPE;  type_smart_phone=Y; type_desc="Outgoing Smart Phone Call";;
  RID) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Ringback Call";;
  WID) type_cat=CALLTYPE;  type_smart_phone= ; type_desc="Call Waiting Caller ID";;

  MSG)  type_cat=MSGTYPE;  type_smart_phone= ; type_desc="Message";;
  NOT)  type_cat=MSGTYPE;  type_smart_phone=Y; type_desc="Notice of a smart phone message";;

esac

# Is this $TYPE one that can be processed by this module?
found=
if [ -z "$allowed_types" ]
then
    allowed_types="ALLTYPES"
    found=1
else
    for i in $allowed_types
    do
        if   [ $i = "ALLTYPES" ];  then found=1; break;
        elif [ $i = "$type_cat" ]; then found=1; break;
        elif [ $i = "$TYPE" ];     then found=1; break;
        fi
    done
fi

# === End of common module script routines - ncid-modules-common.sh

# Exit if $TYPE not found
[ -z "$found" ] && exit 0

if [ "$type_cat" = "MSGTYPE" ]
then
    if [ -n "$Trigger" ]
       then
          x=${#Trigger}
          if [ "${MESG:0:$x}" = "${Trigger}" ]
             then
                MESG=${MESG:$x}
          else
             exit 0
          fi
    fi
else
    if [ "$NAME" = "NO NAME" ] && [ $AreaCodeLength -ne 0 ]
    then
        NMBR=`echo $NMBR |sed 's/[^0-9]//g; s/^1//'`
        length=$((${#NMBR}-AreaCodeLength))
        if [ $length -le 0 ]
        then
            if [ ${#NMBR} -gt 1 ]
            then
                NAME=`echo $NMBR |sed 's/./ &/g'`
            fi
        else
            NAME="Area Code"`echo $NMBR | eval "sed -e 's/.\{$length\}\$//; s/./ &/g'"`
        fi
    fi
fi
eval SpeakThisCall="$SpeakThisCall"
eval SpeakThisMessage="$SpeakThisMessage"

while [ ${SpeakTimes:=1} != 0 ]
do
    if [ "$type_cat" = "MSGTYPE" ]
    then
        eval $SpeakInputMessage
    else
        eval $SpeakInputCall
    fi
    SpeakTimes=`expr $SpeakTimes - 1`
    [ $SpeakTimes = 0 ] || /bin/sleep ${SpeakDelay:=1}
done

exit 0
