#!/bin/bash

## @file modemConfig
# @brief Turn on/off modem support on ttyS0/ttyS1.
# @date November, 2008
# @author Balabanov, Sergey
# @version 2.00 - initial version
# History: 
#  Date     by                description change
#  02.12.08 Balabanov, Sergey  initial version
#  12.03.09 Balabanov, Sergey  Added new parameters.

help_usage()
{
   echo "Usage: modemConfig <on|off> [options]"
   echo "Configure the serial port for either modem or direct connection."
   echo ""
   echo "     on : Configure the serial port for modem connectivity."
   echo "    off : Configure the serial port for a direct connection (no modem)."
   echo " Options"
   echo "   -tty N : apply settings to ttySN port where N either 0 or 1. (Default: 0)"
   echo "   -init \"<init string>\" : modem initialization string." 
}

CMD=""
INITTAB_FILE="/etc/inittab"
TMP_INITTAB_FILE="/tmp/inittab"
COMPORT="0"
COMPORT_O="o" # variable value depends on COMPORT variable. ('o' for COMPORT=0, '1' for COMPORT=1)
NEW_MODEM_INIT_STRING=""  
MODEM_INIT_STRING="ATE0Q1\&D2\&C1S0=0\\\\015"

# Check number of parameters...
NUM_PARAM=$#
if [ $# -eq 0 ] ; then 
   help_usage
   exit 1
fi

# Parse parameters...
for ((i=0 ; i<${NUM_PARAM} ; i++)) ; do
   if [ "$1" == "on" ] || [ "$1" == "off" ] ; then
      CMD="$1"
      
   elif [ "$1" == "-tty" ] ; then
      shift
      NUM_PARAM=$((${NUM_PARAM} - 1))
      
      if [ "$1" == "0" ] ; then
         # Assume that ttyS0 does always present.
         COMPORT="0"
	 COMPORT_O="o"
	 
      elif [ "$1" == "1" ] ; then
	 # Check whether ttyS1 is available on system.
	 TTYS1=`/bin/egrep "^c1:2345:respawn:\/sbin\/agetty" ${INITTAB_FILE}`
	 if [ -z "${TTYS1}" ] ; then
	    echo "Error: ttyS1 is not avaiable on this system."
	    exit 1
	 fi
         COMPORT="1"
	 COMPORT_O="1"
	 
      else
         echo "Error: wrong tty number. It can be either 0 or 1."
	 exit 1
      fi
      
   elif [ "$1" == "-init" ] ; then
      shift
      NUM_PARAM=$((${NUM_PARAM} - 1))

      if [ -z "$1" ] ; then
         echo "Error: -init parameter requires a value to be provided."
	 help_usage
	 exit 1
      fi
      
      NEW_MODEM_INIT_STRING="$1" # this is just for further usage just to check whether -init options is provided.
      MODEM_INIT_STRING="$1" # replace default init string with the new one.

   elif [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
      help_usage
      exit 0
      
   else
      echo "Error: '$1' - unknown option."
      exit 1
   fi
   shift
done

# Check whether Modem init string is already present in the /etc/inittab file.
IS_MODEM_INIT_STRING=`/bin/egrep "^c${COMPORT_O}:2345:respawn:\/sbin\/agetty" ${INITTAB_FILE} | /bin/grep "\-I"`

if [ "$CMD" == "on" ] ; then
   # Put modem init string into the file /etc/inittab.
   
   # Check whether modem already configured on defined tty port.
   if [ -n "${IS_MODEM_INIT_STRING}" ] ; then
      # ttyS${COMPORT} already configured.
      if [ -n "${NEW_MODEM_INIT_STRING}" ] ; then
         # If -init param is provided then probably the customer wants to apply new init string.
         # In this delete the current configured line and then add it again with new init string.
         /bin/sed "s/\(^c${COMPORT_O}:\{1,\}2345:respawn:\/sbin\/agetty\)\(.\{1,\}\)\(ttyS${COMPORT}.\{1,\}\)/\1 \3/" ${INITTAB_FILE} > ${TMP_INITTAB_FILE}
	 /bin/cat ${TMP_INITTAB_FILE} > ${INITTAB_FILE}
      else
         echo "Warning: modem already configured on ttyS${COMPORT}."
	 exit 0
      fi
   fi
   
   /bin/sed "s/\(^c${COMPORT_O}:2345:respawn:\/sbin\/agetty\)\(.\{1,\}\)\(ttyS${COMPORT}.\{1,\}\)/\1 \2 -h -w -I '${MODEM_INIT_STRING}' \3/" ${INITTAB_FILE} > ${TMP_INITTAB_FILE}
   
   # Replace the original file with the new one.
   /bin/cat ${TMP_INITTAB_FILE} > ${INITTAB_FILE}
   
elif [ "$CMD" == "off" ] ; then
   # Remove init string from /etc/inittab
   
   if [ -n "${NEW_MODEM_INIT_STRING}" ] ; then
      echo "Warning: -init parameter is ignored."
   fi
   
   if [ -z "${IS_MODEM_INIT_STRING}" ] ; then
      echo "Warning: ttyS${COMPORT} is not configured for modem connectivity."
      exit 0
   fi
   
   /bin/sed "s/\(^c${COMPORT_O}:\{1,\}2345:respawn:\/sbin\/agetty\)\(.\{1,\}\)\(ttyS${COMPORT}.\{1,\}\)/\1 \3/" ${INITTAB_FILE} > ${TMP_INITTAB_FILE}

   # Replace the original file with the new one.
   /bin/cat ${TMP_INITTAB_FILE} > ${INITTAB_FILE}

else

   echo "Error: either on or off command should be provided."
   exit 1

fi

/sbin/init Q
echo "Done"

