#!/usr/bin/ksh
#
# %W% %E% 
#
###########################################################################
#                                                                         #
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.             #
# Use is subject to license terms.                                        #
#                                                                         #
###########################################################################
#####################    programs, files   ################################
###########################################################################

grep=/usr/xpg4/bin/grep
awk=/usr/xpg4/bin/awk
expr=/usr/bin/expr
tee=/usr/bin/tee
gettext=/usr/bin/gettext

###########################################################################
###################    print localized messages   #########################
###########################################################################
# 
# $1 - string to print
# $2 - logging level: either "log", "logvb" or "nolog"
# $3 - argument 1 (if string needs it)
# $4 - argument 2 (if string needs it)

loc_prnt()
{

typeset msg=`$gettext --domain=localeadm "$1"`

typeset log=$2 arg1=$3 arg2=$4 

#echo "log is $log"
if [ "$log" = "logvb" ]  #write to log always, user only in verbose mode
then
  if [ "$VERBOSE" = "y" ]
  then
	if [ $# -le 3 ]
	then
	    printf "$msg" "$arg1"| $tee -a $LOGFILE
	    echo | $tee -a $LOGFILE
	else
	    printf "$msg" "$arg1" "$arg2"| $tee -a $LOGFILE
	    echo | $tee -a $LOGFILE
	fi
   else  # no verbose
	if [ $# -le 3 ]
	then
	    printf "$msg" "$arg1" >> $LOGFILE
	    echo >> $LOGFILE
	else
	    printf "$msg" "$arg1" "$arg2" >> $LOGFILE
	    echo >> $LOGFILE
	fi
    fi
elif [ "$log" = "log" ]  # write to log & user always
then
    if [ $# -le 3 ]
    then
	printf "$msg" "$arg1"| $tee -a $LOGFILE
	echo | $tee -a $LOGFILE
    else
	printf "$msg" "$arg1" "$arg2"| $tee -a $LOGFILE
	echo | $tee -a $LOGFILE
    fi
else  # never log, just send to user
    if [ $# -le 3 ]
    then
	printf "$msg" "$arg1"
	echo
    else
	printf "$msg" "$arg1" "$arg2"
	echo
    fi
fi    

}

###########################################################################
########################    print help   ##################################
###########################################################################

print_help() {
    loc_prnt "usage: \nlocaleadm [-lcs] [-q locale|region] [-d device [,device2..]] [-t] [-R root_path]" nolog
    echo "localeadm [-a] [-r] locale|region [-m] [-d device [,device2..]] [-t] [-R root_path]"
    echo
    loc_prnt "\t-a  Add the packages for <locale> or <region> to the system" nolog
    loc_prnt "\t-c  Display the locales by codeset name. Use with -l" nolog
    loc_prnt "\t-d  Select the device to add or list locales/regions from" nolog
    loc_prnt "\t-h  Print this help screen." nolog
    loc_prnt "\t-l  List the locales installed on the system or available from a device <device> (if used with -d)" nolog
    loc_prnt "\t-m  Add/remove message support for a locale/region" nolog
    loc_prnt "\t-q  Query to see is <locale> or <region> installed on the system" nolog
    loc_prnt "\t-r  Remove the packages for <locale> or <region> from the system" nolog
    loc_prnt "\t-R  Add, remove, list or query locales/regions installed on root_path" nolog
    loc_prnt "\t-s  Short listing. Only lists region names. Use with -l" nolog
    loc_prnt "\t-t  Test mode. Simulates add or delete, but makes no changes to the system" nolog
    loc_prnt "\t-v  Verbose mode. Gives more detail of operations performed" nolog
    echo 
    echo " e.g. localeadm -l"
    loc_prnt "\t- lists all the regions & locales installed on the system by name" nolog
    echo "  localeadm -lc -d /cdrom/cdrom0"
    loc_prnt "\t- lists all regions available from /cdrom/cdrom0 by codeset" nolog
    echo "  localeadm -a neu -m -d /images/S10"
    loc_prnt "\t- adds the Northern Europe region without message support from /images/S10" nolog
    echo
    loc_prnt "See the localeadm(1M) man page for more details" nolog
    echo 
}

###########################################################################
#############  print the locales available for install  ###################
###########################################################################
#
# $1 - the invalid argument

print_help_msg() {
    echo
    loc_prnt "Invalid argument: %s" nolog $1
    loc_prnt "Please run 'localeadm -h' for a list of valid arguments" nolog 
    echo
    rem_lock
    exit 1
}
    
###########################################################################
#############  print the locales available for install  ###################
###########################################################################

print_aua() {
    echo "$1 (aua)"
    loc_prnt "[ Australia, New Zealand ]" nolog
    echo
}

print_cam() {
#    loc_prnt "$1 (%s)" nolog cam
    echo "$1 (cam)"
    loc_prnt "[ Costa Rica, Guatemala, Nicaragua, Panama, El Salvador ]" nolog
    echo
}

print_ceu() {
#    loc_prnt "$1 (%s)" nolog ceu
    echo "$1 (ceu)"
    loc_prnt "[ Austria, Czech Republic, Germany, Hungary, Poland, Slovakia, Switzerland (German), Switzerland (French) ]" nolog
    echo
}

print_eeu() {
    echo "$1 (eeu)"
    loc_prnt "[ Albania, Bosnia, Bulgaria, Estonia, Croatia, Lithuania, Latvia, Macedonia, Romania, Russia, Serbia, Slovenia, Turkey ]" nolog
    echo
}

print_mea() {
    echo "$1 (mea)"
    loc_prnt "[ Saudi Arabia, Israel ]" nolog
    echo
}

print_naf() {
    echo "$1 (naf)"
    loc_prnt "[ Egypt ]" nolog
    echo
}

print_nam() {
    echo "$1 (nam)"
    loc_prnt "[ Canada (English), Canada (French), United States, Mexico ]" nolog
    echo
}

print_neu() {
    echo "$1 (neu)"
    loc_prnt "[ Denmark, Finland, Iceland, Norway (Bokmal), Norway (Nyorsk),  Sweden ]" nolog
    echo
}

print_sam() {
    echo "$1 (sam)"
    loc_prnt "[ Argentina, Bolivia, Brazil, Chile, Columbia, Ecuador, Paraguay, Peru, Uraguay, Venezuela ]" nolog
    echo
}


print_seu() {
    echo "$1 (seu)"
    loc_prnt "[ Italy, Greece, Portugal, Spain (Catalan), Spain (Spanish) ]" nolog
    echo
}

print_weu() {
    echo "$1 (weu)"
    loc_prnt "[France, Holland, Belgium (French), Belgium (Flemish), Ireland, England ]" nolog
    echo
}

print_zh_tw() { 
    echo "$1 (zh_tw)" 
    echo
}

print_zh_hk() {
    echo "$1 (zh_hk)" 
    echo
}

print_zh_cn() {
    echo "$1 (zh_cn)" 
    echo
}

print_ja() {
    echo "$1 (ja)" 
    echo
}

print_ko() {
    echo "$1 (ko)" 
    echo
}

print_th_th() {
    echo "$1 (th_th)" 
    echo
}

print_hi_in() {
    echo "$1 (hi_in)" 
    echo
}


###########################################################################
#############  print the codesets available for install  ##################
###########################################################################

cprint_aua() {
    echo "$1 (aua)" 
    echo "[ en_AU.ISO8859-1, en_NZ.ISO8859-1 ]"
    echo
}

cprint_cam() {
    echo "$1 (cam)" 
    echo "[ es_CR.ISO8859-1, es_GT.ISO8859-1, es_PA.ISO8859-1, es_SV.ISO8859-1 ]"
    echo
}

cprint_ceu() {
    echo "$1 (ceu)" 
    echo "[ cs_CZ.ISO8859-2, de_AT.ISO8859-1, de_AT.ISO8859-15, de_CH.ISO8859-1, de_DE.ISO8859-1, de_DE.ISO8859-15, de_DE.UTF-8, fr_CH.ISO8859-1, hu_HU.ISO8859-2, pl_PL.ISO8859-2, pl_PL.UTF-8, sk_SK.ISO8859-2 ]"
    echo
}

cprint_eeu() {
    echo "$1 (eeu)" 
    echo "[ bg_BG.ISO8859-5, et_EE.ISO8859-15, hr_HR.ISO8859-2, lt_LT.ISO8859-13, lv_LV.ISO8859-13, mk_MK.ISO8859-5, ro_RO.ISO8859-2, ru_RU.ISO8859-5, ru_RU.ANSI1251, ru_RU.KOI8-R, ru_RU.UTF-8, sh_BA.ISO8859-2, sl_SI.ISO8859-2, sq_AL.ISO8859-2, sr_YU.ISO8859-5, tr_TR.ISO8859-9 ]"
    echo
}

cprint_mea() {
    echo "$1 (mea)" 
    echo "[ ar_SA.UTF-8, he, he_IL.UTF-8 ]"
    echo
}

cprint_naf() {
    echo "$1 (naf)" 
    echo "[ ar, ar_EG.UTF-8 ]"
    echo
}

cprint_nam() {
    echo "$1 (nam)" 
    echo "[ en_CA.ISO8859-1, en_US.ISO8859-1, en_US.ISO8859-15, en_US.UTF-8, es_MX.ISO8859-1, fr_CA.ISO8859-1 ]"
    echo
}


cprint_neu() {
    echo "$1 (neu)" 
    echo "[ da_DK.ISO8859-1, da_DK.ISO8859-15, fi_FI.ISO8859-1, fi_FI.ISO8859-15, fi_FI.UTF-8, is_IS.ISO8859-1, no_NO.ISO8859-1@bokmaal, no_NO.ISO8859-1@nyorsk, sv_SE.ISO8859-1, sv_SE.ISO8859-15, sv_SE.UTF-8 ]"
    echo
}

cprint_sam() {
    echo "$1 (sam)" 
    echo "[ es_AR.ISO8859-1, es_BO.ISO8859-1, es_CL.ISO8859-1, es_CO.ISO8859-1, es_EC.ISO8859-1, es_PE.ISO8859-1, es_PY.ISO8859-1, es_UY.ISO8859-1, es_VE.ISO8859-1, pt_BR.ISO8859-1, pt_BR.UTF-8 ]"
    echo
}


cprint_seu() {
    echo "$1 (seu)" 
    echo "[ ca_ES.ISO8859-1, ca_ES.ISO8859-15, el_GR.ISO8859-7, el_GR.UTF-8, es_ES.ISO8859-1, es_ES.ISO8859-15, es_ES.UTF-8, it_IT.ISO8859-1, it_IT.ISO8859-15, it_IT.UTF-8, pt_PT.ISO8859-1, pt_PT.ISO8859-15, pt_PT.UTF-8 ]"
    echo
}

cprint_weu() {
    echo "$1 (weu)" 
    echo "[fr_FR.ISO8859-1, fr_FR.ISO8859-15, fr_FR.UTF-8, fr_BE.ISO8859-1, fr_BE.ISO8859-15, fr_BE.UTF-8, nl_BE.UTF-8, nl_NL.ISO8859-1, nl_NL.ISO8859-15, nl_NL.UTF-8, en_IE.ISO8859-1, en_IE.ISO8859-15, en_GB.ISO8859-1, en_GB.ISO8859-15] "
    echo
}

cprint_zh_tw() {
    echo "$1 (zh_tw)" 
    echo "[ zh_TW, zh_TW.EUC, zh_TW.BIG5, zh_TW.UTF-8 ]" 
    echo
}

cprint_zh_hk() {
    echo "$1 (zh_hk)" 
    echo "[ zh_HK.BIG5HK, zh_HK.UTF-8 ]" 
    echo
}

cprint_zh_cn() {
    echo "$1 (zh-cn)" 
    echo "[ zh_CN.EUC, zh, zh_CN.GB18030, zh_CN.GBK, zh.GBK, zh_CN.UTF-8, zh.UTF-8 ]" 
    echo
}

cprint_ja() {
    echo "$1 (ja)" 
    echo "[ ja, ja_JP.eucJP, ja_JP.EUC, ja_JP.PCK, ja_JP.UTF-8 ]" 
    echo
}

cprint_ko() {
    echo "$1 (ko)" 
    echo "[ ko, ko_KR.EUC, ko.UTF-8, ko_KR.UTF-8 ]" 
    echo
}

cprint_th_th() {
    echo "$1 (th_th)" 
    echo "[ th, th_TH, th_TH.TIS620, th_TH.ISO8859-11, th_TH.UTF-8 ]" 
    echo
}

cprint_hi_in() {
    echo "$1 (hi_in)" 
    echo "[ hi_IN.UTF-8 ]" 
    echo
}

###########################################################################
###############   Selects the region name to print out  ###################
###########################################################################

get_region_name() {

case $1 in
 aua)
    SEL_STR=`$gettext --domain=localeadm "Australasia"`
    ;;
 cam)
    SEL_STR=`/usr/bin/gettext --domain=localeadm "Central America"`
    ;;
 ceu)
    SEL_STR=`$gettext --domain=localeadm "Central Europe"`
    ;;
 eeu)
    SEL_STR=`$gettext --domain=localeadm "Eastern Europe"`
    ;;
 mea)
    SEL_STR=`$gettext --domain=localeadm "Middle East"`
    ;;
 naf)
    SEL_STR=`$gettext --domain=localeadm "Northern Africa"`
    ;;
 nam)
    SEL_STR=`$gettext --domain=localeadm "North America"`
    ;;
 neu)
    SEL_STR=`$gettext --domain=localeadm "Northern Europe"`
    ;;
 sam)
    SEL_STR=`$gettext --domain=localeadm "South America"`
    ;;
 seu)
    SEL_STR=`$gettext --domain=localeadm "Southern Europe"`
    ;;
 weu)
    SEL_STR=`$gettext --domain=localeadm "Western Europe"`    
    ;;
 zh_tw)
    SEL_STR=`$gettext --domain=localeadm "Traditional Chinese"`
     ;;
 zh_hk)
    SEL_STR=`$gettext --domain=localeadm "Traditional Chinese (Hong Kong)"`
    ;;
 zh_cn)
    SEL_STR=`$gettext --domain=localeadm "Simplified Chinese"`
    ;;
 ja)
    SEL_STR=`$gettext --domain=localeadm "Japanese"`
    ;;
 ko)
    SEL_STR=`$gettext --domain=localeadm "Korean"`
    ;;
 th_th)
    SEL_STR=`$gettext --domain=localeadm "Thai"`
    ;;
 hi_in)
    SEL_STR=`$gettext --domain=localeadm "Hindi"`
    ;;
 *)
    SEL_STR="Locale"
    ;;
esac
if [ "$SEL_STR" = "Locale" ]
then
    return 1 # no correct region found
fi
}


###########################################################################
############  prints the locales currently on the system  #################
###########################################################################
#
# $1 = quiet mode - use for adds/removes if the user hasn't run a listing before. 
# $2 = test mode

print_system(){

typeset -i result=0 keep_list=0 core=0 langcd=0 disks=0

typeset date=`date`

if [[ "$ALTROOT" = "y" || "$TEST" = "y"  || "$3" = "all" ]]
then
    typeset -i keep_list=1
fi

# if we have a list and aren't running in test mode, create a backup
if [[ -f $LOCALES_LIST &&  keep_list -ne 1 ]]
then
    cp $LOCALES_LIST $LOCALES_LIST.bak
    rm $LOCALES_LIST
fi

if [ "$1" != "q" ] # check for silent running - no echoes
then
    loc_prnt "Checking for installed packages. This could take a while." nolog
    echo
fi

if [ $keep_list -eq 1 ] # don't want to overwrite the actual list when we're running test mode
then
    cp $LOCALES_LIST $LOCALES_LIST.true
    rm $LOCALES_LIST
fi

if [[ "$TEST" != "y" || "$3" != "all" ]]
then
    loc_prnt "The following regions are installed on %s on %s" nolog ${HOST} "${date}" > $LOCALES_LIST
    echo >> $LOCALES_LIST
    echo "POSIX (C)" >> $LOCALES_LIST
    echo >> $LOCALES_LIST
fi

for region in aua cam ceu eeu mea naf nam neu sam seu weu ja ko zh_cn zh_hk zh_tw th_th hi_in
do
    result=0
    core=0
    get_region_name $region
#    loc_prnt "checking for $region...." nolog
    if [ "$2" = "y" ] # test mode, don't need to check anything
    then	
	result=8
    else 
	# first, check for core pkgs
	if [ $ALTROOT = "y" ]
	then
	    check_locale $region "alt" "of2c"
	else
	    if [ "$3" = "all" ]
	    then
		check_locale $region "disk" "of2c"
	    else
		check_locale $region "mach" "of2c"
	    fi
	fi
	core=`expr $core + $?`
	#core=$result

	if [ $core -eq 2 ] # if we have at least a core install
	then
	    for disk in 1of2 2of2
	    do
		if [ "$1" = "q" ]
		then
		    if [ $ALTROOT = "y" ]
		    then
			check_locale $region "alt" "$disk" 2>&1 > /dev/null
		    else
			check_locale $region "mach" "$disk" 2>&1 > /dev/null
		    fi
		    result=`expr $result + $?`
		else
		    if [ $ALTROOT = "y" ]
		    then
			check_locale $region "alt" "$disk"
		    else
			if [ "$3" = "all" ]
			then
			    check_locale $region "disk" "$disk"
			else
			    check_locale $region "mach" "$disk"
			fi
		    fi
		    result=`expr $result + $?`
		fi
	    done
	fi # end "if result=1"

	if [ $result -ge 3 ] # we have at least a core and one other
	then
	    if [ "$1" = "q" ]
		then
		    if [ $ALTROOT = "y" ]
		    then
			check_locale $region "alt" "langcd1" 2>&1 > /dev/null
		    else
			check_locale $region "mach" "langcd1" 2>&1 > /dev/null
		    fi
		    langcd=`expr $langcd + $?`

		    if [[ $region = "ceu" || "$region" = "seu" || $region = "nam" ]]
		    then
			if [ $ALTROOT = "y" ]
			then
			    check_locale $region "alt" langcd2 2>&1 > /dev/null
			else
			    check_locale $region "mach" langcd2 2>&1 > /dev/null
			fi
			langcd=`expr $langcd + $?`
		    fi
		else
		    if [ $ALTROOT = "y" ]
		    then
			check_locale $region "alt" "langcd1"
		    else
			if [ "$3" = "all" ]
			then
			    check_locale $region "disk" "langcd1"
			else
			    check_locale $region "mach" "langcd1"
			fi
		    fi
		    langcd=`expr $langcd + $?`

		    if [[ $region = "ceu" || "$region" = "seu" || $region = "nam" ]]
		    then
			if [ $ALTROOT = "y" ]
			then
			    check_locale $region "alt" langcd2
			else
			    check_locale $region "mach" langcd2
			fi
			langcd=`expr $langcd + $?`
		    fi
		fi
	fi # end "if result=3"
    fi # end "if $2='y'" (if "test mode")

    if [[ $result -eq 4 && $core -eq 2 && langcd -ge 2 ]] # if we have a messages as well
    then
	case $1 in
	locale)
	    print_$region "${SEL_STR}" >> $LOCALES_LIST
	    ;;
	code)
	    cprint_$region "${SEL_STR}" >> $LOCALES_LIST
	    ;;
	short)
	    echo | $tee -a $LOCALES_LIST
	    echo "$SEL_STR ($region)" | $tee -a $LOCALES_LIST
	    echo | $tee -a $LOCALES_LIST
	    ;;
	q)
	    echo >> $LOCALES_LIST
	    echo "$SEL_STR ($region)" >> $LOCALES_LIST
	    echo >> $LOCALES_LIST
	    ;;
	esac
    elif [[ $result -eq 4 && $core -eq 2 ]] # no messages but a 1of2 and 2of2
    then
	case $1 in
	locale)
	    print_$region "${SEL_STR} (no messages)" >> $LOCALES_LIST
	    ;;
	code)
	    cprint_$region "${SEL_STR} (no messages)" >> $LOCALES_LIST
	    ;;
	short)
	    echo | $tee -a $LOCALES_LIST
	    echo "$SEL_STR (no messages) ($region)" | $tee -a $LOCALES_LIST
	    echo | $tee -a $LOCALES_LIST
	    ;;
	q)
	    echo >> $LOCALES_LIST
	    echo "$SEL_STR (no messages) ($region)" >> $LOCALES_LIST
	    echo >> $LOCALES_LIST
	    ;;
	esac
    elif [ $core -eq 2 ] # a core install
    then
	case $1 in
	locale)
	    print_$region "${SEL_STR} (core install)" >> $LOCALES_LIST
	    ;;
	code)
	    cprint_$region "${SEL_STR} (core install)" >> $LOCALES_LIST
	    ;;
	short)
	    echo | $tee -a $LOCALES_LIST
	    echo "$SEL_STR (core install) ($region)" | $tee -a $LOCALES_LIST
	    echo | $tee -a $LOCALES_LIST
	    ;;
	q)
	    echo >> $LOCALES_LIST
	    echo "$SEL_STR (core install) ($region)" >> $LOCALES_LIST
	    echo >> $LOCALES_LIST
	    ;;
	esac
    fi

done # end "for i in aua....."

echo
echo "-----------------"
echo
more $LOCALES_LIST

if [ "$1" != "q" ] # check for silent running - no echoes
then	
    loc_prnt "Done." nolog
    if [ "$TEST" != "y" ]
    then
	loc_prnt "A copy of this report can be found in %s" nolog $LOCALES_LIST
    fi
fi

if [ -f $LOCALES_LIST.true ]
then
    mv $LOCALES_LIST.true $LOCALES_LIST
fi

# the program needs a locales.list file, even if it's empty. So, if there are no 
# locales on the system, we still need the file

if [ ! -f $LOCALES_LIST ]
then
    touch $LOCALES_LIST
fi 
}

###########################################################################
##################   check for an installed locale   ######################
###########################################################################
#
# $1 = region to check (e.g. mea)
# $2 = type to check - a machine (mach), directory (dir) or altroot (alt)
# $3 = disk type to check Locale_config.txt for (i.e. 1of2, 2of2, langcd)
# $4 = disk to check (if needed)

check_locale(){
typeset -l region
region=$1
typeset -i count=0 pkg_count=0 num=2 
typeset check_disk=$3
typeset disk=$4
 
if [ "$VERBOSE" = "y" ]
then
    loc_prnt "Checking for %s region (%s) (verbose mode)" logvb "$SEL_STR" "$region" 
    loc_prnt "(%s packages)" log $check_disk
else
    loc_prnt "Checking for %s region (%s)" log "$SEL_STR" "$region" 
    loc_prnt "(%s packages)" log $check_disk
fi
    
if [ "$VERBOSE" != "y" ]
then
    print -n "|"
fi

for pkg in `$awk '$2 ~ /'$region'/ && $3 ~ /'$check_disk'/ {print $1}' $CONFIGFILE`
do
    if [ "$2" = "mach" ] # if we're checking a machine for installed locales
    then
	pkginfo -q $pkg
    elif [ "$2" = "alt" ]
    then
	pkginfo -R $ALT_ROOT_DIR -q $pkg
    elif [ "$2" = "dir" ] # if we're checking a dir for the pkgs for a locale
    then
	if [ -f $disk/.order ] # if we have a 1of2 or DVD we don't need to check individual pkgs
	then
	    $grep -q $pkg $disk/.order
	else
	    find $disk -type d -name $pkg -prune > /dev/null # all we want is the return value
	fi
    fi
    if [ "$?" = "0" ]
    then
	count=`expr $count + 1`
	if [ "$VERBOSE" = "y" ]
	then
	    loc_prnt "package %s found" logvb $pkg
	else
	    print -n "."
	fi
    else  
	if [ "$VERBOSE" = "y" ]
	then
	    loc_prnt "package %s not found" logvb $pkg
	else
	    print -n "."   
	fi
    fi
done

#loc_prnt 
if [ "$VERBOSE" != "y" ]
then
    print -n "|"
fi
echo
echo

pkg_count=`$awk '$2 ~ /'$region'/ && $3 ~ /'$check_disk'/ {print $1}' $CONFIGFILE | wc -l`

if [ $count -eq $pkg_count ]
then
    echo "All packages found."
    echo
    return 2 # we have all of the packages
elif [ $count -gt 0 ]
then
    echo "Not all packages found ($count out of $pkg_count)."
    echo
    return 1 #we have some packages, but not all of them
else
    echo "No packages found."
    echo
    return 0 # we don't have any packages
fi
}

###########################################################################
###############   check for a locale or a region   ########################
###########################################################################
#
# $1 - locale name
# $2 - what type of operation we're doing - install, remove, check or fix

check_region() {

typeset -l locale SEL_REGION
locale=${1%%.*} # strip off the ".iso8859" etc, just want aa_BB

#echo "DEBUG: locale is $locale"
if [ "$locale" = "ar" ]
then
    locale="ar_eg"
fi

# find out if it's a unique locale name (partials give problems e.g es_AR vs. es_ES)
typeset -i loc_count=`$grep locname $CONFIGFILE | sed -e 's/locname//g' | $grep $locale | wc -l`

if [ $loc_count -ne 1 ]
then
    loc_prnt "Warning! The name you have given (\"%s\") is not a unique locale or region identifier. Please choose another locale name and try again. " nolog $locale
    echo
    rem_lock
    exit 1
fi

echo
#loc_prnt "locale/region name is %s" log $locale
get_region_name $locale

if [ "$SEL_STR" = "Locale" ] # a locale name was entered not a region name
then
    if [ "$locale" = "ar_eg" ]
    then
	SEL_REGION="naf"
    elif [ "$locale" = "he" ]
    then
	SEL_REGION="mea"
    else
	SEL_REGION=`$grep locname $CONFIGFILE | $grep $locale | $awk '{print $3}'`
    fi

    #echo "SEL_REGION is $SEL_REGION"

    get_region_name $SEL_REGION
    echo
    if [ "$2" = "install" ]
    then
	loc_prnt "Warning! You have chosen the %s locale rather than a region to install." nolog "$1" 
	loc_prnt "This locale is part of the %s region. If you choose to continue, all of the locales in this region will be installed." nolog  "$SEL_STR"
    elif [ "$2" = "uninstall" ]
    then 
	loc_prnt "Warning! You have chosen the %s locale rather than a region to remove." nolog "$1" 
	loc_prnt "This locale is part of the %s region. If you choose to continue, all of the locales in this region will be removed."  nolog "$SEL_STR"
    elif [ "$2" = "check" ]
    then
	loc_prnt "Warning! You have chosen the %s locale rather than a region to check." nolog "$1" 
	loc_prnt "This locale is part of the %s region. If you choose to continue, this region will be checked."  nolog "$SEL_STR"
    
    elif [ "$2" = "fix" ]
    then
	loc_prnt "Warning! You have chosen the %s locale rather than a region to fix." nolog "$1" 
	loc_prnt "This locale is part of the %s region. If you choose to continue, this region will be fixed."  nolog "$SEL_STR"
    fi
    echo 
    typeset -l ans
    ans_str=`$gettext --domain=localeadm "Do you wish to continue? [y/n]: "`
    read ans?"${ans_str}"

    if [ "$ans" != "y" ]
    then    
	rem_lock
	exit 1
    fi
fi
}

###########################################################################
###################   check for an image type   ###########################
###########################################################################
#
# $1 - which cd to check for - 1of2, 2of2, langcd1 or langcd2
# $2 - locale to check for

check_image() {
typeset -l image=$1 lang=$2
typeset -i count=0 pkg_count=0 num=2 check_count=0

check_dir=$INST_DIR

if [ "$lang" = "all" ]
then
    # get a list of all the 1of2 pkgs (use sort -u so we don't get dupes)
    pkg_count=`$awk '$3 ~ /'$image'/ {print $1}' $CONFIGFILE |sort -u| wc -l`
else
    #just get the pkgs needed for $lang
    pkg_count=`$awk '$3 ~ /'$image'/ && $2 ~ /'$lang'/ {print $1}' $CONFIGFILE | wc -l`
fi
#echo "package count for $image ($lang) should be $pkg_count" 

if [ $pkg_count -eq 0 ] # we don't need this image for this region
then
    return 2
fi
# if we still want to get stuff from this image....
if [ "$VERBOSE" != "y" ]
then
    print -n "|"
fi

if [ "$lang" = "all" ]
then
    # run through the pkg list for each CD for the region wanted
    for pkg in `$awk '$3 ~ /'$image'/ {print $1}' $CONFIGFILE |sort -u`
    do
	if [ -f $INST_DIR/.order ]
	then
	    $grep -q ${pkg} $INST_DIR/.order
	else	
	     find $check_dir -type d -name ${pkg} -prune >/dev/null
	fi
	if [ "$?" = "0" ]
	then
	    count=`expr $count + 1`
	    if [ "$VERBOSE" = "y" ]
	    then
		loc_prnt "package %s found" logvb $pkg
	    else
		print -n "."
	    fi
	    else 
	    if [ "$VERBOSE" = "y" ]
	    then
		loc_prnt "package %s not found" logvb $pkg
	    else
		print -n "."   
	    fi
	fi
    done
    if [ -f /usr/sadm/lib/localeadm/pkgs_list.txt ]
    then
	rm /usr/sadm/lib/localeadm/pkgs_list.txt
    fi

else
    # run through the pkg list for each CD for the region wanted
    for pkg in `$awk '$3 ~ /'$image'/ && $2 ~ /'$lang'/ {print $1}' $CONFIGFILE`
    do
	ls $INST_DIR | $grep -q $pkg

	if [ "$?" = "0" ]
	then
	    count=`expr $count + 1`
	    if [ "$VERBOSE" = "y" ]
	    then
		loc_prnt "package %s found" logvb $pkg
	    else
		print -n "."
	    fi
	    else 
	    if [ "$VERBOSE" = "y" ]
	    then
		loc_prnt "package %s not found" logvb $pkg
	    else
		print -n "."   
	    fi
	fi
    done
fi

if [ "$VERBOSE" != "y" ]
then
    print -n "|"
fi

echo

if [ $count -eq $pkg_count ]
then
    #echo "DEBUG: returning 1"
    return 1
else
    #echo "DEBUG: returning 0"
    return 0
fi

}

###########################################################################
###################    add locale function      ###########################
###########################################################################
#
# $1 - region to add
# $2 - test mode

add_locales() {

#SEL_REGION=$1

typeset -i arr_index=0 rcount=0 daemons=0 ERROR=0

INSTALL_DIR=""

#get_install_dir "$DEVICES" $SEL_REGION

if [ "$2" = "y" ]
then
    loc_prnt "Adding packages for %s (%s)  - Test Mode" log "$SEL_STR" "$SEL_REGION"
else 
    loc_prnt "Adding packages for %s (%s)" log "$SEL_STR" "$SEL_REGION"
fi
echo

# first check if the locale is already installed
rcount=`$grep -c $SEL_REGION $LOCALES_LIST`

# start with 1of2, then 2of2, then langcd.

if [ $MESSAGES -eq 1 ] # if we only want messages, or we don't want messages
then
    if [ $rcount -gt 0 ] # locale was installed without msgs
    then # we only need the langcd
	if [[ "$SEL_REGION" = "ceu" || "$SEL_REGION" = "seu" || $SEL_REGION = "nam" ]]
	then
	    forlist="langcd1 langcd2"
	else
	    forlist="langcd1"
	fi
	loc_prnt "Region %s's message packages will be installed." log "$SEL_REGION"
    else # we want everything but the langcd
	forlist="1of2 2of2"
	loc_prnt "Region %s will be installed without messages" log "$SEL_REGION"
    fi
else
    if [ $rcount -gt 0 ] 
    then 
	loc_prnt "Region %s is already installed on this machine. Exiting" nolog "$SEL_REGION"
	echo
	return 
    else
	if [[ "$SEL_REGION" = "ceu" || "$SEL_REGION" = "seu" || $SEL_REGION = "nam" ]]
	then
	    forlist="1of2 2of2 langcd1 langcd2"
	else
	    forlist="1of2 2of2 langcd1"
	fi
	loc_prnt "Region %s will be installed." log "$SEL_REGION"
    fi
fi

if [ "$2" != "y" ] # don't need to record stuff if we're just doing test mode
then
    echo "Install_Dir=${ONE_OF_TWO_IMG} ${TWO_OF_TWO_IMG} ${LANGCD_IMG1} ${LANGCD_IMG2}" >> $LOGFILE
    echo "Region=$SEL_REGION" >> $LOGFILE
    if [ $"ALTROOT" = "y" ]
    then
	echo "Alt_Root=$ALT_ROOT_DIR" >> $LOGFILE
    fi
    echo >> $LOGFILE
fi

for cd in ${forlist}
do
  echo "installing $cd"
    # check do we have the image
    #echo "DEBUG: DEVICE is $DEVICE"
    get_install_dir ${DEVICE} $SEL_REGION $cd
    position=$?
    #echo "DEBUG: DEVICE is now $DEVICE"
    while [ $position -ne 0 ]
    do
      #echo "DEBUG: array element $position"
	# we have a bad dir so we need to dump it out of the array (bugid 5067857)
      #echo "\nDEBUG: Init dir is $INIT_DIR\n"
        bad_dir=${INIT_DIR}
	#echo "DEBUG: bad_dir is $bad_dir"
	ans_str=`$gettext --domain=localeadm "Please enter the path to this image/disk, or enter 'q' to quit:\n (if this image is on CD-ROM, please mount the disk and give the path to the CD-ROM drive e.g. /cdrom/cdrom0) \n > "` 
	read dev?"${ans_str}"

	if [ "$dev" = "q" ]
	then
	    rem_lock
	    exit 1
	fi 
	#echo "DEBUG: position is $position"
	if [ $position -eq 1 ] # if it's the first element in the array
	  then 
	    newdir1=${DEVICE##${bad_dir}} #strip it off the front of the array
	    newdir="${dev}${newdir1}"
	elif [ $position -eq 2 ] # if it's the middle element
	  then
	    #echo "DEBUG: middle element"
	    newdir1=${DEVICE%%${bad_dir}*}
	    newdir2=${DEVICE##*${bad_dir}}
	    #echo "newdir1 is $newdir1\nnewdir2 is $newdir2"

	    newdir="${newdir1},${dev},${newdir2}"
	else
	   #echo "DEBUG: last element"
	   newdir1=${DEVICE%%${bad_dir}}
	   newdir="${newdir1},${dev}"
	fi
	#echo "\nDEBUG: New device is $newdir \n"
	DEVICE=${newdir}
	#echo "\nDEBUG: DEVICE is now $DEVICE\n"

	get_install_dir $DEVICE $SEL_REGION $cd #now need to change get_install_dir to only look for one img at a time!
	position=$?
	
#	if [ $position -eq 0 ]
#	    then
#	    newdir=${INIT_DIR}

    done

    case $cd in
	1of2)
	    ADD_DIR=${ONE_OF_TWO_IMG}
	    ;;
	2of2)
	    ADD_DIR=${TWO_OF_TWO_IMG}
	    ;;
	langcd1) 
	    ADD_DIR=${LANGCD_IMG1}
	    ;;
	langcd2) 
	    ADD_DIR=${LANGCD_IMG2}
	    ;;
    esac

    # check the disk here!!!!

    for package in `awk '$2 ~ /'$SEL_REGION'/ && $3 ~ /'$cd'/ {print $1}' $CONFIGFILE`
    do
	echo
	loc_prnt "Checking package %s......" nolog $package
	if [ $ALTROOT = "y" ]
	then
	    pkginfo -q -R $ALT_ROOT_DIR $pkg
	else
	    pkginfo -q $package
	fi

	if [ "$?" = "0" ]
	then
	    loc_prnt "Package %s is already installed. Skipping." nolog $package
	else
	    if [ "$2" = "y" ]
	    then
		loc_prnt "Adding package %s (test mode)" nolog $package
		if [ "$ALTROOT" = "y" ]
		then  
		    echo "pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin -R $ALT_ROOT_DIR $package" > /tmp/pkg.add
		    pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin -R $ALT_ROOT_DIR -s /dev/null $package 2>&1 >> /tmp/pkg.add
		else
		    echo "pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin $package" > /tmp/pkg.add
		    pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin -s /dev/null $package 2>&1 > /tmp/pkg.add
		fi
	    else
		loc_prnt "Adding package %s" log $package
		if [ "$ALTROOT" = "y" ]
		then
		    echo "pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin -R $ALT_ROOT_DIR $package" > /tmp/pkg.add
		    pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin -R $ALT_ROOT_DIR $package 2>&1 >> /tmp/pkg.add
		else
		    echo "pkgadd -d $ADD_DIR -a /usr/sadm/lib/localeadm/admin $package" > /tmp/pkg.add
		    pkgadd -a /usr/sadm/lib/localeadm/admin -d $ADD_DIR $package 2>&1 >> /tmp/pkg.add
		fi
	    fi
	    
	    if [ $? -ne 0 ] # if pkgadd didn't work properly
	    then
		cat /tmp/pkg.add >> /tmp/pkgadd.fail
		if [ "$2" = "y" ]
		then
		    loc_prnt "There was an error testing the addition of package %s to the system. Please check the /tmp/pkgadd.fail file for details." nolog $package
		else
		    loc_prnt "There was an error adding package %s to the system. Please check the /tmp/pkgadd.fail file for details." log $package
		    loc_prnt "Your system may be in an unstable state. Please fix the problem and rerun \'localeadm -a\' to repair the locale." log
		fi
		rem_lock
		exit 1
	    fi

	    # check are there any daemons that need to be started...... 
	    # (just the files, so looking for type 'f' only)
	    $grep "etc/init.d" $ADD_DIR/$package/pkgmap | $grep -q "^1 f"
	    if [ $? -eq 0 ]
	    then
		daemons=1
		# get the daemon name & put it into an array, make sure it's a full path (i.e. '/etc/init.d' not 'etc/init.d')
		daemon_cmds[arr_index]=`$grep "etc/init.d/" $ADD_DIR/$package/pkgmap | $grep "^1 f" | awk '{print $4}' | sed -e'/\=/d' -e 's/etc/\/etc/g'`
		arr_index=`expr $arr_index + 1`
	    fi
	fi

    done # end 'for package in.....'
done  # end 'for cd in ${forlist}'

arr_index=0

# update the /var/sadm/system/data/locales_added file
if [ "$2" != "y" ]
then
    add_loc_added $SEL_REGION
fi

# now we need to add the locale to the locales_list
if [ "$2" != "y" ]
then
    print_system q
    print_warning "added" nolog
fi
}

###########################################################################
######################  remove locale function   ##########################
###########################################################################
#
# $1 = region to remove
# $2 = test mode

rem_locales() {

typeset -l SEL_REGION=$1
check_region $SEL_REGION "uninstall"
if [ "$2" = "y" ]
then
    loc_prnt "Removing packages for %s (%s) - Test Mode" nolog "$SEL_STR" "$SEL_REGION"
else 
    loc_prnt "Removing packages for %s (%s)" log "$SEL_STR" "$SEL_REGION"
fi
echo
typeset -i pcount rcount install_flag

# first check that the locale is actually installed
if [ "$ALTROOT" = "y" ]
then
    query_locale $SEL_REGION
    rcount=$?
else
    rcount=`$grep -c $SEL_REGION $LOCALES_LIST`
fi    


if [ $rcount -eq 0 ] 
then 
    loc_prnt "Region %s is not installed on this machine. Exiting" nolog "$1"
    return
fi

if [ "$2" != "y" ] # don't need to record stuff if we're just doing test mode
then
    echo "Region=$SEL_REGION" >> $LOGFILE
    if [ $"ALTROOT" = "y" ]
    then
	echo "Alt_Root=$ALT_ROOT_DIR" >> $LOGFILE
    fi
    echo >> $LOGFILE
fi

if [ $MESSAGES -eq 1 ] # we only want messages
then # we only need the langcd
    forlist="langcd"
else
    forlist="1of2 2of2 langcd"
fi
#loc_prnt "Image list is %s" $forlist

for cd in ${forlist}
do
    for package in `awk '$2 ~ /'$SEL_REGION'/  && $3 ~ /'$cd'/ {print $1}' $CONFIGFILE`
    do
	install_flag=0 # reset each time around
	loc_prnt "Checking package %s...." log $package
    
	if [ $ALTROOT = "y" ]
	then
	    pkginfo -q -R $ALT_ROOT_DIR $pkg
	else
	    pkginfo -q $package
	fi

	if [ $? -ne 0 ]
	then
	    loc_prnt "Package %s is already uninstalled. Skipping." nolog $package
	    echo
	else
	# have to take into account pkgs that may be needed by other installed locales
	    count=`$grep -c $package $CONFIGFILE`
	    if [ $count -gt 1 ]
	    then
		loc_prnt "More than one region uses this package. Checking installed regions...." log
		for region in `awk '$1 ~ /'$package'/ {print $2}' $CONFIGFILE`
		do
		    rcount=`$grep -c $region $LOCALES_LIST`
		    if [ $rcount -ge 1 ] 
		    then 
			if [ "$region" != "$SEL_REGION" ]
			then
			    loc_prnt "Checking if region %s is installed....." logvb $region
			    reg_count=`$grep -c $region $LOCALES_LIST`
			    if [ $reg_count -gt 0 ]
			    then 
				loc_prnt "Package %s is needed by region %s. Skipping." log $package $region
				echo
				install_flag=1
				break
			    fi   #end "if $reg_count..."
			fi       #end "if $region..."
		    fi           #end "if $rcount.." 
		done             #end "for region...."
	    fi                   #end "if $count...."

	    #echo "flag is $install_flag"
	    if [ $install_flag -eq 0 ]
	    then
		if [ "$2" == "y" ]
		then
		    loc_prnt "Removing package %s (test mode)" nolog $package
		    
		    if [ "$ALTROOT" = "y" ]
		    then
			echo "pkgrm -a /usr/sadm/lib/localeadm/admin -n -R $ALT_ROOT_DIR $package" > /tmp/pkg.rem
			#pkgrm -a /usr/sadm/lib/localeadm/admin -n -t -R $ALT_ROOT_DIR $package 2>&1 >> /tmp/pkg.rem
		    else
			echo "pkgrm -a /usr/sadm/lib/localeadm/admin -n $package" > /tmp/pkg.rem
			#pkgrm -a /usr/sadm/lib/localeadm/admin -n -t $package 2>&1 >> /tmp/pkg.rem
		    fi
		    ERROR=$?
		else
		    loc_prnt "Removing package %s" log $package
	
		    if [ "$ALTROOT" = "y" ]
		    then
			echo "pkgrm -a /usr/sadm/lib/localeadm/admin -n -R $ALT_ROOT_DIR $package" > /tmp/pkg.rem
			pkgrm -a /usr/sadm/lib/localeadm/admin -n -R $ALT_ROOT_DIR $package 2>&1 >> /tmp/pkg.rem
		    else
			echo "pkgrm -a /usr/sadm/lib/localeadm/admin -n $package" > /tmp/pkg.rem
			pkgrm -a /usr/sadm/lib/localeadm/admin -n $package 2>&1 >> /tmp/pkg.rem
		    fi
		    ERROR=$?
		fi # end "if $2...."

		if [ $ERROR -ne 0 ] # if pkgrm didn't work properly
		then
		    mv /tmp/pkg.rem /tmp/pkgrm.fail
		    if [ "$2" = "y" ]
		    then
			echo
			loc_prnt "There was an error testing the removal of package %s from the system. Please check the /tmp/pkgrm.fail file for details." nolog $package
		    else
			echo
			loc_prnt "There was an error removing package %s from the system. Please check the /tmp/pkgrm.fail file for details." log $package
			loc_prnt "Your system may be in an unstable state. Please fix the problem and rerun \'localeadm -d\' to repair the locale." log
		    fi
		    echo
		    echo
		    rem_lock
		    exit 1
		fi #end "if $ERROR...."
		echo
	    fi    #end "if $install_flag...."
	fi        #end "if $?....."
    done          #end "for package...."
done              #end "for cd...."

# update the /var/sadm/system/data/locales_added file
if [ "$2" != "y" ]
then
    rem_loc_added $SEL_REGION
fi

# need to clear the locale out of the locales.list
if [ "$2" != "y" ]
then
    
    print_system q
    print_warning "removed" log
fi
}

###########################################################################
#################  add to locales_added function   ########################
###########################################################################
#
# $1 = region to add to /var/sadm/system/data/locales_installed file

add_loc_added() {

add_region=$1

# get the name of the region from the config file
# get the name of the region from the config file
region_name=`$grep locname $CONFIGFILE | sed -e 's/locname//g' | $grep -i $add_region | $awk '{print $1}'`

#echo "DEBUG: region name is $region_name"
# find out is it in there already 
if [ `$grep -c $region_name $LOC_ADD_FILE ` -eq 0 ]
then 
#  $sed -e's/"GEOS="/"GEOS=${region_name},"/g' $LOC_ADD_FILE > /tmp/locadd
 echo "s/GEOS=/GEOS=${region_name},/g" > /tmp/sedscript
#  cp /tmp/locadd $LOC_ADD_FILE
fi

for i in `$grep locname $CONFIGFILE | sed -e 's/locname//g'| $grep $add_region | $awk '{ for (j=3; j<=NF; j++) print $j }'`
do
#    #echo "DEBUG: loc is $i"
    if [ `$grep -c $i $LOC_ADD_FILE ` -eq 0 ]
    then
	locs="${i}${locs}"
    fi
done

echo "s/LOCALES=/LOCALES=${locs}/g" >> /tmp/sedscript
#mv /tmp/locadd $LOC_ADD_FILE
#echo "DEBUG: sed script is "
#more /tmp/sedscript
#echo
sed -f /tmp/sedscript $LOC_ADD_FILE > /tmp/locrm
#more /tmp/locrm


mv /tmp/locrm $LOC_ADD_FILE
rm /tmp/sedscript
}


###########################################################################
################  remove from locales_added function   ####################
###########################################################################
#
# $1 = region to remove from /var/sadm/system/data/locales_added file

rem_loc_added() {

rem_region=$1

# have to get rid of 'locname' out of final search string here 
# 'cos north america region (nam) keeps picking it up! 
region_name=`$grep locname $CONFIGFILE | sed -e 's/locname//g' | $grep -i $rem_region | $awk '{print $1}'`

#echo "region name is $region_name"

if [ "$region_name" = "Asia" ] # there are multiple geos which call themselves asia
then
    for geo in `$grep locname $CONFIGFILE | grep "Asia" | awk '{print $3}'`
    do
	acount=0
	lcount=0
	#echo "looking for $geo"
	for i in `$grep locname $CONFIGFILE | sed -e 's/locname//g'| $grep $geo | $awk '{ for (j=3; j<=NF; j++) print $j }'`
	do
	    if [ `$grep -c $i $LOC_ADD_FILE` -ne 0 ]
	    then
		acount=`expr acount+1`
	    fi
	done 
	lcount=`$grep locname $CONFIGFILE | sed -e 's/locname//g'| $grep $geo | $awk '{ for (j=3; j<=NF; j++) print $j }' | wc -l`
#	echo "acount is $acount, lcount is $lcount"
	if [ $acount -eq $lcount ] #if all teh locales are mentioned
	then
	    geo_count=`expr $geo_count+1` #up the asian geo count by 1
	fi
    done
#echo "geo_count is $geo_count"
fi

# find out is it in there already 
if [[ `$grep -c $region_name $LOC_ADD_FILE` -ne 0  && $geo_count -le 1 ]]
then 
    echo "s/$region_name,//g" > /tmp/sedscript
    echo "s/,$region_name//g" >> /tmp/sedscript # if it's at the end of the string
fi

for i in `$grep locname $CONFIGFILE | sed -e 's/locname//g'| $grep $rem_region | $awk '{ for (j=3; j<=NF; j++) print $j }'`
do
    echo "s/${i}//g" >> /tmp/sedscript
done

#echo "sed script is "
#more /tmp/sedscript
echo
sed -f /tmp/sedscript $LOC_ADD_FILE > /tmp/locrm
#more /tmp/locrm
rm /tmp/sedscript
mv /tmp/locrm $LOC_ADD_FILE


}


###########################################################################
######################  print warning function   ##########################
###########################################################################
#
# $1 = added or removed

print_warning() {
echo
echo
if [ "$1" = "added" ]
then
    loc_prnt "One or more locales have been added. To update the list of locales available at the login screen's \"Options->Language\" menu, please restart the dtlogin daemon (WARNING: this will terminate any active dtlogin sessions)" nolog
    echo

    if [ $daemons -eq 1 ]
    then
	loc_prnt "There are also processes/daemons that will need to be started/restarted for some of these locales to work properly with the desktop (e.g. input method daemons to allow input of localized text):" log
	for cmd in ${daemon_cmds[*]}
	do
	    loc_prnt "$cmd" log
	done
	echo
	loc_prnt "You will need to either start these manually or restart your machine for the locales to function properly." log 
    fi
    echo
    loc_prnt "Please log out and login again to use the new locale(s) at your desktop. If you are not intending to use the new locale(s) with the GUI desktop, you can start using the new locale(s) immediately by setting the LC_* environment variables." nolog
    echo    
else
    loc_prnt "One or more locales have been removed. To update the list of locales available at the login screen's \"Options->Language\" menu, please restart the dtlogin daemon (WARNING: this will terminate any active dtlogin sessions)" nolog
fi
echo
}

###########################################################################
######################  Fix locale function   #############################
###########################################################################
#
# $1 = the locale to fix

fix_locale() {

# find the last time the locale was used 
lastregion=$1

check_region $lastregion "fix"

for i in `ls -rt /var/sadm/install/logs/localeadmin_* `
do 
    lastlog=`grep -l "Region\=$SEL_REGION" $i`
done

if [ -z $lastlog ]
then
    loc_prnt "Error: There don't seem to have been any previous operations on %s (%s)" nolog "$SEL_STR" "$SEL_REGION"
    rem_lock
    exit 1
fi

# get all the arguments used last time
op=`$grep Operation\= $lastlog`
lastop=${op##Operation=}

alt=`grep Alt_Root $lastlog`
if [ $? -eq 0 ]
then
    ALTROOT="y"
    ALT_ROOT_DIR=${alt##=}
fi

if [ "$lastop" = "install" ]
then 
    if [ -z $DEVICE ]
    then
	echo "no dir supplied"
	dir=`grep Install_Dir\= $lastlog`
	lastdir=${dir##Install_Dir=}
	set -A DEVICES `echo $lastdir | awk '{for(i = 1; i <= NF; i++) print $i}'`
    else
	get_devices $DEVICE
	get_install_dir $DEVICES $SEL_REGION
    fi
  #  echo "Adding locale $SEL_REGION from ${DEVICES[*]}"
    rem_locales $lastregion
    add_locales $lastregion
else
 #   echo "removing locale $SEL_REGION"
    rem_locales $lastregion
fi
}

###########################################################################
######################  Query locale function   ###########################
###########################################################################

query_locale() {
typeset -l SEL_REGION=$1
DEVICE=$2
typeset -i result=0 core=0 langcd=0

check_region $SEL_REGION "check"

# check for core cluster pkgs first
if [ $ALTROOT = "y" ]
then
    check_locale $SEL_REGION "alt" "of2c"
else
    check_locale $SEL_REGION "mach" "of2c"
fi
core=`expr $core + $?`

if [ $core -eq 2 ]
then
    # we have something on the system, so check for the rest of the pkgs
    for disk in 1of2 2of2
    do
	if [ $ALTROOT = "y" ]
	then
	    check_locale $SEL_REGION "alt" "$disk"
	else
	    check_locale $SEL_REGION "mach" "$disk"
	fi
	result=`expr $result + $?`
    done
	 
    if [ $result -ge 3 ] # we have a 1of2 and a 2of2
    then
	if [ $ALTROOT = "y" ]
	then
	    check_locale $SEL_REGION "alt" langcd1
	else
	    check_locale $SEL_REGION "mach" langcd1
	fi
	langcd=`expr $result + $?`

	if [[ $SEL_REGION = "ceu" || "$SEL_REGION" = "seu" || $SEL_REGION = "nam" ]]
	then
	    if [ $ALTROOT = "y" ]
	    then
		check_locale $SEL_REGION "alt" langcd2
	    else
		check_locale $SEL_REGION "mach" langcd2
	    fi
	langcd=`expr $result + $?`
	fi
    fi
fi

if [[ $result -eq 4 && $core -eq 2 && langcd -ge 2 ]] # if we have a messages as well
then
    loc_prnt "The %s region (%s) is installed on this system" nolog "$SEL_STR" "$1"
    echo
    return 1    
elif [[ $result -eq 4 && $core -eq 2 ]] # no messages but a 1of2 and 2of2
then
    loc_prnt "The %s region (%s) is installed on this system without messages" nolog "$SEL_STR" "$1"
    echo
    return 1
elif [ core -eq 2 ]
then
    loc_prnt "The %s region (%s) is installed on this system, but some packages may be missing." nolog "$SEL_STR" "$1"
    loc_prnt "Please run localeadm with the -v option to find the list of missing packages." nolog 
else
    loc_prnt "The %s region (%s) is not installed on this system" nolog "$SEL_STR" "$1"
    echo
    return 0
fi
# we should never get here, but let's put in a fail return just in case
return 0

}

###########################################################################
#######################  get devices function  ############################
###########################################################################
#
# $1 - dir list taken in from the user

get_devices(){
# if we have multiple images, they should be comma separated with no spaces
typeset -i num=0 arr=0 arrnext=0

num=`echo $1 | awk -F"," '{print NF}'`

# separate all the dirs out
set -A DEVICES `echo $1 | awk -F"," '{for(i = 1; i <= NF; i++) print $i}'`
arr=`expr $arr + 1` 
echo
loc_prnt "Devices are %s " nolog "${DEVICES[*]}"
}

###########################################################################
#####################  get install dir function   #########################
###########################################################################
#
# $1 = DEVICES - array of dirs passed in as arg
# $2 = $LOC - locale chosen by user
# $3 = image - image we are looking for

get_install_dir() {
DEVS="$1"
LOC=$2
echo
loc_prnt "Operating System is Solaris %s" nolog $OS_VER
echo 
loc_prnt "Checking for a valid disk image:" log
echo
typeset -i pkgcount=0 arr=0 break=0 result=0 arr_size=0 retval=0 cd1=0 cd2=0 langcd1=0 langcd2=0

#if [ $MESSAGES -eq 1 ] # if all we want to do is add messages, we don't need to look for 1of2 or 2of2
#then
#    cd1=1
#    cd2=1  
#fi


if [[ "$LOC" = "ceu" || "$LOC" = "seu" || "$LOC" = "nam" ]]
then
    langcd2=1
fi

get_devices $1
arr_size=${#DEVICES[*]}

if [ $arr_size -lt 3 ] # if we have less than 3 args, we have to up the number to at least 3 
then
    if [ "${DEVICES[0]}" = "${DEVICES[$arr_size -1]}" ]
#   if [ "${DEVS[0]}" = "${DEVS[$arr_size -1]}" ]
    then 
	set -A devices ${DEVICES[*]} ${DEVICES[0]} ${DEVICES[0]}
#	set -A devices ${DEVS[*]} ${DEVS[0]} ${DEVS[0]}
	cd2=1
	langcd=1
	#arr_size=`expr $arr_size + 1` # minimum no. of arguments is 1, so we need to add at least 2
    else
	set -A devices ${DEVICES[*]} ${DEVICES[0]}
#	set -A devices ${DEVS[*]} ${DEVS[0]}
    fi
else
 set -A devices ${DEVICES[0]} ${DEVICES[1]} ${DEVICES[2]}
#    set -A devices ${DEVS[0]} ${DEVS[1]} ${DEVS[2]}
fi

arr_size=${#devices[*]}

# debug stuff
#echo "DEBUG: New Device 1 is ${devices[0]}"
#echo "DEBUG: New Device 2 is ${devices[1]}"
#echo "DEBUG: New Device 3 is ${devices[2]}"

while [[ arr -lt arr_size && break -eq 0 ]]
do
  retval=0
    INIT_DIR=${devices[${arr}]} 
    #echo "DEBUG: INIT_DIR is $INIT_DIR"

#	INIT_DIR=$1
#	echo "Init dir is $INIT_DIR"

    if [ -d $INIT_DIR/Solaris_$OS_VER ] # we have a 1of2, 2of2 or combined image
    then 
	INST_DIR=$INIT_DIR/Solaris_$OS_VER/Product

    elif [ -d $INIT_DIR/components ] # we have a langcd - need to get the correct subdir
    then
	case $LOC in
	weu)
	    dir="French/$ARCH/Packages"
	    ;;
	ceu)
	    dir="German/$ARCH/Packages" 
	    ;;
	nam)
	    dir="Spanish/$ARCH/Packages"
	    ;;
	neu)
	    dir="Swedish/$ARCH/Packages"
	    ;;
	seu)
	    dir="Spanish/$ARCH/Packages" 
	    ;;
	sam)
	    dir="Spanish/$ARCH/Packages" 
	    ;;
	ja)
	    dir="Japanese/$ARCH/Packages"
	    ;;
	ko)
	    dir="Korean/$ARCH/Packages"
	    ;;
	zh_tw)
	    dir="TraditionalChinese/$ARCH/Packages"
	    ;;
	zh_cn)
	    dir="SimplifiedChinese/$ARCH/Packages"
	    ;;
	zh_hk)
	    dir="TraditionalChineseHongKong/$ARCH/Packages"
	    ;;
	all)
	    dir="" 
	    ;;
	esac
	
	INST_DIR=$INIT_DIR/components/$dir
	loc_prnt "Directory to check is %s" logvb $INST_DIR

	if [ ! -d $INST_DIR ]
	then # we've been given a langcd but we don't need it
	    INST_DIR=${devices[arr]}
	  #INST_DIR=$1
	fi
	#echo "DEBUG: INST DIR is $INST_DIR"

    elif [ -d $INIT_DIR ] # assume just a dir
    then
	INST_DIR=${devices[arr]}
      #INST_DIR=$1
   else
	loc_prnt "Warning! The directory specified does not exist \n (%s) \n" nolog $INIT_DIR
	typeset -i retval=`expr $arr+1`
	#echo "DEBUG: returning $retval"
	return $retval
    fi
    if [ `basename $INST_DIR` = "components" ]
    then
	typeset -i pkgcount=1
    else
    typeset -i pkgcount=`ls $INST_DIR | $grep -c SUNW`
    fi

    if [ $pkgcount -eq 0 ]
    then
	loc_prnt "Warning! No Solaris packages exist in %s" nolog $INST_DIR
	typeset -i retval=`expr $arr+1`
	#echo "DEBUG: returning $retval"
    else
	#echo "DEBUG: PATH is $INST_DIR"

	# make sure that all pkgs have the same architecture - bugid 5052631

	if [ -f /tmp/arch.txt ] # make sure this file is clear before we start!
	then
	    rm /tmp/arch.txt 
	fi

	for package in `awk '$2 ~ /'$LOC'/ {print $1}' $CONFIGFILE`
	do
	    if [ -f  $INST_DIR/$package/pkginfo ]
	    then
		tmp_arch=`grep ARCH $INST_DIR/$package/pkginfo | sed -e's/ARCH=//g'`
		#echo "DEBUG: arch of $package is $tmp_arch"
		echo $tmp_arch >> /tmp/arch.txt
	    fi
	done

	sort -u /tmp/arch.txt > /tmp/arch_final.txt

	if [ `wc -l /tmp/arch_final.txt | awk '{print $1}'` -ne 1 ]
	then
	    typeset -i retval=`expr $arr+1`
	    echo
	    loc_prnt "The packages in %s do not appear to be all of the same architecture." nolog $INST_DIR
 	    loc_prnt "Please check these packages and re-run localeadm."
	    echo
	    break=1
	else
	    pkg_arch=`more /tmp/arch_final.txt`
	fi
	
	#echo "DEBUG: pkg_arch is $pkg_arch"

	# clean up all the temp files
	if [ -f /tmp/arch_final.txt ]
	then
	    rm /tmp/arch_final.txt
	fi
	if [ -f /tmp/arch.txt ]
	then
	    rm /tmp/arch.txt
	fi

	if [ $pkg_arch != $ARCH -a break -eq 0 ] # if the pkgs aren't the same arch as the system
 	then
	    typeset -i retval=`expr $arr+1`
	    echo
	    loc_prnt "The packages in \n%s \nappear to be the incorrect architecture for your system." nolog $INST_DIR
	    loc_prnt "(packages are %s, your system is %s)" nolog $pkg_arch $ARCH
 	    loc_prnt "Please check these packages and re-run localeadm."
	    echo
	    #echo "DEBUG: returning $retval"
#	    return $retval
	    break=1
	fi

#	if [ cd1 -eq 0 ]
      if [ $3 = "1of2" -a break -eq 0 ]
	then
	    echo
	    loc_prnt "Checking for a 1of2 image" log
	    check_image "1of2" $LOC
	    result=$?
	    if [ $result -eq 1 ]
	    then
		loc_prnt "We have a 1of2: \n%s" log $INST_DIR
		ONE_OF_TWO_IMG=${INST_DIR}
		cd1=1
		break=1
	    elif [ $result -eq 2 ]
	    then
		loc_prnt "We don't need a 1of2" logvb
		ONE_OF_TWO_IMG=""
		cd1=1
		break=1
	    else
	      loc_prnt "No 1of2 image has been found in \n%s " nolog $INST_DIR
	      echo

	      typeset -i retval=`expr $arr+1`
	      #echo "DEBUG: returning $retval"
	    fi
	fi

#	if [ cd2 -eq 0 -a break -eq 0 ]
	if [ $3 = "2of2" -a break -eq 0 ] 
	then	
	    echo
	    loc_prnt "Checking for a 2of2 image" log
	    check_image "2of2" $LOC
	    result=$?
	    if [ $result -eq 1 ] 
	    then	
		loc_prnt "We have a 2of2: \n%s" log $INST_DIR
		TWO_OF_TWO_IMG=${INST_DIR}
		cd2=1
		break=1
	    elif [ $result -eq 2 ]
	    then
		loc_prnt "We don't need a 2of2" logvb
		TWO_OF_TWO_IMG=""
		cd2=1
		break=1
	    else
	      loc_prnt "No 2of2 image has been found in \n%s " nolog $INST_DIR
	      echo
	      typeset -i retval=`expr $arr+1`
	    fi
	      #echo "DEBUG: returning $retval"
	fi
	
#	if [ langcd1 -eq 0  -a break -eq 0 ]
	if [ $3 = "langcd1" -a break -eq 0 ]
	then	    	
	    echo
	    loc_prnt "Checking for a langcd image" log
	    check_image "langcd1" $LOC
	    result=$?
	    if [ $result -eq 1 ] 
	    then 
		loc_prnt "We have a langcd: \n%s" log $INST_DIR
		LANGCD_IMG1="${INST_DIR}"
		langcd1=1
		break=1
	    elif [ $result -eq 2 ]
	    then
		loc_prnt "We don't need a langcd" log
		LANGCD_IMG1=""
		langcd1=1
		break=1
	    else
	      loc_prnt "No langcd image has been found in \n%s " nolog $INST_DIR
	      echo
	      typeset -i retval=`expr $arr+1`
	      #echo "DEBUG: returning $retval"
	    fi
	fi	# end if langcd    

#	if [ langcd1 -eq 1 -a langcd2 -eq 1 ]
	if [ $3 = "langcd2" ]
	then  	
	    echo
	    loc_prnt "Checking for a 2nd langcd image" log
	    if [ -d $INIT_DIR/Solaris_$OS_VER ] # we have a 1of2, 2of2 or combined image
	    then 
		INST_DIR=$INIT_DIR/Solaris_$OS_VER/Product
	    elif [ -d $INIT_DIR/components ]
	    then
		case $LOC in
		seu)
		    dir="Italian/$ARCH/Packages" 
		    ;;
		ceu)
		    dir="French/$ARCH/Packages"
		    ;;
		nam)
		    dir="French/$ARCH/Packages"
		    ;;
		esac

		INST_DIR=$INIT_DIR/components/$dir
	    else # assume just a dir
		INST_DIR=$INIT_DIR
	    fi
	    check_image "langcd2" $LOC
	    result=$?
	    if [ $result -eq 1 ] 
	    then 
		loc_prnt "We have a 2nd langcd: \n%s" log $INST_DIR
		LANGCD_IMG2="${INST_DIR}"
		langcd2=0
		break=1
	    elif [ $result -eq 2 ]
	    then
		loc_prnt "We don't need a 2nd langcd" logvb
		LANGCD_IMG2=""
		langcd2=0
		break=1
	    else
	      loc_prnt "No 2nd langcd image has been found in \n%s " nolog $INST_DIR
	      echo
	      typeset -i retval=`expr $arr+1`
	      #echo "DEBUG: returning $retval"
	    fi
	fi
    fi
    arr=`expr $arr + 1`
done

if [[ cd1 -eq 0 && "$2" != "all" && "$3" = "1of2" ]]
then
    loc_prnt "You need at least a 1of2 image to add any locales." nolog
    loc_prnt "Please provide the path to a Solaris 1of2 image." nolog
    echo    
  #  rem_lock
  #  typeset -i retval=`expr $arr+1`
    #echo "DEBUG: returning $retval"
fi
if [[ cd2 -eq 0 && "$3" = "2of2" ]]
then
    loc_prnt "No 2of2 image has been found in \n%s " nolog "${devices[*]}"

  #  typeset -i retval=`expr $arr+1`
    #echo "DEBUG: returning $retval"

fi
if [[ langcd1 -eq 0 && "$3" = "langcd1" ]]
then
    loc_prnt "No langcd image has been found in \n%s " nolog "${devices[*]}"

fi
echo
     
return $retval
}

###########################################################################
########################  check user function  ############################
###########################################################################
check_rights(){

for auth in `auths | tr , " "`
do
    [ "$auth" = "solaris.*" ] && break
    [ "$auth" = "solaris.admin.patchmgr.*" ] && break
    [ "$auth" = "solaris.admin.patchmgr.write" ] && break
done

if [[ "$auth" = "solaris.*" || "$auth" = "solaris.admin.patchmgr.*" || "$auth" = "solaris.admin.patchmgr.write" ]]
then
    RIGHTS="y"
fi

if [ "$RIGHTS" != "y" ]
then
    loc_prnt "Warning! You do not have the correct authorisation to modify system packages." nolog
    loc_prnt "Please contact your system administrator about obtaining this authorization." nolog
#    rm /usr/sadm/lib/localeadm/lock
    rem_lock
    exit
fi
}

###########################################################################
####################    set up the logfile     ############################
###########################################################################

make_log() {
CMD=$1
DATE=`date "+%Y-%m-%d"`
if [ -f /var/sadm/install/logs/localeadmin_${CMD}.$DATE ]
then # have to go to $DATE.1, $DATE.2 etc.
    if [ -f /var/sadm/install/logs/localeadmin_${CMD}.$DATE.* ]
    then
	typeset -i datenum=1
	while [ -f /var/sadm/install/logs/localeadmin_${CMD}.$DATE.$datenum ]
	do
	    datenum=`expr $datenum + 1`
	done    
	LOGFILE=/var/sadm/install/logs/localeadmin_${CMD}.$DATE.$datenum
    else
	LOGFILE=/var/sadm/install/logs/localeadmin_${CMD}.$DATE.1
    fi
else
    LOGFILE=/var/sadm/install/logs/localeadmin_${CMD}.$DATE
fi
date > $LOGFILE
echo "Operation=$CMD" >> $LOGFILE

loc_prnt "Log file is %s" nolog $LOGFILE

PKGTMP=/tmp/pkg.${CMD}
}

###########################################################################
####################    set up a lockfile     #############################
###########################################################################

make_lock() {

if [ -L /tmp/lock ]
then
    lock=`ls -la /tmp/lock | awk '{ print $NF}'`

    loc_prnt "Warning: Another localeadm process seems to be running on display %s" nolog $lock
    echo
    loc_prnt "Please wait until this process is finished, and if necessary remove the /tmp/lock file" nolog
    exit 0
else
    if [ $DISPLAY="" ]
    then
	DISPLAY="${HOST}:0.0"
    fi
    ln -s $DISPLAY /tmp/lock
fi
}

###########################################################################
####################    delete the lockfile     ###########################
###########################################################################

rem_lock() {

    if [ -L /tmp/lock ]
    then
	rm /tmp/lock
    fi
}

###########################################################################
#######################    main loop    ###################################
###########################################################################

TEST="n" # default value is no test mode
#global variables
typeset HOST=`uname -n`
typeset ARCH=`uname -p`

typeset -i MESSAGES=0 sub_list=0 add_rem=0 COMBINED_IMG=0 exit_val=0 daemons=0
typeset -l ALTROOT="n"
typeset daemon_cmds


RELEASE=`uname -r`
OS_VER=${RELEASE##*.} #have to hack for testing on linux
#OS_VER=10

CONFIGFILE=/usr/sadm/lib/localeadm/Locale_config_S${OS_VER}.txt
if [ ! -f $CONFIGFILE ]
then
    loc_prnt "Error: The configuration file for this release (%s) does not seem to exist on your system. Please contact your system administrator" nolog $CONFIGFILE
    rem_lock
    exit 1
fi

LOC_ADD_FILE=/var/sadm/system/data/locales_installed

if [ -w /usr/sadm/lib/localeadm/ ]
then
    LOCALES_LIST=/usr/sadm/lib/localeadm/locales.list
    if [ ! -L /tmp/locales.list ]
    then
	ln -s /usr/sadm/lib/localeadm/locales.list /tmp/locales.list
    fi
else
    LOCALES_LIST=/tmp/locales.list
fi

while getopts a:cd:f:hlLmnq:r:R:stv opts
do
case $opts in
    a)
	if [[ "$OPTARG" = "r" || "$OPTARG" = "-r" ]]
	then
	    add_rem=`expr $add_rem + 1`
	elif [ "$CMD" = "rem" ]
	then
	    add_rem=`expr $add_rem + 1`
	elif [ "$OPTARG" = "t" ]
	then
	    TEST="y"
	fi
	    CMD="add"	    
	    LOC=$OPTARG
	;;
    c)
	sub_list=`expr $sub_list + 1`
	P_CMD="code"
	;;
    d)
	if [ "$CMD" = "list" ]
	then
	    CMD=listall
	fi
	DEVICE="$OPTARG"
	;;
    f)
	CMD="fix"
	LOC=$OPTARG
	;;
    h)
	print_help
	exit 0
	;;
    l) 
	CMD="list"
	if [ ! $P_CMD ]
	then
	    P_CMD="locale"
	fi
	;;
    m)
	MESSAGES=1
	;;
    q)
	CMD="query"
	LOC=$OPTARG
	;;
    r)
	if [[ "$OPTARG" = "a" || "$OPTARG" = "-a" ]]
	then
	    add_rem=`expr $add_rem + 1`
	elif [ "$CMD" = "add" ]
	then
	    add_rem=`expr $add_rem + 1`
	elif [ "$OPTARG" = "t" ]
	then
	    TEST="y"
	fi
	CMD="rem"
	LOC="$OPTARG"
	;;
    R)
	ALTROOT="y"
	ALT_ROOT_DIR=$OPTARG
	;;
    s)
	sub_list=`$expr $sub_list + 1`
	P_CMD="short"
	;;
    t) 
	TEST="y"
	;;
    v)
	loc_prnt "Verbose mode" nolog
	VERBOSE="y"
	;;
    V)
	print_version
	exit 0
	;;
    \?)
	print_help_msg $1
	exit 0
	;;
esac

# just check to see did we enter any conflicting subcommands....
if [ $sub_list -gt 1 ]
then
    loc_prnt "Warning: You can enter only one listing subcommand at a time." nolog
    loc_prnt "Please enter -lc or -ls" nolog
    exit_val=1
    break
fi

if [ $add_rem -gt 0 ]
then 
    echo
    loc_prnt "Error: you cannot add and remove locales at the same time." nolog
    loc_prnt "Please choose either '-a' (add) or '-r' (remove) as an option" nolog
    echo
    exit_val=1
    break
fi
done

# now we have the args sorted out, let's actually do something

# set up a lock file so you can't run more than one invocation at a time
make_lock

case $CMD in
    add)
    	check_rights
	if [ ! $DEVICE ]
	then
	    loc_prnt "Error: You must supply a location to add packages from." nolog
	    echo "e.g. localeadm -a weu -d /cdrom/cdrom0"
  	    echo
	    exit_val=1
	    break
	else
	    make_log "install"
	    if [ ! -f $LOCALES_LIST ]
	    then
		loc_prnt "Analyzing system for addition. This may take a minute." nolog
		print_system q
	    fi
	    typeset -l SEL_REGION=$LOC
	    check_region $LOC "install"
	    #get_devices $DEVICE
	    # do a quick check to make sure we're not trying to add a locale that's already there
	    rcount=`$grep -c $SEL_REGION $LOCALES_LIST`
	    msg_count=`$grep  $SEL_REGION $LOCALES_LIST | $grep -q "no messages"`
	    if [[ $rcount -gt 0 && $msg_count -ne 0 ]] 
	    then
		loc_prnt "Region %s is already installed on this machine. Exiting" nolog $SEL_REGION
		echo
		exit_val=0
		break
	    else		
		add_locales $LOC $TEST
	    fi
	fi
	;;
    fix)
	fix_locale $LOC
	;;
    listall)	
	get_devices $DEVICE
	print_system $P_CMD $TEST "all"
	;;
    list)
	print_system $P_CMD $TEST
	;;
    query)
	query_locale $LOC $DEVICE
	# have to reverse the normal exit values as I use o/p of query_locale as a counter elsewhere
	if [ $? -eq 1 ]
	then 
	    exit_val=0
	    break
 	elif [ $? -eq 0 ]
	then
	    exit_val=1
	    break
	fi
	;;
    rem)
	check_rights
	make_log "uninstall"

	if [ ! -f $LOCALES_LIST ]
	then
	    loc_prnt "Analyzing system for remove. This may take a minute." nolog
	    print_system q
	fi

	rem_locales $LOC $TEST
	;;
esac

# once we're finished, remove the lock file
rem_lock

# make sure we exit with an error value if we get any interruptions/segfaults etc
# get rid of the lockfile first, so we can run again
trap 'if [-L /tmp/lock]; then rm /tmp/lock;fi;exit 1'0 1 2 3 10 11 15 
#trap "exit 1" 0 1 2 3 10 11 15 

exit $exit_val # make sure we exit cleanly if we get this far.........
