#!/bin/sh
# set up a line shared between alphanumeric terminal and modem
# this scripts replaces modemin, modemout, modemoff, switch, switchbox
#    at customer sites.
########################################################
# (C) Barco Graphics 1993
########################################################
# RCS version:
#  $Log:	setport,v $
# Revision 1.3  95/01/18  12:18:14  kh
# sleep before selftest
# 
# Revision 1.2  93/11/08  13:59:49  kh
# *** empty log message ***
# 
# Revision 1.1  93/11/08  12:24:40  kh
# First revision.
#  
# Locked by $Locker:  $
########################################################
# KH Version 2.0, October 1993
# KH Version 2.1, November 1993.	Asks password. Performs selftest.
#	Sets getty for incoming modem by default to 9600 baud,
#	which is baud rate between Octocom modem and DTE.
# KH January 1995. FIX001
# { [

USAGE='setport -{t|m|o|s} [-ASP] [-l speed] [ port ]
	-t set port up for terminal
	-m set port up for incoming modem
	-o set port up for outgoing modem
	-s sets modem on port in factory-set mode and performs self-test
	-A when setting up, use an alternate setup sequence, not factory setup
	-S when setting up, use setup modified for Spain
	-P when setting up, use setup modified for Portugal'

# defaults
PORT=1
FACTORYSETUP='&F';			WHICH=FACTORY
  # go to factory set-up
ALTSETUP='&D2&C1Q2&U2S23=87S37=30F0'	# you can modify this on-site
  # AT&D2: if DTR goes low, modem hangs up
  # AT&C1: DCD is on if modem is online and ready to send data. It follows CD in data mode
  # AT&U2: XON/XOFF is passed on to the remote modem
  # ATQ2: results are not passed on to DTE in answer mode, but are in originate mode
  # ATS23=8D: baud rate between modem and DTE is 9600 bps (is default)
  # ATS37=30: inactivity timer is 30 secs
  # ATF0: communicatition format is autobaud (is default)
SAVESETUP='&W'
  # save settings in non-volatile memory
SPAINSETUP='S22=86S33=00'
PORTUGALSETUP='S22=F6S33=02'
ASKID='ATI0'		; RESPID='242'
TESTIT='ATS18=5&T8'	; TESTOK='000'
  # self-test in analog loopback mode, for 5 seconds

DIE() { echo setport: $*; exit 1; }
 
KILLPORT() { 	# kill any getty or login process on this port
  while true; do 
    PROCESSES=`ps -e | egrep 'getty|login' | grep -v grep  | grep tty.$1 | cut -c-6`
    if [ -z "$PROCESSES" ]; then break; fi
    kill -9 $PROCESSES || DIE "could not kill: pid $PROCESSES"
  done
}

DOIT() {	# replace a line in the inittab
                # Args: port hunt prefix action
if [ $OS -eq 5 ]; then
  GETTY=/sbin/getty
else
  GETTY=/etc/getty
fi 

if [ $OS -ge 4 -a \( $1 -ne 1 -o $3 != ttyd \) ]; then
  GETTYOPTS="-N "
else
  GETTYOPTS=
fi 

SCRIPT="{ gsub(/^t$1:.*\$/, \"t$1:23:$4:$GETTY $GETTYOPTS$3$1 $2 # bg setport\"); print }"
nawk "$SCRIPT" /etc/inittab >/tmp/inittab || DIE "could not replace inittab line"
mv /tmp/inittab /etc/inittab 
chmod 644 /etc/inittab 

telinit q || DIE "could not telinit"
}

SELFTEST() {
  echo "is switchbox in position B? (<Enter> to confirm)"
  [ -z "`line`" ] || DIE "modem setup on port $1 not done"

  echo "is the modem an Octocom OSI8224A? (<Enter> to confirm)"
  [ -z "`line`" ] || DIE "modem setup on port $1 not done"

  SETUP=AT$FACTORYSETUP
  test $WHICH = ALTERNATE && SETUP=${SETUP}$ALTSETUP
  test -n "$SPAIN" && SETUP=${SETUP}$SPAINSETUP
  test -n "$PORTUGAL" && SETUP=${SETUP}$PORTUGALSETUP
  SETUP=${SETUP}$SAVESETUP
 
  $COLLECT /dev/ttyd$1 9600 $SETUP OK ERR || DIE "modem setup failed"
  echo "modem setup on port $1 done"

  echo "continue with modem self-test? (<Enter> to confirm)"
  [ -z "`line`" ] || DIE "modem self-test on port $1 not done"

  echo "is telephone line disconnected? (<Enter> to confirm)"
  [ -z "`line`" ] || DIE "modem self-test on port $1 not done"

  $COLLECT /dev/ttyd$1 9600 $ASKID $RESPID ERR >/dev/null || DIE "modem is NOT an Octocom OSI8224A!"

  echo "modem self-test on port $1 in progress (7 seconds)..."

  sleep 1 #FIX001
  $COLLECT -t 15 /dev/ttyd$1 9600 $TESTIT $TESTOK ERR || DIE "modem test failed"
  echo "modem self-test succesful. Done"
}

BADUSAGE() {
  echo "setport: usage: $USAGE"
  exit 2
}

# main 

# must be root or have root uid
id | cut -d' ' -f1 | grep root >/dev/null
if [ $? -ne 0 ]; then
  echo "setport: must be super-user to execute $0: \c"
  su root -c "$0 $*"
  exit $?
fi

OS=`uname -r | cut -c1`
PROCESSOR=`uname -m`

if [ -z "$BG_DIR" ]; then
  COLLECT=$BG_DIR/sys/bgcollect
else
  COLLECT=bgcollect
fi

while getopts il:mostASP c; do
  case $c in
    t)  if [ -n "$MODE" ]; then BADUSAGE; else MODE=TERMINAL; fi;;
    l)  SPEED=$OPTARG;;
    i|m)  if [ -n "$MODE" ]; then BADUSAGE; else MODE=IN; fi;;
    o)  if [ -n "$MODE" ]; then BADUSAGE; else MODE=OUT; fi;;
    s)  if [ -n "$MODE" ]; then BADUSAGE; else MODE=SELFTEST; fi;;
    A)  WHICH=ALTERNATE;;
    S)  SPAIN=TRUE;;
    P)  PORTUGAL=TRUE;;
    \?) BADUSAGE;;
  esac
done

shift `expr $OPTIND - 1`

if [ $# -gt 1 ]; then BADUSAGE
elif [ -n "$1" ]; then
  case $1 in
    [1-8])		PORT=$1;;
    tty?[1-8])		PORT=`echo $1 | cut -c5` ;;
    /dev/tty?[1-8])	PORT=`echo $1 | cut -c10` ;;
    *)			BADUSAGE;;
  esac
fi

[ -n "$MODE" ] || BADUSAGE
[ $PORT -gt 0 -a $PORT -le 8 ] || DIE "illegal port number $PORT"
if [ \( $PROCESSOR = IP5 -o $PROCESSOR = IP7 \) -a $PORT -gt 4 ]; then
  $PORT=`expr $PORT + 40`	# On-board port of CPUs 2 and 3
fi

echo `tty` | grep -v "/dev/tty[dm]$1" >/dev/null || DIE "you are logged in on port $PORT, cannot set it"
grep "^t$PORT:" /etc/inittab >/dev/null || DIE "no inittab line found for port $PORT"

case $MODE in
  TERMINAL) HUNT=co_${SPEED:=9600};;
  *) HUNT=du_${SPEED:=9600}			# du_ is hunting, from 2400 to 300 Baud.
     test "$SPEED" -gt 2400 && HUNT=dx_$SPEED	# dx_ is single-speed, can be up to 19.2 kBaud.
esac
grep "^${HUNT}#" /etc/gettydefs >/dev/null || DIE "no such gettydef: $HUNT"

case $MODE in
  TERMINAL) echo "setting port $PORT up for use by a terminal"
    DOIT $PORT $HUNT ttyd respawn
    echo '\007setup done. Set switchbox to position A. Press <Enter> when done.';;
  IN) 	echo "setting port $PORT up for use by a modem (incoming calls)"
    DOIT  $PORT $HUNT ttym respawn
    [ -l /dev/bgmodem ] && rm -f /dev/bgmodem
    ln -s /dev/ttym$PORT /dev/bgmodem
    chmod 666 /dev/bgmodem
    echo '\007setup done. Set switchbox to position B. Press <Enter> when done';;
  OUT) 	echo "setting port $PORT up for use by a modem (outgoing calls)"
    DOIT  $PORT $HUNT ttym off
    echo '\007setup done. Set switchbox to position B. Press <Enter> when done';;
  SELFTEST)
    DOIT  $PORT $HUNT ttym off
    SELFTEST $PORT; exit $?;;
  *)  exit 1
esac
line >/dev/null
KILLPORT $PORT
echo "setport: bye"

if [ -c /dev/bgtablet ]; then chmod 666 /dev/bgtablet; fi # to make sure

exit 0
# ] }
