#!/usr/bin/bash

# Server Hangup Extension

# Created by Ed Attfield
# Changes Jon Tulk
# Last edited: Jul 26, 2021

# This script calls multiple hangup extensions to chain hangup rules.

# Script is not called if caller number or name is in ncidd.whitelist.

# This hangup-combo-skel script is REPLACED whenever NCID is updated.
# Rename this script to hangup-combo and make your changes
# to the list of hangup extensions on the EXTLIST= line.

# Be sure your hangup-combo script has the execute permission set.
# Make sure you 'set hupmode' to be non-zero in ncidd.conf.
# and also have 'set hupname = hangup-combo'

# Input: The server passes one <string> to this script containing:
#        *DATE*<mmddyyyy>
#        *TIME*<hhmm>
#        *LINE*<lineid>
#        *NMBR*<number>
#        *MODE*<1,2,3>
#        *FNMBR*<formatted number>
#        *NTYPE*<type of device>
#        *CTRY*<country code>
#        *LOCA*<location>
#        *CARI*<carrier>
#        *NAME*<name>*

# The -v option must only be used in test mode, -v will be passed
# on to each extension and the results will be echoed to stdout.
# Do not use -v in production mode as it will cause excess output
# to the server.

EXTDIR=/usr/share/ncid/extensions

ConfigDir=/etc/ncid
ConfigFile=$ConfigDir/hangup-combo.conf

### defaults if not using config file ###
###  see $ConfigFile for description  ###
# Change this list of extension names to call different extensions
EXTLIST="hangup-fakenum hangup-fcc"

[ -f $ConfigFile ] && . $ConfigFile

# Look for "-v" option but don't remove it from the arg list
for ARG in "$@"
do
    if [ "x$ARG" = "x-v" ]
    then
        verbose=1
    fi
done

for EXT in $EXTLIST
do
    result=`$EXTDIR/$EXT "$@"`
    if [[ "$result" =~ .*hangup$ ]]; then
       # exit on the first "hangup"
       echo "$result"
       exit 0
    elif [ -n "$verbose" ]
    then
       # show result anyway
        echo "$result"
    fi
done

# the name and number looks good
echo "$result"
exit 0
