#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by CA.  The
#* program, including this material, may not be duplicated, disclosed or
#* reproduced in whole or in part for any purpose without the express
#* written authorization of CA.  All authorized reproductions must be
#* marked with this legend.
#*
#*                        Copyright (c) 1994-2005
#*                                   CA
#*                    The Software That Manages eBusiness
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
# parameter :
#	-------case 1: ---------
#	p1:message file full path
#	p2:nls.cfg file full path
#	p3:nls_utility full path
#	p4:checked folder
#	p5:currenct LANG
#	p6:nls.map file full path
# return value:
#	0 		OK
#	others 		error

RTN_C=1
RTN_EUC=2
RTN_SJIS=3
RTN_DE=4
RTN_FR=5
RTN_ERROR=-1

LANG_C=C
LANG_JA=ja
LANG_EUC=ja_euc
LANG_SJIS=ja_sjis
LANG_DE=de
LANG_FR=fr
LANG_KO=ko
LANG_SP=sp
LANG_SC=sc
LANG_TC=tc
LANG_IT=it
LANG_HK=tchk
LANG_PT=pt

DEFAULT_LANG=C
MULTI_LANG_CERTIFIED=TRUE

RTN_OK=0
RTN_ERR=1
modify_nls_cfg ()
{
   src=$1
   LANG_VAR=$2
   lang_val=$3
   locale_val=$4
    
	if [ -x "$NLS_UTILITY_FILE" ] 
	then
		$NLS_UTILITY_FILE -u $src  "${LANG_VAR}" ${lang_val} ${locale_val}
		return $?	
	fi
	return $RTN_OK
	
}

LOAD_MESSAGES()
{
	if [ -f "$1"  ] 
	then
		.  $1
	fi
}
get_nls_name()
{
	MY_MSG=""
	if [ $# -gt 0 ]
	then
		if [ "$1" = "$LANG_C" ];then
			MY_MSG="${JAVA_LANG_MSG2}"
		elif [ "$1" = "$LANG_JA" ] ;then
			MY_MSG="${JAVA_LANG_MSG17}"
		elif [ "$1" = "$LANG_EUC" ] ;then
			MY_MSG="${JAVA_LANG_MSG3}"
		elif [ "$1" = "$LANG_SJIS" ] ;then
			MY_MSG="${JAVA_LANG_MSG4}"
		elif [ "$1" = "$LANG_DE" ] ;then
			MY_MSG="${JAVA_LANG_MSG5}"
		elif [ "$1" = "$LANG_FR" ] ;then
			MY_MSG="${JAVA_LANG_MSG6}"
		elif [ "$1" = "$LANG_SC" ] ;then
			MY_MSG="${JAVA_LANG_MSG7}"			
		elif [ "$1" = "$LANG_TC" -o  "$1" = "$LANG_HK" ] ;then
			MY_MSG="${JAVA_LANG_MSG8}"			
		elif [ "$1" = "$LANG_IT" ] ;then
			MY_MSG="${JAVA_LANG_MSG14}"			
		elif [ "$1" = "$LANG_SP" ] ;then
			MY_MSG="${JAVA_LANG_MSG15}"			
		elif [ "$1" = "$LANG_KO" ] ;then
			MY_MSG="${JAVA_LANG_MSG9}"					
		elif [ "$1" = "$LANG_PT" ] ;then
			MY_MSG="${JAVA_LANG_MSG16}"					
		fi
		
	fi			
	
}
select_lang()
{
	
	NLS_DIR=$1
	ALTERNATE_LANG=$2
	#typeset -i ANS

 
	LANG_LIST="$LANG_C $ALTERNATE_LANG"
	AVAILABLE_LIST=""
	MENU_BODY=""
	index=0
	valid_lang=""
	for lang_id in $LANG_LIST
	do
		if [ -d $NLS_DIR/$lang_id ]
		then
		  	AVAILABLE_LIST="${AVAILABLE_LIST:+$AVAILABLE_LIST,}${lang_id}"
			get_nls_name $lang_id
		  	nls_name=$MY_MSG
		  	index=`${ECHO} $AVAILABLE_LIST | awk -F"," '{ print NF }'`
		  	MENU_BODY="${MENU_BODY:+$MENU_BODY \n}     $index) $nls_name"
		 fi
	done
	
	if [ "$MENU_BODY" != "" ]
	then
		langValid=0
		while [ $langValid -eq 0 ]
		do
			${ECHO} "${JAVA_LANG_MSG1} "
			${ECHO} "${MENU_BODY}"
			${ECHO} ""
			${ECHO} "${JAVA_LANG_MSG11} (1-$index) :\c"
			read ANS 
			case "$ANS" in 
			[0-9])
				if [ $ANS -le $index -a $ANS -gt 0 ]
				then
					langValid=1
				else
					${ECHO} "${JAVA_LANG_MSG12}"
				fi
				;;
			*)
				${ECHO} "${JAVA_LANG_MSG12}"		
				;;
			esac

		done
		valid_lang=`${ECHO} $AVAILABLE_LIST | cut -d"," -f $ANS`
	fi

	
}

nls_do()
{
	MSGFILE=$1
	CFGFILE=$2
	NLS_UTILITY_FILE=$3
	CHECKED_DIR=$4
	CUR_LOCALE=$5
	NLS_MAP_PATH=$6
	LANG_VAR=CA_NLS_LANG
	

	
	CUR_LANG=`${NLS_UTILITY_FILE} -c ${CUR_LOCALE} ${NLS_MAP_PATH} 2>/dev/null`
	OLDLANG=`${NLS_UTILITY_FILE} -g ${CFGFILE}  ${LANG_VAR} 2>/dev/null`
	OLD_LOCALE=`${NLS_UTILITY_FILE} -gl ${CFGFILE}  ${LANG_VAR} 2>/dev/null`
	
	
	MYLANG=$CUR_LANG
	
       if [ "$MULTI_LANG_CERTIFIED" = "TRUE" -a "$CUR_LANG" != "$DEFAULT_LANG" ] 
	then
 		LOAD_MESSAGES ${MSGFILE}
 		#${ECHO} ${JAVA_LANG_MSG13}

 			
 		select_lang $CHECKED_DIR $MYLANG
	 	MYLANG=$valid_lang
	 	
	 	if [ "$MYLANG" = "" ]
		then
			MYLANG=$DEFAULT_LANG
		fi

		${ECHO} ""
        
        	if [ "$CUR_LANG" != "$MYLANG" -a "$MYLANG" = "$DEFAULT_LANG" ]
        	then
        		MYLOCALE=$DEFAULT_LANG
        	else
        		MYLOCALE=$CUR_LOCALE
        	fi
        	 		
	else
		MYLANG=$CUR_LANG
		MYLOCALE=$CUR_LOCALE			 				
 	fi
 	

        
        if [ "$OLDLANG" != "$MYLANG" -o ${OLD_LOCALE} != "${MYLOCALE}" ]
        then
        	modify_nls_cfg $CFGFILE $LANG_VAR $MYLANG ${MYLOCALE}
        	return $?
        fi 
	return $RTN_OK	 	
}
#
# main section
#
OS=`uname -s`
#
# Platform-specific settings
#
case "$OS" in
  Linux)
        ECHO='echo -e'
        ;;
  *)
        ECHO='echo'
        ;;
esac

if [ $# -eq 0 ];then
	exit 0
fi

nls_do $1 $2 $3 $4 $5 $6
exit $?

