# serial port configuration script

logfile=/var/logs/startup.serial

raw(){
	echo $"Setting serial port to raw mode" | tee -a $logfile
	echo port $port | tee -a $logfile
	echo speed $speed | tee -a $logfile
	
	# configuration serial port
	/home/set_serial $port $speed
}

slip(){
	echo $"Setting serial port to slip mode" | tee -a $logfile
	echo port $port | tee -a $logfile
	echo speed $speed | tee -a $logfile
	echo proto $proto | tee -a $logfile
	echo localip $localip | tee -a $logfile
	echo remoteip $remoteip | tee -a $logfile
	
	if [ "$port" != "/dev/ttyS1" ] ; then
		echo "error: slip supported on serial port 1 only"
		exit 1
	fi
	
	# attach serial port to network interface 
	/sbin/slattach -vL -s $speed -p $proto $port &
	
	# wait a bit
	/bin/sleep 1
	
	# set up network interface
	# NOTE: assume only 1 slip interface is active
	/sbin/ifconfig sl0 $localip pointopoint $remoteip mtu 530 txqueuelen 150
}

ppp(){
	echo $"Setting serial port to ppp mode" | tee -a $logfile
	echo port $port | tee -a $logfile
	echo speed $speed | tee -a $logfile
	echo localip $localip | tee -a $logfile
	echo remoteip $remoteip | tee -a $logfile
	echo authentication $authentication | tee -a $logfile
	echo connection_type $connection_type | tee -a $logfile
	
	if [ "$port" != "/dev/ttyS1" ] ; then
		echo "error: ppp supported on serial port 1 only"
		exit 1
	fi
	
	curr_seq=`cat /etc/serial_1.seq`
	
	echo curr_seq $curr_seq | tee -a $logfile
	echo seq_num $seq_num  | tee -a $logfile
	
	update_config=no
	if [ "$seq_num" != "$curr_seq" ] ; then
		update_config=yes
		echo $seq_num > /etc/serial_1.seq
	fi
	
	echo update_config $update_config | tee -a $logfile
	
	case "$connection_type" in
	direct)
		if [ "$authentication" == "yes" ] ; then
			# direct connection, no authentication
			/usr/sbin/pppd /dev/ttyS1 $speed $localip:$remoteip \
			file /etc/ppp/options.direct-noauth &
		else
			# direct connection, authentication
			/usr/sbin/pppd /dev/ttyS1 $speed $localip:$remoteip \
			file /etc/ppp/options.direct-auth &
		fi
	;;
	dial-in)
		echo modem_init1 $modem_init1 | tee -a $logfile
		echo modem_init2 $modem_init2 | tee -a $logfile
		
		if [ "$update_config" == "yes" ] ; then
			echo "updating mgetty.config and options.modem-incoming" | tee -a $logfile
			
			# prepare modem initialization string for mgetty
			mgetty_config=/etc/mgetty+sendfax/mgetty.config
			/bin/cp ${mgetty_config}.temp ${mgetty_config}
			echo "" >> ${mgetty_config}
			echo "# NMX Tarurus user specific modem config" >> ${mgetty_config}
			echo "#" >> ${mgetty_config}
			echo "speed $speed" >> ${mgetty_config}
			echo "init-chat \"\" $modem_init1 $modem_init2" >> ${mgetty_config}
			
			# dial-in, authentication
			options_file=/etc/ppp/options.modem-incoming
			/bin/cp ${options_file}-temp ${options_file}
			echo "$speed $localip:$remoteip" >> ${options_file}
		fi
	;;
	dial-out)
		echo modem_init1 $modem_init1 | tee -a $logfile
		echo modem_init2 $modem_init2 | tee -a $logfile
		echo phone_num $phone_num | tee -a $logfile
		echo username $username | tee -a $logfile
		echo password $password | tee -a $logfile
		
		if [ "$update_config" == "yes" ] ; then
			# prepare modem initialization in chat-modem 
			chat_file=/etc/ppp/chat-modem-outgoing
			/bin/cp ${chat_file}-temp ${chat_file}
			echo "\"\" ATZ OK $modem_init1 $modem_init2 ATDT$phone_num CONNECT \"\"" >> ${chat_file}
		
			# prepare user account information in pap-secrets
			secrets_file=/etc/ppp/pap-secrets
			/bin/cp ${secrets_file}-temp ${secrets_file}
			echo "$username	*	$password	*" >> ${secrets_file}
		
			# sample run script
			startpppd_script=/etc/ppp/startpppd-modem-outgoing
			echo "generating script for dial-up connection" | tee -a $logfile
			echo "/usr/sbin/pppd /dev/ttyS1 $speed name $username \\" > ${startpppd_script}
			echo "file /etc/ppp/options.modem-outgoing" >> ${startpppd_script}
		
			# make script executable
			/bin/chmod a+x ${startpppd_script}
		fi
	;;
	*)
		echo "Unknown connection mode" | tee -a $logfile
		exit 1
	;;
	esac
	
	# modifying inittab for dial-in connections
	if [ "$update_config" == "yes" ] ; then
	
		echo "modifying inittab" | tee -a $logfile
		
		# update inittab 
		if [ "$connection_type" == "dial-in" ] ; then
			/bin/cp /etc/inittab.mgetty /etc/inittab
		else
			/bin/cp /etc/inittab.nomgetty /etc/inittab
		fi
		
		# rerun inittab
		/sbin/init q
	fi
}

serial(){
	if [ ! -f /etc/serial_$1.conf ]; then
		echo "Missing /etc/serial_$1.conf file" | tee -a $logfile
		exit 1
	fi

	. /etc/serial_$1.conf
	port=/dev/ttyS$1
	
	case "$line_discipline" in
	raw)
		raw
	;;
	slip)
		slip
	;;
	ppp)
		ppp
	;;
	*)
		echo $"Unknown serial line discipline" | tee -a $logfile
		exit 1
	;;
	esac
}

case "$1" in 
	start)
		# bring up serial ports
		echo "Serial port setup" > $logfile 
		echo $"Bringing up serial port 1" | tee -a $logfile
		serial 1
		echo $"Bringing up serial port 2" | tee -a $logfile
		serial 2
	;;
	*)
		echo $"Usage: $0 {start}"
		exit 1
esac
