#!/bin/ksh
#
# ###########################################################################
#
#   getLocale
#
#   Configuration for gaining locale for feedback messages
#   on a CD0 boot.
#
# ###########################################################################

### Where the locale info file will be copied to
copyLocale=$1

### SED
SED="/usr/bin/sed -e"

### DTDSPMG- Desktop Display Message
DTDSPMSG=/usr/dt/bin/dtdspmsg

### Columns settings for the selection list
LINES=6

### Type of CPU (i386/sparc)
CPU=`/usr/bin/uname -p`

### The choice list itself
typeset choiceList=""
typeset expansionList=""
typeset defaultLanguage=""
typeset descriptionNumber=""
typeset descriptionName=""
typeset currentLocale=""
typeset newLocaleName="" 

### Set up some directory variables
LOCALE_DIR=/webstart/lib/locale
MAIN_LOCALE_DIR=/usr/lib/locale

### Question for choice list
PS3="Select the language you want to use to run the installer: "

### Set up NLS Path to current locale settings for descriptions.
export NLSPATH=$LOCALE_DIR/%L/%N


# ##########################################################################
#
# Functions
#
# ##########################################################################


### L10N Message Translation Utility for names of locales.
DescriptionMessage() {
        ### Message() - display the locale description from getLocale.cat
        ### $1 - message id
        ### $2 - fallback message text

        if [ -x $DTDSPMSG ]; then
                $DTDSPMSG -s 1 getLocale.cat $1 "$2"
        else
                echo "$2"
        fi
}

### L10N Message Translation Utility for reading message sentence.
Message() {
	### Message() - display message from getLocale.cat
	### $1 - message id
	### $2 - fallback message text
	### $3 - format variable to replace in string

	if [ -x $DTDSPMSG ]; then
		$DTDSPMSG -s 2 getLocale.cat $1 "$2" "$3"
	else
		echo "$2"
	fi
}



### Expand the country codes into real strings
NameExpansion() {
	#  $1 The country code
	#  Also substitute any space with an '_' so that
	#  select wont make too many undesirable entries

	#  If the directory does not exist, then there is no support
	#  for that locale in tty mode....
	if [ -d $LOCALE_DIR/$1 ]; then
		if [ -e $LOCALE_DIR/$1/description ]; then
			echo `cat $LOCALE_DIR/$1/description` | read descriptionNumber descriptionName	
			echo `DescriptionMessage $descriptionNumber "$descriptionName"` | $SED 's: :_:g'
		fi
	fi
}

### WriteLocale
### Create the locale environment variables to a file 
### as well as set them ourselves.
WriteLocale() {
	typeset newCC=""

	### Get the disk mini-root's boot locale name

	if [ -e $LOCALE_DIR/$1/localeName ]; then
	  echo `cat $LOCALE_DIR/$1/localeName` | read newLocaleName
	else 
	  newLocaleName=$1;
	fi
	
	### Use a locale_map if present

	if [ -e $MAIN_LOCALE_DIR/$newLocaleName/locale_map ]; then
		### Move the description file to a location to
 		### be read from external programs.
		/usr/bin/cp $MAIN_LOCALE_DIR/$newLocaleName/locale_map $2
	else
		### Write out the file to source
		echo "LC_COLLATE=$newLocaleName" > $2
		echo "LC_CTYPE=$newLocaleName" >> $2
		echo "LC_MESSAGES=$newLocaleName" >> $2
		echo "LC_MONETARY=$newLocaleName" >> $2
		echo "LC_NUMERIC=$newLocaleName" >> $2
		echo "LC_TIME=$newLocaleName" >> $2
	fi
	
	### Set the NLSPATH
	echo "NLSPATH=$LOCALE_DIR/%L/%N:$NLSPATH" >> $2
	
	### Set the locale directory for the messages
	echo "BOOT_LOCALE_DIR=$LOCALE_DIR" >> $2
	
	### See if we are multi-byte (need dtterm for text)
	if [ -a $LOCALE_DIR/$1/useXserver ]; then
		echo "MULTI=YES; export MULTI" >> $2
	else
		echo "MULTI=NO; export MULTI" >> $2
	fi
	
	### Export all these vars to be used elsewhere
	echo "export LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME LANG" >> $2
	echo "export BOOT_LOCALE_DIR NLSPATH" >> $2
	
}





# ##########################################################################
#
# Main  Program
#
# ##########################################################################

### Check for the file argument.
if [ -z $copyLocale ]; then
	echo "No file argument given. Requires a file to write locale info to."
	exit 0
fi


### Setup what the default language is:
if [ ! -z $LC_ALL ]; then
   currentLocale=$LC_ALL
elif [ ! -z $LC_MESSAGES ]; then
   currentLocale=$LC_MESSAGES
elif [ ! -z $LANG ]; then
   currentLocale=$LANG
else
   currentLocale="C"
   export LANG=$currentLocale
fi

if [ -e $LOCALE_DIR/$currentLocale/description ]; then
    echo `cat $LOCALE_DIR/$currentLocale/description` | read descriptionNumber defaultLanguage
else
    currentLocale="C"
    defaultLanguage="English"
fi

typeset -i numLocales=0

### Read the directory /webstart/locale to come up
### with available locales.
for i in $(ls $LOCALE_DIR)
	do
	  ### Only present an install message language if this scripts
	  ### getLocale message catalog is present for that language
	  
	  if [ -f $LOCALE_DIR/$i/getLocale.cat ];then
		numLocales=$numLocales+1
		### Append each country code to the main choice list
		choiceList="$choiceList $i"

		### Expand the name to its real form
		expandedName=`NameExpansion $i`
		expansionList="$expansionList $expandedName"
	  fi
done

### If there are more languages available then just the default 
### language then put up a language choice list

echo ""

if [ $numLocales -gt 1 ];then
  Message 1 "The Solaris Installer can be run in $defaultLanguage, \
or any of the following languages:" $defaultLanguage

  echo ""

  ### Set up the choice list
  select choice in $expansionList ;do
	if [ ! -z $choice ];then
		if [ $choice = "Quit" ];then
			exit 0
		fi

		### Find the correct listing in the arrays
		typeset -i cnt=1
		for i in $expansionList;do
			if [ $choice = $i ];then
				break
			else
				cnt=$cnt+1
			fi
		done

		typeset -i ncnt=1
		for i in $choiceList; do
			if [ $ncnt -eq $cnt ];then
				WriteLocale $i $copyLocale
				exit 0
			else
				ncnt=$ncnt+1
			fi
		done
	fi
  done
else
  ###
  ### Only one locale (the current default) available, 
  ### copy its locale map to the supplied target
  ###
  WriteLocale $currentLocale $copyLocale
  exit 0
fi
