#!/bin/csh  -f

# Created - By Ido Regev.
# Revised - By Arieh Stroul.
# Mar 2001 - revision 1.03
# ------------------------------------------
# Setting the port of operation

setenv port $1
setenv log  log.txt
setenv modem_cnf last_modem_cnf

rm ./log.txt >& /dev/null
#touch ./log.txt

if (("$port" == "tty0") || ("$port" == "com1")) then
    setenv  portno  "s1"
    setenv num  0
else if (("$port" == "tty1") || ("$port" == "com2")) then
    setenv portno  "s2"
    setenv num  1
else if (("$port" == "tty2") || ("$port" == "com3")) then
    setenv portno  "s3"
    setenv num  2
else if (("$port" == "tty3") || ("$port" == "com4")) then
    setenv portno  "s4"
    setenv num 3
else
    echo ""
    echo ""
    echo ""
    echo "usage: new_inst_modem tty<x> or new_inst_modem com<y>"
    echo "           x: 0, 1 ,2, 3           y: 1, 2, 3, 4"
    echo ""
    echo "Notes"
    echo "-----"
    echo "* tty0 is equivalent to com1"
    echo "     Examples:  new_inst_modem tty0"
    echo "                new_inst_modem com1" 
    echo "* The com port number can be found in the rear panel of the machine"
    echo ""
    echo ""
    echo ""
    exit 1
endif

# Reset the tty port
#echo "--> rm /etc/locks/*"
# rm /etc/locks/*

echo ""
echo "--> pdisable tty$num > /dev/null"
pdisable tty$num > /dev/null
if ($status != 0) then
   echo "Error in pdisable tty$num" >> $log
   exit 1
endif

echo ""
echo "--> rmdev -l tty$num -d > /dev/null"
rmdev -l tty$num -d > /dev/null
if ($status != 0) then
   echo "Error in rmdev -l tty$num" >> $log
   exit 1
endif

sleep 1
# Config the tty port

echo ""
echo "--> mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w "$portno" -a term='vt100' -a login='delay' -a speed='38400'"
mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w "$portno" -a term='vt100' -a login='delay' -a speed='38400'
if ($status != 0) then
   echo "Error in mkdev operation" >> $log
  exit 1
endif
sleep 1

# Configuring the Modem to tty0
if (("$port" == "tty0") || ("$port" == "com1")) then
   echo ""
   echo "Configuring tty0 (com1)"
   ./kermit ./set0
   sleep 2
   if ($status != 0) then
     echo "Error in Configuring tty0/com1 " >> $log
   exit 1
   endif
   penable tty0 

# Configuring the Modem to tty1
else if (("$port" == "tty1") || ("$port" == "com2")) then
      echo ""
      echo "Configuring tty1 (com2)"
      ./kermit ./set1
      sleep 2
      if ($status != 0) then
         echo "Error in Configuring tty1/com2 " >> $log
      exit (1)
      endif
      sleep 2
      penable tty1 

# Configuring the Modem to tty2
else if (("$port" == "tty2") || ("$port" == "com3")) then
      echo ""
      echo "Configuring tty2 (com3)"
      ./kermit ./set2
      if ($status != 0) then
        echo "Error in Configuring tty2/com3 " >> $log
        exit 1
      endif
      sleep 2
      penable tty2 

# Configuring the Modem to tty3
else if (("$port" == "tty3") || ("$port" == "com4")) then
     echo ""
     echo "Configuring tty3 (com4)"
     ./kermit ./set3
     if ($status != 0) then
         echo "Error in Configuring tty3/com4 " >> $log
         exit 1
     endif
     sleep 2
     penable tty3 
endif

echo ""
if ($status == 0) then
  echo "$port" > $modem_cnf  
endif
echo "--> Finished Modem Settings..."
