#!/bin/sh 

######################
## TSconfig
## Technologic Systems
## ver 1.0.02	liberty young	7.30.2002
##
## Script to automate the configuration a TS-Linux SBC
## This script relies on /etc and on the TS distributed
## start up scripts

clear

###################{CONFIGDIR} needs trailing slash
CONFIGDIR="/etc/"
COUNTER=0
changeBAUD="false"
VTY=5



function GrepAsk 
{
# Arg 1 is $Item..item to grep for, ask about
Item="$1"
	dflt=$(grep "$Item" ${CONFIGDIR}${FILE}.bak| awk -F: '{print $2}')
	echo "(default=$dflt)y/n)" 
	read answer
}

FILE=inittab

 if [ ! -f /etc/inittab ]; then
	 echo "/etc/inittab does not exists..critical failure"
	 cp /etc/opt/inittab /etc/inittab
	 exit -1
 fi
 cp /etc/inittab /etc/inittab.bak

## First, lets manipulate /etc/inittab 
## Dftl Runlevel?

echo "Would you like to change the Default Runlevel? "
GrepAsk "dflt"
case $answer in
	"y" | "Y" |"yes")
	   echo "Boot into which RunLevel (2,3,4,5)?"
	   read DFLT
	   ;;
	 *)
	   DFLT=$dflt
	  ;;
 esac

echo "Change Serial Console Login? (y/n)"
read answer
case $answer in
	"y" | "Y"|"yes")
	    echo "Which serial  port for login (COM2 is 1) (0,1)?"
	    read reply
	    if [ "$reply" = "" ]; then 
		    COM=ttyS1
	    else
		    COM=ttyS$reply
	    fi
	    
	    echo "Enable Console Logins in which Runlevels? (2,3,4,5)"
	    echo "Default is 2345 for levels 2,3,4 and 5"
	    read LGN 

		BAUD=`grep lgn0 /etc/inittab | awk '{print $3}'`
		if [ $COM == "ttyS1" ] ; then
			currentCOM=2
		else
			currentCOM=1
		fi

		echo "Change the kernel messages and console baud rate (current=$BAUD) on COM$currentCOM? (y,n)"
		read answer
		
		case $answer in
			"y"|"Y"|"yes")
		
			if [ $BAUD == "115200" ]; then	
				echo "Change COM$currentCOM to 9600? (y/n)"
				read reply
	
				case $reply in
					"y"|"Y"|"yes")
			
					BAUD=9600
					changeBAUD=true
				esac
			else
				echo "Change COM$currentCOM to 115200? (y/n)"
				read reply

				case $reply in
					"y"|"Y"|"yes")
				
					BAUD=115200
					changeBAUD=true
				esac
			fi

			echo ""
		esac
	   ;;
	*)
	    COM=ttyS1
	    LGN=2345
	    ;;
esac



echo "Change VGA Terminal Login? (y/n)"
read answer
case $answer in 
	"y"|"Y"|"yes")
	   echo "Enable Virtual Terminal Login? (y/n)"
	   read reply
	   if [ $reply == "y" ]; then
		   ENABLEVTY=s/###//
   	   else
		   ENABLEVTY="s/^[^v]*v/###v/"
	   fi 

	   echo "Enabled for which Runlevels (5 typical) ?"
	   read  VTY

	 ;;
	 *)
	   VTY=5
	   ENABLEVTY="s/^[^v]*v/###v/"
	   ;;
esac

#now do that sed thang!

sedrules="/^dflt/{;s/:[^:]*:/:$DFLT:/;};/lgn0/{;s/:[^:]*:/:$LGN:/;s/[^[:space:]]*$/$COM/;};/vty/{;$ENABLEVTY;s/:[^:]*:/:$VTY:/;};p"

sed -ne $sedrules /etc/inittab.bak > /etc/inittab

#Print out message to user if they need to change JP4
#Also change the sysconfig.cfg file to the correct baud
if [ $changeBAUD == "true" ]; then
	if [ $BAUD == "9600" ]; then
		echo "****************************************************"
		echo "* Remove JP4, to ensure you can see boot messages. *"
		echo "****************************************************"
		sedrules="s/115200/9600/g"
	else
		echo "*************************************************"
		echo "* Add JP4, to ensure you can see boot messages. *"
		echo "*************************************************"
		sedrules="s/9600/115200/g"
	fi
	sed -e $sedrules /etc/inittab > /etc/inittab.new
	mv /etc/inittab.new	/etc/inittab	

	#mount the first partition then change the 
	#sysconfig.cfg to the correct baud rate
	modprobe msdos
	mount /dev/hda1 /tmp
	sed -e $sedrules /tmp/syslinux.cfg > /tmp/syslinux.new
	mv /tmp/syslinux.cfg /tmp/syslinux.bak
	mv /tmp/syslinux.new /tmp/syslinux.cfg
	umount /tmp
fi

