#!/bin/sh

#set -x

LOG_FILE=disk0_install.log
LOCALE_DIR=/tmp/.locale_dir
HAVE_LOCALES=/tmp/.have_locales
rm ${LOCALE_DIR} > /dev/null 2>&1
rm ${HAVE_LOCALES} > /dev/null 2>&1

#
# where to put the output file that will eventually be read
# by the solaris wizard.
#
locale_file=$1

#
# private variables...
#
tmp_locale_file=${locale_file}.tmp

#
# file prep...
#
if [ -f ${locale_file} ]
then
	rm ${locale_file}
fi

if [ -f ${tmp_locale_file} ]
then
	rm ${tmp_locale_file}
fi

rm -f /tmp/*locales.out > /dev/null 2>&1

#
# extract_locales finds all the locale derivatives of $1
#
extract_locales()
{
	locid=$1
        mylocalefile=/tmp/${locid}locales.out
	#echo "extracting locales for $locid"
	my_tmp_localefile=${mylocalefile}.tmp
	if [ -f ${my_tmp_localefile} ]
	then
		rm ${my_tmp_localefile}
	fi

	cd $locale_dir

	locs=`ls -d ${locid} ${locid}.* | sort -u | sed 's/,/ /g'`
	#echo "locs = $locs"
	for locale in ${locs}
	do
		if [ -f ${locale}/locale_map ]
		then
	        	echo ${locale} >> ${my_tmp_localefile}
		fi
	done
	
	if [ -f ${my_tmp_localefile} ]
	then
	        lang_ids=`cat ${my_tmp_localefile} | sort -u`
	else
		lang_ids=""
	fi
	
	#
	# for each locale, get the locale description string
	# and write this info out to the locale file
	#
	/usr/bin/touch ${mylocalefile}
	if [ "X$lang_ids" != "X" ]
        then
	    for lang_id in ${lang_ids}
	    do
	        locale_desc_file=${locale_dir}/${lang_id}/locale_description
	        if [ -f ${locale_desc_file} ]
	        then
	            lang_desc=`cat ${locale_desc_file} | cut -f1`
	        else
	            lang_desc=${lang_id}
	        fi
		geo_file=${locale_dir}/${lang_id}/geo_map
		if [ -f ${geo_file} ]
		then
		    geos=`cat ${geo_file} | cut -d= -f2`
		    echo "${lang_id};${lang_desc};${geos}" >> ${mylocalefile}
		    #echo "geos for ${lang_id} = ${geos}" >>/tmp/$LOG_FILE
	        else
                    geo_file=${locale_dir}/${locid}/geo_map
		    if [ -f ${geo_file} ]
                    then
		        geos=`cat ${geo_file} | cut -d= -f2`
		        echo "${lang_id};${lang_desc};${geos}">> ${mylocalefile}
		        #echo "geos for ${lang_id} = ${geos}" >>/tmp/$LOG_FILE
                    else
		        echo "${lang_id};${lang_desc}" >> ${mylocalefile}
		        echo "NO geos for ${lang_id}" >>/tmp/$LOG_FILE
		   fi
		fi

	    done
	else
	        echo "#" >> ${mylocalefile}
	fi
	# cleanup
	if [ -f ${my_tmp_localefile} ]
	then
		rm ${my_tmp_localefile}
	fi
}


#
# Figure out which OS we have and set the locale directory info
# properly. Solaris 2.5.1 is a special case, the others have the
# same layout.
#
# $CD is used for debugging only. Otherwise, we should be using /cdrom.
#
base_dir=/cdrom
if [ ! -z "$CD" ]; then
	# use the environment variable
	echo "Using CD image: ${CD}"
	base_dir=$CD
fi

#
# if changes are made to logic below, see if checkSolImage in
# ProfileServerObject need similar changes.
#
if [ -d ${base_dir}/Solaris_2.5.1 ]; then
	platform=`/usr/bin/uname -p`
        product_dir=${base_dir}/Solaris_2.5.1
        locale_dir=${base_dir}/export/exec/${platform}.Solaris_2.5.1/lib/locale
else
	if [ -d ${base_dir}/Solaris_2.6 ]; then
		osdir=Solaris_2.6
	elif [ -d ${base_dir}/Solaris_2.7 ]; then
		osdir=Solaris_2.7
	elif [ -d ${base_dir}/Solaris_8 ]; then
		osdir=Solaris_8
	else
		# If we have no product dir, something isn't right.
		# Just output dummy file.
		echo "getInstallLangs: No Solaris Image found" >> /tmp/$LOG_FILE
		echo "#C;English" >> ${locale_file}
		exit 1

	fi
	product_dir=${base_dir}/${osdir}/Product
	locale_dir=${base_dir}/${osdir}/Tools/Boot/usr/lib/locale
fi

#
# save the locale dir for the system locale panel
#
echo "${locale_dir}" > ${LOCALE_DIR}


#
# Get the locales available on the image out of the .packagetoc
#
cd $product_dir
sunw_locs=`grep SUNW_LOC .packagetoc | cut -d= -f2 | sort -u | sed 's/,/ /g'`
#echo "sunw_locs = $sunw_locs"
for locale in ${sunw_locs}
do 
	echo $locale >> ${tmp_locale_file}
done

if [ -f ${tmp_locale_file} ]
then
	lang_ids=`cat ${tmp_locale_file} | sort -u`
fi
#echo "lang_ids = $lang_ids"

#
# for each locale on the image, get it's locale description string
# and write this info out to the locale file. Also create a file
# with all the locale derivatives for use by the systemlocale panel.
#
# It is actually important to at least create an empty file here,
# since the FileClient app stuff fail on a readOpen of a non-existent
# file.
#
/usr/bin/touch ${locale_file}
if [ "X$lang_ids" != "X" ]
then
        #
        # create have_locales for preinstall task to use to
        # deterimine if we need SoftwareLocalePanel
        #
        /usr/bin/touch ${HAVE_LOCALES}
	for lang_id in ${lang_ids}
	do
		locale_desc_file=${locale_dir}/${lang_id}/locale_description
		if [ -f ${locale_desc_file} ]
		then
			lang_desc=`cat ${locale_desc_file} | cut -f1`
		else
			lang_desc=${lang_id}
		fi
                geo_file=${locale_dir}/${lang_id}/geo_map
                if [ -f ${geo_file} ]
                then
			geos=`cat ${geo_file} | cut -d= -f2`
			echo "${lang_id};${lang_desc};${geos}" >> ${locale_file}
			#echo "geos for ${lang_id} = ${geos}" >>/tmp/$LOG_FILE
                else
			echo "${lang_id};${lang_desc}" >> ${locale_file}
			echo "NO geos for ${lang_id}" >>/tmp/$LOG_FILE
                fi
                extract_locales ${lang_id}
	done
else
	echo "#C;English" >> ${locale_file}
fi



if [ -f ${tmp_locale_file} ]
then
	rm ${tmp_locale_file}
fi
