#
# /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
RCLOCKFILE=/var/lock/subsys/serial


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

#
# Handle System V init conventions...
#
case $1 in
start)
	action="start";
	;;
stop)
	action="stop";
	;;
*)
	action="start";
esac

if test $action  = stop ; then
	if test `head -1 /etc/serial.conf`X = "###AUTOSAVE###X" ; then
		echo -n "Saving state of serial devices... "
		grep "^#" /etc/serial.conf > /etc/.serial.conf.new
		${SETSERIAL} -G -g ${ALLDEVS} >> /etc/.serial.conf.new
		mv /etc/serial.conf /etc/.serial.conf.old
		mv /etc/.serial.conf.new /etc/serial.conf
		echo "done."
	fi
	rm -f ${RCLOCKFILE}
	exit 0
fi

#
# If not stop, it must be a start....
#

if test -f /etc/serial.conf ; then
	grep -v ^# < /etc/serial.conf | while read device args
	do
	    ${SETSERIAL} -z $device $args
	done 
else
	echo "###AUTOSAVE###" > /etc/serial.conf
fi

touch ${RCLOCKFILE}
${SETSERIAL} -bg ${ALLDEVS}
