#!/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

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 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 
	    ;;
	*)
	    COM=ttyS1
	    LGN=2345
	    ;;
esac

echo "Change Virtual 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
