#!/bin/bash
#
#==============================================================
#
#  Abs:  resets, shows config of Terminal Servers
#
#  Name: iocTSmgr IOCNAME [ config | reset  | line | ether | port ]
#
#
# Created: 10-Nov-2008; Brobeck
#
#--------------------------------------------------------------
# MODS:
#
#  26-Apr-2021, Brobeck
#     Added MOXA command line tools to reset ports
#
#  19-Nov-2019, Brobeck
#     Implemented telnet-iocTSmgr.py to connect to Terminal servers.
#
#  27-May-2011, Brobeck
#     Added softegr as we are removing the iocegr account
#
#  24-Mar-2011, Brobeck
#     Added support for facet
#
#--------------------------------------------------------------
#
# Control who uses this script
# Users must be logged in as softegr, fsoftegr, laci, or flaci for production
#
if [ -d "/afs" ] ; then
  ONDEV=0
fi
#
# Show the HELP if no parameter is provided
#
if [ "$1" = "" -o "$1" = "-h" -o "$2" = "" ]  
 then
  echo ""
  echo "Usage:  iocTSmgr (ioc-name) ( config | reset | line | ether | port )"
  echo ""
  echo " ex)  iocTSmgr ioc-und1-uc25 config"
  echo ""
  echo "  config 	-(Digi) shows configuration of terminal server"
  echo "  reset		-(Digi/MOXA) performs a reset on the port associated with the ioc"
  echo "  line		-(Digi) shows serial port configurations"
  echo "  ether 	-(Digi) shows ethernet port configurations"
  echo "  port          -(Digi) shows All IOC's and their port # on terminal server"
  printf "\n\n\n"
  exit
fi
#
# MOXA Command
#MOXA_CMD="/usr/local/admin/moxa/mcc_tool -fw -r -i "
MOXA_CMD="/usr/local/admin/moxa/mcc_tool "
#
# screeniocs file
#
CFILE="$IOC_SCREEN/screeniocs"
#
# Check that the screeniocs file is not the DEV version
#
DEVCFILE=`echo $CFILE | grep dev >/dev/null; echo $?`
if [ "$DEVCFILE" = 0 ]; then
   printf "\n\n\tWrong screeniocs file--> $CFILE"
   printf "\n\tType \"bash\" to setup proper environment\n\n\n"
   exit
fi
TermSrv=`grep -i "^$1" $CFILE | awk '{ print $2 }'`

#
# Check to see if in the screeniocs file
#
if [ "$TermSrv" = "" ] ; then
    printf "\n\n"
    printf "\t---------------------------------------------------\n"
    printf "\tERROR:  $1 is NOT in the screeniocs file\n"
    printf "\t---------------------------------------------------\n\n\n"
    exit
  else
    ### The ts-b34-nw01 has a different password
    ### See Matt Boyes or Sonya Hoobler
    if [ "$TermSrv" = "ts-b34-nw01" ] && [ "$2" != "port" ]; then
      printf "\n\t$1 in on $TermSrv\n"
      printf "\tSee Sonya Hoobler or Matt Boyes for access\n\n\n"
      exit
    fi
fi

if [ "$ONDEV" = 0 ]; then
   CMD="python /afs/slac/g/lcls/tools/script/telnet-iocTSmgr.py"
   PASSFILE="/nfs/slac/g/lcls/sys/ts-reset"
   MOXA_CMD="/nfs/slac/g/lcls/sys/moxa/mcc_tool "
   TSFILE="/nfs/slac/g/lcls/sys/TerminalServer/ts-info"
 else
   CMD="python /usr/local/lcls/tools/script/telnet-iocTSmgr.py"
   PASSFILE="/usr/local/admin/sys/ts-reset"
   MOXA_CMD="/usr/local/admin/moxa/mcc_tool "
   TSFILE="/usr/local/admin/TerminalServer/ts-info"
fi

PASS=`cat $PASSFILE`
ISMOXA=`cat $TSFILE | grep MOXA | grep -i $TermSrv >/dev/null; echo $?`

case "$2" in

config)
  if [ "$ISMOXA" == 0 ]
    then
      echo "Not available on MOXA Terminal Servers"
    else
      $CMD  root $PASS $TermSrv "show config"
  fi
;;

reset)
  port=`/bin/grep -i "^$1" $CFILE | awk '{ print $3 }'`
  PORT=`expr $port - 2000`
  if [ "$ISMOXA" == 0 ]
    then
      TSIP=`host $TermSrv | awk '{print $4}'`
      $MOXA_CMD -re -sp -ps $PORT  -u "admin" -p $PASS -l /tmp/results.log -i $TSIP
    else
      $CMD root  $PASS $TermSrv "kill tty=$PORT" 
  fi
;;

line)
   if [ "$ISMOXA" == 0 ]
    then
      echo "Not available on MOXA Terminal Servers"
    else
      $CMD root $PASS $TermSrv "show line" 
  fi
;;

ether)
  if [ "$ISMOXA" == 0 ]
    then
      echo "Not available on MOXA Terminal Servers"
    else
      $CMD root $PASS $TermSrv "show ethernet"
  fi 
;;

port)
  if [ "$ISMOXA" == 0 ]
    then
      echo "Not available on MOXA Terminal Servers"
    else
       printf "\n\n"
       grep "$TermSrv" $CFILE | grep -v "^#"
       printf "\n\n"
  fi 
;;

*)
 printf "\n\n"
 printf "\t--------------------------------------------\n"
 printf "\tERROR:  $2 :  Not a valid parameter\n"
 printf "\t--------------------------------------------\n\n\n"
;;

esac
