#! /bin/sh
# /etc/rc.serial 
#	Initializes the serial ports on your system
#
# chkconfig: 2345 50 75
# description: This initializes the settings of the serial port
#
# Distributed with setserial version 2.15
# 
# XXXX note: as of 2.15, the autosave feature doesn't work if you are
# using the multiport feature; it doesn't save the multiport configuration
# (for now).  Autosave also doesn't work for the hayes devices.  
#Will fix later...
#
#

SETSERIAL=/bin/setserial
modconf=/var/run/setserial.conf

# If the serial executable has been removed abort the configuration
[ -x ${SETSERIAL} ] || exit 0

#
# make sure that a serial device is loaded...
# insmod -k serial 2>/dev/null
#

ALLDEVS="/dev/ttyS?"
if /bin/ls /dev/ttyS?? 2> /dev/null 1>&2 ; then
	ALLDEVS="$ALLDEVS /dev/ttyS??"
fi

#
# Handle System V init conventions...
#
case $1 in
start | restart | force-reload )
	action="start";
	;;
stop)
	action="stop";
	;;
modload)
	action="modload";
	;;
modsave)
	action="modsave";
	;;
*)
	action="start";
esac

if test $action  = modload ; then
  echo "Restoring persistent state of serial.o module due to module reload... "
  if test -f ${modconf} ; then
        while read device args
        do
               case "$device" in
                   ""|\#*)
                       continue
                       ;;
               esac
           ${SETSERIAL} -z $device $args
        done < ${modconf}
  else
    echo "Warning - no module state found (ok if this is at bootup)"
    echo "Using the bootup configuration instead."
    action="start";
  fi
  exit 0
fi

if test $action  = stop ; then
	if test "`head -1 /etc/serial.conf`X" = "###AUTOSAVE###X" ; then
		echo -n "Saving state of known serial devices... "
		grep "^#" /etc/serial.conf > /etc/.serial.conf.new
		${SETSERIAL} -G -g ${ALLDEVS} | grep -v "uart unknown\|pcmcia" >> /etc/.serial.conf.new
		echo -n "backing up serial.conf"
		mv /etc/serial.conf /etc/.serial.conf.old
		mv /etc/.serial.conf.new /etc/serial.conf
		echo " done."
	elif test "`head -1 /etc/serial.conf`X" = "###AUTOSAVE-FULL###X" ; then
		echo -n "Saving state (including unknowns) of serial devices... "
		grep "^#" /etc/serial.conf > /etc/.serial.conf.new
		${SETSERIAL} -G -g ${ALLDEVS} | grep -v "pcmcia" >> /etc/.serial.conf.new
		echo -n "backing up serial.conf"
		mv /etc/serial.conf /etc/.serial.conf.old
		mv /etc/.serial.conf.new /etc/serial.conf
		echo " done."
	elif test "`head -1 /etc/serial.conf`X" = "###AUTOSAVE-ONCE###X" ; then
		echo -n "Saving state of known serial devices... "
		echo "###PORT STATE GENERATED USING AUTOSAVE-ONCE###" > /etc/.serial.conf.new
		grep "^#" /etc/serial.conf >> /etc/.serial.conf.new
		${SETSERIAL} -G -g ${ALLDEVS} | grep -v "uart unknown" >> /etc/.serial.conf.new
		echo -n "backing up serial.conf"
		mv /etc/serial.conf /etc/.serial.conf.old
		mv /etc/.serial.conf.new /etc/serial.conf
		echo " done."
	fi
	exit 0
fi

#
# Is it Start
#

if test $action  = start ; then
  echo "Loading the saved-state of the serial devices... "
  rm -f ${modconf}

  if test -f /etc/serial.conf ; then
        while read device args
        do
               case "$device" in
                   ""|\#*)
                       continue
                       ;;
               esac
           ${SETSERIAL} -z $device $args
           ${SETSERIAL} -bg $device
        done < /etc/serial.conf
  else
	echo "###AUTOSAVE###" > /etc/serial.conf
  fi
fi

if test $action  = modsave ; then
	echo -n "Saving serial.o state to emulate module data persistence... "
	rm -f ${modconf}
	${SETSERIAL} -G -g ${ALLDEVS} | grep -v "uart unknown\|pcmcia" > ${modconf}
	echo "done."
	exit 0
fi
