#!/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-2003
#*                                   CA
#*                    The Software That Manages eBusiness
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
# parameter:
#		-----case1--- 	
#		object:update nls.cfg
#		p1:-u
#		p2:file full path of nls.cfg
#		p3:variable name of XXX_LANG
#		p4:new value of variable XXX_LANG
#		p5:real value of a locale 
#
#		-----case3--- 
#		object:get right messages file
#		p1:-m
#		p2:parent path of nls.cfg
#		p3:value of LANG
#
#		-----case4--- 
#		object:get value of a section in nls.cfg.	
#		p1:-g
#		p2:file full path of nls.cfg
#		p3:section name
#
#		-----case6--- 
#		object:get right messages file whose format is prefix.lang.sufix
#		p1:-md
#		p2:parent path of nls.cfg
#		p3:value of LANG
#		p4:prefix
#		p5:subfix
#
#		-----case7--- 
#		object:accoding to LANG,return correponding value of XXX_LANG in nls.cfg
#		p1:-c
#		p2:value of LANG
#		p3:path of nls.map,if it's null, assume nls.map is in the same directory
#	
#		-----case8--- 
#		object:get value of locale in nls.cfg.	
#		p1:-gl
#		p2:file full path of nls.cfg
#		p3:section name
#		-----case9--- 
#		object:judge present whether UTF8 in involved or not by kernel version
#		p1:-l
#		p2:kernel version
# return value:
#	0		OK
#	others		some error
#		-----case10--- 
#		object:return help file path
#		p1:-dh
#		p2:parent path of nls.cfg
#		p3:value of LANG

RTN_OK=0
RTN_ERROR=1
LANG_DEFAULT=C
MAP_PATH_DEFAULT=nls.map
UTF8_IN_MAP="utf8"
ISO8859_IN_MAP="iso8859"
UTF8_FLAG_VALUE="UTF-8"
ISO8859_FLAG_VALUE="ISO8859"

LANG_2_NLSLANG()
{
	ENV_LANG=$1
	if [ "$2" = "" ];then
		MAP_PATH=$MAP_PATH_DEFAULT
	else
		MAP_PATH=$2
	fi

	if [ -f ${MAP_PATH} ];then
	
		NLSLANG=`grep -v "^#" $MAP_PATH | ${AWK} '{
		if (/^ *\[/) {
			nls_lang=$0;
			sub(/\[/,"",nls_lang);
			sub(/\]/,"",nls_lang);
			if ( nls_lang ==  "'$ENV_LANG'" )
			{
				print nls_lang;
				exit;
			}
		}
		if  ( $0 == "'$ENV_LANG'" )
		{

		        print nls_lang;
		        exit;
		}
		}' 2>/dev/null `
	
	fi
	
	if [ "$NLSLANG" = "" ] ;then
		NLSLANG=$LANG_DEFAULT
	fi
	
 
}
convert_lang()
{
	LANG_2_NLSLANG $1 $2
	get_langtype $NLSLANG
	${ECHO} ${NLS_LANG_TYPE}
}
nlscfg_update()
{
	CFGFILE=$1
	TMPCFGFILE=${CFGFILE}.tmp
	LANG_VAR=$2
	LANG_VALUE=$3
	LOCALE_VALUE=$4
	
	LANG_TYPE=$LANG_VALUE
	if [ "$LANG_TYPE" = "" ] 
	then
		LANG_TYPE=$LANG_DEFAULT
	fi
	
	if [ -f ${CFGFILE} ]
	then
		
		
		${AWK} '/^[ \t]*\#/ { print; next; }
		{
		if (match($1,"'$LANG_VAR'")){ printf "%s %s %s" ,$1,"'$LANG_VALUE'","'$LOCALE_VALUE'" }
		else { print; }
		}' $CFGFILE > $TMPCFGFILE
		if [ $? -eq 0 ] 
		then
	        	mv $TMPCFGFILE $CFGFILE
	        	chmod 755 $CFGFILE
	        else
	        	MY_RTN=$?
	        	rm -f $TMPCFGFILE 2>/dev/null
	        	return $MY_RTN	
	        fi
	else
		return ${RTN_ERROR}
	fi
	return ${RTN_OK}
}
get_nls_file()
{
	NLS_PATH=$1
	LANG_SRC=$2

	LANG_TYPE=$LANG_SRC
	if [ "$LANG_TYPE" = "" ] 
	then
		LANG_TYPE=$LANG_DEFAULT
	fi
		
	${ECHO} "${NLS_PATH}/${LANG_TYPE}/Messages"
	return ${RTN_OK}	
}
get_drhelp_file()
{
	NLS_PATH=$1
	LANG_SRC=$2

	LANG_TYPE=$LANG_SRC
	if [ "$LANG_TYPE" = "" ] 
	then
		LANG_TYPE=$LANG_DEFAULT
	fi
		
	${ECHO} "${NLS_PATH}/${LANG_TYPE}/DR_help.msg"
	return ${RTN_OK}	
}
get_nls_var()
{
	CFGFILE=$1
	VAR_NAME=$2
	VAR_VALUE=""
	if [ -f ${CFGFILE} ]
	then
        	VAR_VALUE=`${AWK} ' !/^ *#/ {
			if (match($1,"'$VAR_NAME'")){ print $2 ;exit;}
			}' $CFGFILE 
			`
	fi
	if [ "$VAR_VALUE" = "" ]
	then
		VAR_VALUE=$LANG_DEFAULT
	fi
	${ECHO} "${VAR_VALUE}"	
	return ${RTN_OK}
}
get_locale_var()
{
	CFGFILE=$1
	VAR_NAME=$2
	LOCALE_VALUE=""
	if [ -f ${CFGFILE} ]
	then
        	LOCALE_VALUE=`${AWK} ' !/^ *#/ {
			if (match($1,"'$VAR_NAME'")){ print $3 ;exit;}
			}' $CFGFILE 
			`
	fi
	if [ "$LOCALE_VALUE" = "" ]
	then
		LOCALE_VALUE=$LANG_DEFAULT
	fi
	${ECHO} "${LOCALE_VALUE}"	
	return ${RTN_OK}
}
get_nls_file_db()
{
	NLS_PATH=$1
	LANG_SRC=$2
	FILE_PREFIX=$3
	FILE_SUBFIX=$4

	LANG_DES=$LANG_SRC
	if [ "$LANG_DES" = "" ]
	then
		LANG_DES=$LANG_DEFAULT
	fi
	${ECHO} "${NLS_PATH}/${LANG_DES}/${FILE_PREFIX}.${LANG_DES}.${FILE_SUBFIX}"
	return ${RTN_OK}	
}
is_utf()
{
	if [ $# -lt 2 ] 
	then
		$ECHO $ISO8859_FLAG_VALUE
		return ${RTN_OK}
	fi
	
	KERNEL=$1
	NLSMAPFILE=$2
	NLS_LOCALE=$3

#	if [ -f /etc/redhat-release ];then
#		#RH8 kernel version is 2.4.18
#		version_compare $KERNEL "2.4.17"
#		if [ $? -eq 1 ] ;then
#			$ECHO $UTF8_FLAG_VALUE
#			return ${RTN_OK}
#		fi
#	elif [ -f /etc/redflag-release ] ;then
#		#RedFlag4.1 kernel version is 2.4.21
#		version_compare $KERNEL "2.4.20"
#                if [ $? -eq 1 ] ;then
#                        $ECHO $UTF8_FLAG_VALUE
#                        return ${RTN_OK}
#                fi
#	fi

	if [ "$NLS_LOCALE" = "" ] 
	then 
		if [ "$LANG" = "" ] 
		then
			LANG=C
		fi	
	
	        LANG_2_NLSLANG $LANG $NLSMAPFILE 
	        NLS_LANG=$NLSLANG
	        LC_CTYPE=`locale 2>/dev/null | grep LC_CTYPE | grep -v grep | cut -d"=" -f2| cut -d"\"" -f2`
		if [  "$NLS_LANG" = "C" -a "$LC_CTYPE" != "" ]
		then
		        LANG_2_NLSLANG $LC_CTYPE $NLSMAPFILE 
		        LC_CTYPE_TYPE=$NLSLANG
		        if [ "$LC_CTYPE_TYPE" != "$NLS_LANG" ]
		        then
		                NLS_LANG=$LC_CTYPE_TYPE
		        fi
		fi
	else
		LANG_2_NLSLANG $NLS_LOCALE $NLSMAPFILE 
		NLS_LANG=$NLSLANG
	fi
        get_langtype $NLS_LANG
        if [ "$NLS_UTF_FLAG" = "$UTF8_IN_MAP" ] 
        then
        	$ECHO $UTF8_FLAG_VALUE
        else
        	$ECHO $ISO8859_FLAG_VALUE
        fi
        return ${RTN_OK}
       
	
}
version_compare()
{
	if [ $# -eq 2 ]
	then
		LEFT=`echo $1 | awk -F"-" ' { print $1 }'`
		RIGHT=`echo $2 | awk -F"-" ' { print $1 }'`
		
		LEFT_1=`echo $LEFT | cut -d"." -f1`
		LEFT_2=`echo $LEFT | cut -d"." -f2`
		LEFT_3=`echo $LEFT | cut -d"." -f3`
		
		RIGHT_1=`echo $RIGHT | cut -d"." -f1`
		RIGHT_2=`echo $RIGHT | cut -d"." -f2`
		RIGHT_3=`echo $RIGHT | cut -d"." -f3`	
		
		LEFT_1=${LEFT_1:-0}
		LEFT_2=${LEFT_2:-0}
		LEFT_3=${LEFT_3:-0}
		RIGHT_1=${RIGHT_1:-0}
		RIGHT_2=${RIGHT_2:-0}
		RIGHT_3=${RIGHT_3:-0}		

		if [ $LEFT_1 -eq $RIGHT_1  -a $LEFT_2 -eq $RIGHT_2 -a $LEFT_3 -eq $RIGHT_3 ] #=
		then
			return 0
		elif  [ $LEFT_1 -lt $RIGHT_1  ] || [ $LEFT_1 -eq $RIGHT_1  -a $LEFT_2 -lt  $RIGHT_2  ] || [  $LEFT_1 -eq $RIGHT_1  -a $LEFT_2 -eq  $RIGHT_2  -a $LEFT_3 -lt $RIGHT_3 ] #left<right
		then
			return 2
		else #left > right
			return 1
		fi
	else
		return 3
	fi
}
get_langtype()
{
	NLS_LANG_TYPE=`$ECHO $1 | $AWK -F"_" '{ print $1}' 2>/dev/null`
	NLS_UTF_FLAG=`$ECHO $1 | $AWK -F"_" '{ print $2}' 2>/dev/null`
	if [ "$NLS_UTF_FLAG" != "$UTF8_IN_MAP" -a "$NLS_UTF_FLAG" != "$ISO8859_IN_MAP" ];then
		NLS_LANG_TYPE="$1"
	fi
}
#
# main section
#
OS=`uname -s`
#
# Platform-specific settings
#
case "$OS" in
  Linux)
        ECHO='echo -e'
        ;; 
  *)
        ECHO='echo'
        ;;
esac
case "$OS" in
  Linux)
        AWK=' awk '
        ;; 
  SunOS)
        AWK=' nawk '
        ;; 
  *)
        if [ -f "`which nawk`" ]
        then
           AWK=' nawk '
        else
           AWK=' awk '
        fi
        ;;
esac
if [ $# -gt 0 ] 
then
	
	if [ "X$1" = "X-u"  ] # cmd -c NLS_FILE_PATH LANG_VAR LANG_VALUE
	then
		nlscfg_update $2 $3 $4 $5
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi
	elif [ "X$1" = "X-m"  ] # cmd -m NLS_FILE_PARENT_PATH LANG_SRC
	then
		get_nls_file $2	$3 
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi	
	elif [ "X$1" = "X-dh"  ] # cmd -dh NLS_FILE_PARENT_PATH LANG_SRC
	then
		get_drhelp_file $2	$3 
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi				
	elif [ "X$1" = "X-g"  ] # cmd -g NLS_FILE_PATH VAR_NAME
	then
		get_nls_var $2	$3 
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi
	elif [ "X$1" = "X-md"  ] # 
	then
		get_nls_file_db  $2 $3 $4 $5 
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi	
	elif [ "X$1" = "X-c"  ] # 
	then
		convert_lang  $2  $3
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi
	elif [ "X$1" = "X-gl"  ] # 
	then
		get_locale_var $2 $3 
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi
	elif [ "X$1" = "X-e"  ] # 
	then
		is_utf $2 $3 $4
		if [ $? -ne ${RTN_OK} ] 
		then
			exit $?
		fi				
	fi 
		
fi 
exit $RTN_OK
