#!/bin/bash 

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

INITTAB="/etc/inittab"
SYSLINUX="syslinux.cfg"
board=`grep TS /proc/SBC/info | cut -d '-' -f2`

############################################################
#This function will parse the syslinux.cfg file on the first
#partition to determine where the boot messages are sent to.
#It will also parse /etc/inittab on the second partition to
#determine where the login prompt is available. If formatting
#changes are made to eighther of these files this script may
#not work properly.
#############################################################
function printSettings
{
clear
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

#lets check if the base board is a TS-3300 or TS-5300
if [ "$board" != "3300" -a "$board" != "5300" ]; then
	bool_53_33=false
else
	bool_53_33=true
fi

#lets find where the boot messages are being sent to
#also get the baud rate
bootMsgCom=`grep -m 1 console /mnt/syslinux.cfg | cut -d '=' -f4 | cut -d ',' -f 1 |  sed -e "s, ,,g"`
bootMsgBaud=`grep -m 1 console /mnt/syslinux.cfg | cut -d '=' -f4 | cut -d ',' -f 2 | sed -e "s, ,,g"`
echo "+------------------------------------------+"
echo "+           Current Settings               +"
echo "+---+----------------+---------------------+"
if [ "$bootMsgCom" == "ttyS0" ]; then
	echo -n "| B |  Boot messages |  COM1 @"
	if [ "$bootMsgBaud" == "115200" ]; then
		echo " 115200      |"
	elif [ "$bootMsgBaud" == "9600" ]; then
		echo " 9600        |"
	fi
elif [ "$bootMsgCom" == "ttyS1" ]; then
	echo -n "| B |  Boot messages |  COM2 @"
	if [ "$bootMsgBaud" == "115200" ]; then
		echo " 115200      |"
	elif [ "$bootMsgBaud" == "9600" ]; then
		echo " 9600        |"
	fi
elif [ "$bootMsgCom" ==  "tty1" ]; then
	echo "| B |  Boot messages |  VGA                |"
else
	echo "| B |  Boot messages |  Unknown            |"
fi
echo "+---+----------------+---------------------+"

#lets find where the login prompt is available
loginVGA=`grep vty1 /etc/inittab | cut -d ':' -f0`
loginSerial=`grep lgn0 /etc/inittab | cut -d ':' -f0`

#lets check if VGA login is available
if [ "$loginVGA" == "#vty1" ]; then
	boolLoginVGA=false
elif [ "$loginVGA" == "vty1" ]; then
	boolLoginVGA=true
else
	boolLoginVGA=null
fi

#lets check if Seial login is available
if [ "$loginSerial" == "lgn0" ]; then
	boolLoginSerial=true
elif [ "$loginSerial" == "#lgn0" ]; then
	boolLoginSerial=false
else
	boolLoginSerial=null
fi

#If serial login is available lets find on what com port & baud
if [ "$boolLoginSerial" ==  "true" ]; then
	
	loginSerial=`grep lgn0 /etc/inittab | cut -d ' ' -f4`
	loginBaud=`grep lgn0 /etc/inittab | cut -d ' ' -f3`
	
	if [ "$loginSerial" == "tts/1"  ]; then
		COM=COM2
	elif [ "$loginSerial" == "tts/0" ];then
		COM=COM1
	else
		COM=Unknown
	fi
fi

if [ "$boolLoginVGA" == "true" -a "$boolLoginSerial" == "true" ]; then
	echo -n "| L |  Login Prompt  | $COM @"
	if [ "$loginBaud" == "115200" ]; then
		echo " 115200 & VGA |"	
	elif [ "$loginBaud" == "9600" ]; then
		echo " 9600   & VGA |"
	fi
elif [ "$boolLoginVGA" == "true" -a "$boolLoginSerial" == "false" ]; then
	echo "| L |  Login Prompt  |  VGA         |"
elif [ "$boolLoginVGA" == "false" -a "$boolLoginSerial" == "true" ]; then
	echo -n "| L |  Login Prompt  |  $COM @"
	
	if [ "$loginBaud" == "115200" ]; then
		echo " 115200      |"
	elif [ "$loginBaud" == "9600" ]; then
		echo " 9600        |"
	fi
else
	echo "| L |  Login Prompt  |  Unknown     |"
fi 
echo "+---+----------------+---------------------+"
echo "| X |  Exit          |                     |"
echo "+---+----------------+---------------------+"
echo ""
echo "Press the letter corresponding to the option you would like to change"
echo -n "(B, L, X)? "

}

########################################
#This function will prompt the user to
#determine what changes need to be made.
########################################
function getInput
{
	read userInput
	if [ "$userInput" == "x" -o "$userInput" == "X" ]; then
		exit=true
	elif [ "$userInput" == "L" -o "$userInput" == "l" ]; then
		changeLogin
	elif [ "$userInput" == "B" -o "$userInput" == "b" ]; then
		changeBootmsg
	else
		echo -n "Invalid input, Press any key to try again..."
		read anyKey
	fi	
}

#####################################
#This function will change where login
#prompts are available.
#####################################
function changeLogin
{
	
	if [ "$boolLoginSerial" == "true" -a "$boolLoginVGA" == "true" ]; then
	#allow the user to configure serial login
		echo ""
		echo "Press B to change the baud rate for $COM"
		echo "Press C to change the COM/VGA  for login"
		echo "Press D to disable the VGA login"
		echo -n "(B, C, D)? "
		read serialChanges

		if [ "$serialChanges" == "D" -o "$serialChanges" == "d" ]; then
			sed -e s/vty1/#vty1/g /etc/inittab.tmp > /etc/inittab		

		elif [ "$serialChanges" == "B" -o "$serialChanges" == "b" ]; then
			if [ "$loginBaud" == "115200" ]; then
				echo -n "Would you like to change the baud rate on $COM to 9600 (y,n)? "
				read serialChanges
				if [ "$serialChanges" == "y" -o "$serialChanges" == "Y" ]; then
				sed -e s/115200/9600/g /etc/inittab.tmp > /etc/inittab
				
				fi
			elif [ "$loginBaud" == "9600" ]; then
				echo -n "Would you like to change the baud rate on $COM to 115200 (y,n)? "
				read serialChanges

				if [ "$serialChanges" == "y" -o "$serialChanges" == "Y"  ];then
				sed -e s/9600/115200/g /etc/inittab.tmp > /etc/inittab

				fi
			fi
		elif [ "$serialChanges" == "C" -o "$serialChanges" == "c"  ]; then
			if [ "$COM" == "COM1" ]; then
				echo -n "Would you like to change the login prompt to COM2 (y,n)? "
				read serialChanges
				
				if [ "$serialChanges" == "y" -o "$serialChanges" == "Y" ]; then
					#change inittab from tts/0 to tts/1
					sed -e "s,tts/0,tts/1,g" /etc/inittab.tmp > /etc/inittab							
				fi
			elif [ $COM == "COM2" ]; then
				echo -n "Would you like to change the login prompt to COM1 (y,n)? "
				read serialChanges
				
				if [ "$serialChanges" == "y" -o "$serialChanges" == "Y"  ]; then
				#change inittab, replace tts/1 with tts/0
					sed -e "s,tts/1,tts/0,g" /etc/inittab.tmp > /etc/inittab

				fi
			fi
		fi
	
	elif [ "$boolLoginSerial" == "false" -a "$boolLoginVGA" == "true" ]; then
	#allow the user to enable and configure serial login
		echo -n "Would you like to enable a login prompt on a COM port (y,n)? "
		read serialChanges

		if [ "$serialChanges" == "y" -o "$serialChanges" == "Y" ]; then
			echo "Press 1 to enable login on COM1 @ 9600"
			echo "Press 2 to enable login on COM1 @ 115200" 
			echo "Press 3 to enable login on COM2 @ 9600"
			echo "Press 4 to enable login on COM2 @ 115200"
			echo -n "(1, 2, 3, 4)? "
			read serialChanges

			#lets check what the current settings are
			currentCom=`grep lgn0 /etc/inittab | cut -d ' ' -f4`
			currentBaud=`grep lgn0 /etc/inittab | cut -d ' ' -f3`

			if [ "$serialChanges" == "1" ]; then
				sed -e "s/#lgn0/lgn0/g" -e "s/115200/9600/g" -e "s,$currentCom,tts/0,g" /etc/inittab.tmp > /etc/inittab
			
			elif [ "$serialChanges" == "2" ]; then
				sed -e "s/#lgn0/lgn0/g" -e "s/9600/115200/g" -e "s,$currentCom,tts/0,g" /etc/inittab.tmp > /etc/inittab

			elif [ "$serialChanges" == "3" ]; then
				sed -e "s/#lgn0/lgn0/g" -e "s/115200/9600/g" -e "s,$currentCom,tts/1,g" /etc/inittab.tmp > /etc/inittab
			
			elif [ "$serialChanges" == "4" ]; then
				sed -e "s/#lgn0/lgn0/g" -e "s/9600/115200/g" -e "s,$currentCom,tts/1,g" /etc/inittab.tmp > /etc/inittab
			fi
		fi
		
	elif [ "$boolLoginSerial" == "true" -a "$boolLoginVGA" == "false" ]; then
		echo ""
		if [ "$COM" == "COM1" ]; then
			if [ "$loginBaud" == "9600" ]; then
				echo "Press 0 to enable login on COM1 @ 115200"
				newBaud=115200
			else
				echo "Press 0 to enable login on COM1 @ 9600"
				newBaud=9600
			fi
				echo "Press 1 to enable login on COM2 @ 9600"
				echo "Press 2 to enable login on COM2 @ 115200"
				newCom=tts/1
		elif [ "$COM" == "COM2" ]; then
			if [ "$loginBaud" == "9600"  ]; then
				echo "Press 0 to enable login on COM2 @ 115200"
				newBaud=115200
			else
				echo "Press 0 to enable login on COM2 @ 9600"
				newBaud=9600
			fi
				echo "Press 1 to enable login on COM1 @ 9600"
				echo "Press 2 to enable login on COM1 @ 115200"
				newCom=tts/0
		fi
		echo "Press 3 to enable login on VGA"
		echo -n "(0,1,2,3)? "
		read serialChanges
	
		if [ "$serialChanges" == "0" ]; then
			sed -e s/$loginBaud/$newBaud/g  /etc/inittab.bak > /etc/inittab
		elif [ "$serialChanges" == "1" ]; then
			sed -e s/$loginBaud/9600/g -e "s,$loginSerial,$newCom,g" /etc/inittab.bak > /etc/inittab
		elif [ "$serialChanges" == "2" ]; then
			sed -e s/$loginBaud/115200/g -e "s,$loginSerial,$newCom,g" /etc/inittab.bak > /etc/inittab
		elif [ "$serialChanges" == "3" ]; then

			sed -e "s/#vty1/vty1/g" /etc/inittab.bak > /etc/inittab
			echo ""
			echo "*****************************************"
			echo "* Ensure you have a TS-9500 connected   *"
			echo "* with DIP switch 8 down to ensure you  *"
			echo "* see the boot messages...              *"
			echo "* Press enter to continue               *"
			echo "*****************************************"
		
		else
			echo "Invalid input, Press any key to contiune..."
			read anyKey
		fi

	else
		echo "There is a problem with your /etc/inittab"
		exit=true
	fi
}

#####################################
#This function will change where boot
#messages are sent to.
#####################################
function changeBootmsg
{

	if [ "$bootMsgCom" == "ttyS0" ]; then
		#allow the user to send the messages to com2
		#or to VGA
		if [ "$bootMsgBaud" == "115200" ]; then
			echo "Press 0 to send boot messages to COM1 @ 9600"		
			newBaud=9600
		else
			echo "Press 0 to send boot messages to COM1 @ 115200"
			newBaud=115200
		fi
		echo "Press 1 to send boot messages to COM2 @ 9600"
		echo "Press 2 to send boot messages to COM2 @ 115200"
		echo "Press 3 to send boot messages to VGA"
		echo -n "(0,1,2,3)? "
		read userInput

		if [ "$userInput" == "0" ]; then
			sed -e s/$bootMsgBaud/$newBaud/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

		elif [ "$userInput" == "1" ]; then
			sed -e s/115200/9600/g -e s/ttyS0/ttyS1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			echo ""
			echo "*****************************************"
			echo "* Ensure you don't have JP4 on your     *"
			echo "* TS-$board to ensure you see the boot   *"
			echo "* messages...                           *"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			read anyKey

			echo ""
		elif [ "$userInput" == "2" ]; then
			sed -e s/9600/115200/g -e s/ttyS0/ttyS1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			echo ""
			echo "*****************************************"
			echo "* Ensure you have JP4 on your           *"
			echo "* TS-$board to ensure you see the boot   *"
			echo "* messages...                           *"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			echo ""
		elif [ "$userInput" == "3" ]; then
			sed -e s/ttyS0/tty1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

				echo ""
				echo "*****************************************"
				echo "* Ensure you have a TS-9500 connected   *"
				echo "* with DIP switch 8 down to ensure you  *"
				echo "* see the boot messages...              *"
				echo "* Press enter to continue               *"
				echo "*****************************************"

			echo ""
		fi
	elif [ "$bootMsgCom" == "tty1" ]; then
		echo ""
		echo "Press 1 to send boot messages to COM1 @ 9600"
		echo "Press 2 to send boot messages to COM1 @ 115200?"
		echo "Press 3 to send boot messages to COM2 @ 9600"
		echo "Press 4 to send boot messages to COM2 @ 115200"
		echo -n "(1,2,3,4)? "
		read userInput
		
		if [ "$userInput" == "1" ]; then
			sed -e s/tty1/ttyS0/g -e s/115200/9600/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			echo ""
			echo "*****************************************"
			echo "* Ensure you don't have JP4 on your     *"
			echo "* TS-$board to ensure you see the boot   *"
			echo "* messages...                           *"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			read anyKey

		elif [ "$userInput" == "2" ]; then
			sed -e s/tty1/ttyS0/g -e s/9600/115200/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			echo ""
			echo "*****************************************"
			echo "* Ensure you have JP4 on your TS-$board  *"
			echo "* to ensure you see the boot messages...*"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			read anyKey

		elif [ "$userInput" == "3" ]; then
			sed -e s/tty1/ttyS1/g -e s/115200/9600/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			echo ""
			echo "*****************************************"
			echo "* Ensure you don't have JP4 on your     *"
			echo "* TS-$board to ensure you see the boot   *"
			echo "* messages...                           *"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			read anyKey
		elif [ "$userInput" == "4" ]; then
			sed -e s/9600/115200/g -e s/tty1/ttyS1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg
			
			echo ""
			echo "*****************************************"
			echo "* Ensure you have JP4 on your TS-$board  *"
			echo "* to ensure you see the boot messages...*"
			echo "* Press enter to continue               *"
			echo "*****************************************"
			read anyKey
		fi	

	elif [ "$bootMsgCom" == "ttyS1" ]; then
		
		if [ "$bool_53_33" == "true"  ]; then
			echo -n "Would you like to send boot messages to VGA (y,n)? "
			read userInput
			
			if [ "$userInput" == "y" -o "$userInput" == "Y" ]; then
				sed -e s/ttyS1/tty1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

				echo ""
				echo "*****************************************"
				echo "* Ensure you have a TS-9500 connected   *"
				echo "* with DIP switch 8 down to ensure you  *"
				echo "* see the boot messages...              *"
				echo "* Press enter to continue               *"
				echo "*****************************************"
				read anyKey
			fi
		else
			echo ""
			if [ "$bootMsgBaud" == "115200" ]; then
				echo "Press 0 to send boot messages to COM2 @ 9600"
				newBaud=9600
			else
				echo "Press 0 to send boot messages to COM2 @ 115200"
				newBaud=115200
			fi
			echo "Press 1 to send boot messages to COM1 @ 115200"
			echo "Press 2 to send boot messages to COM1 @ 9600"
			echo "Press 3 to send boot messages to VGA"
			echo -n "(1,2,3)? "
			read userInput

			if [ "$userInput" == "0" ]; then
				sed -e s/$bootMsgBaud/$newBaud/g /mnt/syslinux.tmp > /mnt/syslinux.cfg

			elif [ "$userInput" == "1"  ]; then
				sed -e s/ttyS1/ttyS0/g -e s/9600/115200/g /mnt/syslinux.tmp > /mnt/syslinux.cfg
				
				echo ""
				echo "*****************************************"
				echo "* Ensure you have JP4 and JP6 on your   *" 
				echo "* TS-$board to ensure you see the boot   *"
				echo "* messages...                           *"
				echo "* Press enter to continue               *"
				echo "*****************************************"
				read anyKey
			
			elif [ "$userInput" == "2" ]; then
				sed -e s/ttyS1/ttyS0/g -e s/115200/9600/g /mnt/syslinux.tmp > /mnt/syslinux.cfg
				echo ""
				echo "*****************************************"
				echo "* Ensure you don't have JP4 on your     *"
				echo "* TS-$board to ensure you see the boot   *"
				echo "* messages...                           *"
				echo "* Press enter to continue               *"
				echo "*****************************************"
				read anyKey

			elif [ "$userInput" == "3" ]; then
				
				sed -e s/ttyS1/tty1/g /mnt/syslinux.tmp > /mnt/syslinux.cfg	
				echo ""
				echo "*****************************************"
				echo "* Ensure you have a TS-9500 connected   *"
				echo "* with DIP switch 8 down to ensure you  *"
				echo "* see the boot messages...              *"
				echo "* Press enter to continue               *"
				echo "*****************************************"
				read anyKey
			fi
		fi
	else
		echo "There is a problem with your syslinux.cfg file"
		exit=true
  	fi

}
###################################
#I guess you could call this main()
###################################
modprobe msdos
mount /dev/hda1 /mnt
#This will backup both files
cp -f /etc/inittab /etc/inittab.bak
cp -f /mnt/syslinux.cfg /mnt/syslinux.bak

exit=false
while [ "$exit" != "true"  ]
do
	#these are temp files needed by sed
	cp -f /etc/inittab /etc/inittab.tmp
	cp -f /mnt/syslinux.cfg /mnt/syslinux.tmp

	printSettings	
	getInput
	
done
rm /etc/inittab.tmp
rm /mnt/syslinux.tmp
umount /mnt

