#!/bin/ksh
# dial: find & dial a number in address/phone num database
# @(#) dial.ksh 2.1 94/05/12
# 1988, 1989 john h. dubois iii (john@armory.com)
# 90/02/03 last DOS version
# 90/10/01 ported to XENIX
# 91/10/11 Use address to look up addresses.
# 92/04/27 Changed address option specifier to +
# 92/06/21 Give ATH0 as a separate modem command.
# 92/06/28 Only print name of dialee if a name is given.
# 92/07/19 Print entire address record;
#          use +e & print a separator line if -e is given;
#          remove qualifier & secondary numbers from phone number line;
#          split input on every newline
# 94/04/23 Use .dialrc
# 94/05/12 Added speed parameter.

x=0	    # default uuchat debugging level

# These aliases test an integer variable to determine 
# if it is 0 (false) or nonzero (true).
alias istrue="test 0 -ne"    # exit zero for true, nonzero for false
alias isfalse="test 0 -eq"    # exit nonzero for true, zero for false

name=${0##*/}
usage=\
"Usage: $name [-hclnr] [-xn] [-f addrfile] [-t tty] [-s speed] [name|number]"

typeset -i check=0 isname=0 isnumber=0
unset SPEED	# Don't want to get this from environment
defSpeed=1200

while getopts :hctlnex:f:t:s: opt; do
    case $opt in
    h) echo \
"$name: look up name in address files and dial given phone number,
or translate & dial phone number.
$name uses a modem on a tty port to dial.  The name of the port can be given
with the -p option, in the environment variable PHONE, or by assigning a value
to PHONE in the file .dialrc in the user's home directory in the form
PHONE=/dev/portname
If it is neccessary to set the port to a particular serial rate before using it
to dial, the appropriate value can be assigned to SPEED in .dialrc, or passed
with the -s option.  The default is ${defSpeed}b.
The modem attached to the port must understand AT commands.
$usage
-h: print this help.
-c, -t: look up address & phone number but don't dial it.
-l: force [name|number] to be interpreted as a literal number.
-n: force [name|number] to be interpreted as a name to look up.
-e: print the regular expression that is searched for with awk.
-xn: set the uuchat debugging level to n.  The default is 0.
-f: search address file addrfile instead of the default address files.
-p: dial using the modem on tty port phoneport.
-s: set the tty speed before dialing."
       exit 0;;
    f) files="$files $OPTARG";;    # add arg to list of address files
    [tc]) check=1;;	# search for address but don't dial
    l) isnumber=1;;	# force name arg to be interpreted as number
    n) isname=1;;	# force name arg to be interpreted as name
    t) PHONE=$OPTARG;;	# tty port to use to dial
    s) SPEED=$OPTARG;;	# speed to set tty to
    x) x=$OPTARG;;	# set uuchat debugging level
    e) e=+e;;
    +?) echo "$0: options should not be preceded by a '+'."; exit 1;;
    ?) echo "$0: bad option '$OPTARG'.  Use -h for help."; exit 1;;
    esac
done
 
# remove args that were options
let OPTIND=OPTIND-1
shift $OPTIND

if [ $# -lt 1 ]; then
    echo "$usage"
    exit
fi

[ -n "$files" ] && files="-a $files"

# Save these so they won't be overridden by .dialrc
ttyPort=$PHONE
ttySpeed=$SPEED

rc=$HOME/.dialrc
[ -f $rc -a -r $rc ] && . $rc

[ -z "$ttyPort" ] && ttyPort=$PHONE
[ -z "$ttySpeed" ] && ttySpeed=$SPEED
[ -z "$ttySpeed" ] && ttySpeed=$defSpeed

if [ -z "$ttyPort" ]; then
    print -u2 "$name: PHONE not set."
    isfalse check && exit 1
fi

# Look up name if it's not specified as a literal number and
# either doesn't begin with a digit or is specified as a name
if isfalse isnumber && [[ $1 != [0-9]* ]] || istrue isname; then
    entry="$(address $e +d@ $files $*)" || exit 1
    echo "$entry" | {
	read Name
	read Address
	read Number
    }
    [ -n "$e" ] && echo ""
    if [ -z "$entry" ]; then
	print -u2 "No entry for $*."
	exit
    fi
    if [ -z "$Number" ]; then
	print -u2 "Entry for $* has no phone number."
	exit
    fi
    if [[ "$entry" = *+* ]]; then
	print -u2 "Name is ambiguous:"
	print -u2 "$entry"
	exit
    fi
    echo "$entry\n"
    Number=${Number%%  *}	# Remove other number fields, if any
    Number=${Number##*:}	# Remove qualifier, if any
    who="$Name at "
else
    Number=$1
fi

# Don't translate upper case letters; they may be modem commands
if [[ $Number = *[a-z]* ]]; then
    orignum=$Number
    Number=`echo $Number | tr '[a-p][r-y]' \
    222333444555666777888999`
    echo "Dialing $who$orignum ($Number) on $ttyPort."
else
    echo "Dialing $who$Number on $ttyPort."
fi

# A separate ATH0 works more reliably than H0 at the end of dial string.

# E1: echo commands
# Q0: quiet off (response codes on)
# M0: speaker off
# S11: set DTMF period
# ;: return to command mode after dialing
# H0: hang up
isfalse check && /usr/lib/uucp/uuchat \
-x$x $ttyPort $ttySpeed "" "ATE1Q0M0S11=50DT${Number};" OK ATH0 OK
