#!/usr/bin/bash

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

# Last edited: Mar 9, 2019

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

# reinitialize a modem to handle Caller ID if NMBR=RING
# this indicates modem droped out of Caller ID mode.  Do
# not use with a modem that does not support Caller ID.
# requires a modem

# modem must send "RING" each time it sees the ringing signal
# must be run as root

ConfigDir=/etc/ncid/conf.d
ConfigFile=$ConfigDir/ncid-initmodem.conf
nciddconf=/etc/ncid/ncidd.conf

# Configuration file is not needed
#[ -f $ConfigFile ] && . $ConfigFile

[ "$NMBR" = "RING" ] &&
{
    # try to get the modem port and lockfile from ncidd.conf: set word = value
    while read arg1 word arg3 value junk
    do
        if [ "$arg1" = "set" -a "$word" = "lockfile" ]
        then
            lockfile="$value"
        elif [ "$arg1" = "set" -a "$word" = "ttyport" ]
        then
            ttyport="$value"
        fi
    done < $nciddconf

    # set the modem port and lockfile
    [ -z "$ttyport" ] && ttyport=/dev/ttyACM0
    [ -z "$lockfile" ] && lockfile=/var/lock/LCK..${ttyport##/dev/}

    # tell ncidd to reinitialize the modem
    touch $lockfile
    sleep 1
    rm -f $lockfile
}

exit 0
