#!/bin/sh
#ident	"@(#)pkg.ls:ls/request	1.17"
#
#  Request script for the ls package
#
#
#
# set LSVAR for use later
#
LSVAR=/var/opt/ls
MENU_RESP=$1
PKGMSG=ls.pkg

# There are three scenarious in which this script will run:
#
# $HOW_INSTALL=
#
# (a) ON_BFLOP:		it is called from the
#			boot floppies. 
#
# (b) POSTREBOOT:	it is called upon reboot from set installation on the
#			boot floppies.
#
# (c)NORMAL:		it is called from pkgadd'ing the individual package
#			or from the network set installation.
write_exit()
{
	echo CLASSES=\"${CLASSES}\" >> ${MENU_RESP}

	echo KEYBOARD=\"$KEYBOARD\" >> ${MENU_RESP}
	echo LOCALE=\"$LOCALE\" >> ${MENU_RESP}
	echo DOALL=\"$DOALL\" >> ${MENU_RESP}
	echo GRAPHICS=\"$GRAPHICS\" >> ${MENU_RESP}
	echo LANGUAGE=\"$LANGUAGE\" >> ${MENU_RESP}
	echo TERR=\"$TERR\" >> ${MENU_RESP}
	if [ "$HOW_INSTALL" = "NORMAL" ]
	then
		menu -c
	fi
    	exit 0
}

HOW_INSTALL=NORMAL
# We are on the boot floppies if:
# the script /etc/inst/scripts/postreboot.sh exists 
[ -f /etc/inst/scripts/postreboot.sh ] && {
	HOW_INSTALL=ON_BFLOP
}

# We are rebooting after installation if the script
# /etc/rc2.d/S02POSTINST exists
[ -f /etc/rc2.d/S02POSTINST ] && {
	HOW_INSTALL=POSTREBOOT
}


# Graphics support will always be installed
# previously it had been optional depending on whether graphics was 
# already installed
GRAPHICS=YES

if [ "$HOW_INSTALL" != "POSTREBOOT" ]
then
   
if [ $GRAPHICS = "YES" ]
then
	CLASSES="base xsup build"
else
	CLASSES="base"
fi

elif $SILENT_INSTALL
then
	if [ "$KEYBOARD" = "" ]
	then
		KEYBOARD=NONE
	fi
#   Write out the configuration.
	write_exit
fi


if [ "$HOW_INSTALL" = "ON_BFLOP" ] 
then
# create magic file for postreboot.sh to know LS is installed
> /tmp/ls
	echo CLASSES=\"${CLASSES}\" >> ${MENU_RESP}
	exit 0
fi


MLOCALE=${LC_ALL:-${LC_MESSAGES:-${LANG:-C}}}
MENU_DIR=/etc/inst/locale/${MLOCALE}/menus/${PKGINST}
if [ ! -f ${MENU_DIR}/menu.up.err ] 
then
	if [ ! -d ${REQDIR}/${PKG}/${MLOCALE} ]
	then
		MLOCALE=C
	fi
	MENU_DIR=${REQDIR}/${PKG}/${MLOCALE}
fi
	
#
#  Set up to use menu_colors; default to C-locale if ${LANG}'s dir has
#  no menu_colors.sh
#
if [ -f /etc/inst/locale/${MLOCALE}/menus/menu_colors.sh ]
then
        . /etc/inst/locale/${MLOCALE}/menus/menu_colors.sh
else
        . /etc/inst/locale/C/menus/menu_colors.sh
fi

menu_colors regular

if [ $HOW_INSTALL = "NORMAL" ] 
then
# cannot upgrade package independent of underlying system because of 
# dependencies
	if [ `uname -r` != "4.2MP" ]
	then
    	menu -f $MENU_DIR/menu.up.err -o /dev/null
		exit 1
	else
# set for convenience to be used below
		PKGINSTALL_TYPE="OVERLAY"
	fi
fi

#
#  Check whether the boot floppies configured a keyboard.
#

eval `defadm keyboard KEYBOARD 2> /dev/null`
if [ $? != 0 ]
then
	KEYBOARD=""
	if [ "$HOW_INSTALL" = "NORMAL" ]
	then
    	menu -f $MENU_DIR/menu.ls00 -o /tmp/lSinfo -r
	fi
else
	if [ -z "$KEYBOARD" ] 
	then
		KEYBOARD=NONE
		export KEYBOARD
	fi
	KEYBOARD_NAME=`egrep $KEYBOARD $MENU_DIR/menu.ls1 | sed -e "s/.*$KEYBOARD:://" | sed -e"s/\/\/.*//"`
	export KEYBOARD_NAME
	if [ "$PKGINSTALL_TYPE" = "OVERLAY" ]
	then
		eval `grep -v KEYBOARD $UPGRADE_STORE/$LSVAR/config 2> /dev/null`
		if [ $? = 0 ] && [ ! -z "$LOCALE" ] && [ ! -z "$DOALL" ] && 
			[ ! -z "$LANGUAGE" ] && [  ! -z "$TERR" ]
		then
			if [ $HOW_INSTALL = "NORMAL" ]
			then
				if [ "$DOALL" = "YES" ]
				then
					LOCALES_INST=`LC_MESSAGES=$MLOCALE gettxt "${PKGMSG}:3:All Locales"`
				else
					LOCALES_INST=`LC_MESSAGES=$MLOCALE gettxt "${PKGMSG}:4:Related Locales Only"`
				fi
				LOCALE_NAME=`egrep "!$LOCALE" $MENU_DIR/menu.ls2 | sed -e"s/.*!$LOCALE:://" | sed -e"s/\/\/.*//"`
				export LOCALE_NAME LOCALES_INST
      			menu -r -f $MENU_DIR/menu.ls04 -o /tmp/lSinfo -r
      			OPT=`cat /tmp/lSinfo`
      			OPT=`expr "$OPT" : '.*"\(.*\)"'`
	  			if [ $OPT -eq 1 ]
	  			then
					write_exit
	  			else
					KEYBOARD=""
				fi
			else
				write_exit
			fi
		elif [ "$HOW_INSTALL" = "NORMAL" ]
		then
      			menu -f $MENU_DIR/menu.ls01 -o /tmp/lSinfo -r
      			menu -f $MENU_DIR/menu.ls02 -o /tmp/lSinfo -r
      			OPT=`cat /tmp/lSinfo`
      			OPT=`expr "$OPT" : '.*"\(.*\)"'`
	  		if [ $OPT -eq 2 ]
	  		then
          			KEYBOARD=""
	  		fi
    		fi
	elif [ "$PKGINSTALL_TYPE" = "UPGRADE" ]
	then
		unset LOCALE
		eval `grep -v KEYBOARD $UPGRADE_STORE/var/opt/els/config 2> /dev/null`
		if [ $? = 0 ] && [ ! -z "$LOCALE" ] && 
			[ ! -z "$FULL_LOC_SET" ] && [  ! -z "$LOCALE_SET" ]
		then
			FL=`echo $FULL_LOC_SET`
			LOCS=`echo $LOCALE_SET`
			if [ "$FL" = "$LOCS" ]
			then
				DOALL=YES
			else
				DOALL=NO
			fi
			L=`egrep "!$LOCALE" $MENU_DIR/menu.ls2 | sed -e"s/!$LOCALE::.*//" | sed -e"s/.*\/\///"`
			LANGUAGE=`echo $L | cut -f1 -d_`
			TERR=`echo $L | cut -f2 -d_`
			write_exit
		fi
	fi
fi

# Change defaults/input new values.
#

if [ "$KEYBOARD" = "" ] ; then
   unset KEYBOARD
   menu -f $MENU_DIR/menu.ls1 -o /tmp/lSkeyboard -r
   . /tmp/lSkeyboard
   rm /tmp/lSkeyboard
   export KEYBOARD
   if [ "$KEYBOARD" = "NONE" ]
   then
	defadm keyboard KEYBOARD=""
   else
   	defadm keyboard KEYBOARD="$KEYBOARD"
   fi
fi

menu -r -f $MENU_DIR/menu.ls2 -o /tmp/lScountry
. /tmp/lScountry
rm /tmp/lScountry
LOCALE=`echo $L | cut -f2 -d!`
LANGUAGE=`echo $L | cut -f1 -d! | cut -f1 -d_`
TERR=`echo $L | cut -f1 -d! | cut -f2 -d_`
# All input processed - leave.

write_exit
