#!/bin/csh  -f

# ------------------------------------------
# Setting Maximum number of ports: lscfg -vp |grep "Serial Port"
# All available tty: lscfg -vp |grep tty
# ------------------------------------------

echo ""
echo ""
echo "*******************************************************"
echo "*** Setup for MULTITECH only - do NOT use for ZyXEL ***"
echo "*******************************************************"
echo ""
echo ""

setenv modem_path     "/scitex/version/utils/modem/isdn/zyxel/europe"
setenv port           $1
setenv phone          $2
setenv log            log.txt
setenv modem_cnf      last_modem_cnf
setenv ColdStart_path /scitex/version/scripts/RemoteTools

rm $modem_path/$log >& /dev/null
pdisable $port

# Identify Machine Model Settings
setenv model `uname -M | tr -d 'A-z' | tr -d '(,/ )' | tr -d '-'`
# setenv model `lsattr -l sys0 -E | grep modelname | awk '{split($0,a,"-");split(a[2],b," ");print b[1]}'`

if ($model == 7024200) then 
    echo "Machine Type =  E30";
else  if ($model == 70427043) then 
    echo "Machine Type =  43P-140 Tiger1"; 
else  if ($model == 7043150) then 
    echo "Machine Type = 43P-150 Tiger2"; 
else if ($model == 702550) then 
    echo "Machine Type =  F50";
else if ($model == 7044170) then 
    echo "Machine Type =  44P-170 Hammerhead";
else if ($model == 702580) then 
    echo "Machine Type =  F80"; 
else if ($model == 702561) then 
    echo "Machine Type =  6F1";
endif



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> <phone_number=optional>"
    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 can't 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
   # arieh echo "Error in rmdev -l tty$num" >> $log
   # exit 1
  ;
endif

sleep 1
# Config the tty port
if (($model == "702550") || ($model == "702580") || ($model == "7044170")) then
   echo "Configuration for F50/F80/170 Models"
   echo "--> mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w '0' -a term='vt100' -a login='delay' -a speed='115200'"
   mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w '0' -a term='vt100' -a login='delay' -a speed='115200' 
   if ($status != 0) then
      echo "Error in mkdev operation" >> $modem_path/$log
   endif
else if ($model != "702550") then
   echo "Regular Configuration"
   echo "--> mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w "$portno" -a term='vt100' -a login='delay' -a speed='115200'"
   mkdev -c tty  -t 'tty' -s 'rs232' -p "sa$num" -w "$portno" -a term='vt100' -a login='delay' -a speed='115200'
   if ($status != 0) then
      echo "Error in mkdev operation" >> $modem_path/$log
      # exit 1
   endif
endif

# 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)"
   pdisable $port
   sleep 2
   ./kermit ./set0
#   sleep 2
    if ($status != 0) then
      echo "Error in Configuring tty0/com1 " >> $log
      exit 1
    endif
    sleep 2
   echo ""
   echo "--> pdisable tty0"
   pdisable tty0 > /dev/null
   echo ""
   echo "--> chdev -l 'tty0' -a flow_disp='rts'"
   chdev -l tty0 -a flow_disp='rts'
   sleep 2
   penable tty0 

# Configuring the Modem to tty1
else if (("$port" == "tty1") || ("$port" == "com2")) then
      echo ""
      echo "Configuring tty1 (com2)"
      pdisable $port
      sleep 2
      ./kermit ./set1
#      sleep 2
       if ($status != 0) then
          echo "Error in Configuring tty1/com2 " >> $log
          exit 1
       endif
      sleep 2
      echo ""
      echo "--> pdisable tty1"
      pdisable tty1 > /dev/null
      echo ""
      echo "--> chdev -l 'tty1' -a flow_disp='rts'"
      chdev -l tty1 -a flow_disp='rts'
      sleep 2
      penable tty1 

# Configuring the Modem to tty2
else if (("$port" == "tty2") || ("$port" == "com3")) then
      echo ""
      echo "Configuring tty2 (com3)"
      pdisable $port
      sleep 2
      ./kermit ./set2
#       sleep 2
       if ($status != 0) then
         echo "Error in Configuring tty2/com3 " >> $log
         exit 1
       endif
      sleep 2
      echo ""
      echo "--> pdisable tty2"
      pdisable tty2 > /dev/null
      echo ""
      echo "--> chdev -l 'tty2' -a flow_disp='rts'"
      chdev -l tty2 -a flow_disp='rts'
      sleep 2
      penable tty2 

# Configuring the Modem to tty3
else if (("$port" == "tty3") || ("$port" == "com4")) then
     echo ""
     echo "Configuring tty3 (com4)"
     pdisable $port
     sleep 2
     ./kermit ./set3
 #     sleep 2
      if ($status != 0) then
          echo "Error in Configuring tty3/com4 " >> $log
          exit 1
      endif
     sleep 2
     echo ""
     echo "--> pdisable tty3"
     pdisable tty3 > /dev/null
     echo ""
     echo "--> chdev -l 'tty3' -a flow_disp='rts'"
     chdev -l tty3 -a flow_disp='rts'
     sleep 2
     penable tty3 
endif

echo ""
# changed by arieh
# writing wrong file definition in last_modem_cobnfig
# if ($status == 0)  then
if (-e $modem_path/$log) then
  echo "----------------------------------------------------"
  echo "Operation ended with error/warning"
  echo "Please look in $modem_path/$log for more details on operation"
  echo "----------------------------------------------------"
else
  echo "isdn zyxel $port $phone " > $ColdStart_path/$modem_cnf  
endif

echo "--> Finished Modem Settings..."


pdisable $port
penable  $port

