#/bin/sh
#
# LI18NUX2K.L1/utils/localedef/localedef.sh
#
# Copyright (c) 1991, 1992, 1997, 1998, 1999, 2001 X/Open Company Ltd.
# (X/Open)
# trading as The Open Group
# Copyright (c) 2001, 2003 International Business Machines Corp.
# All rights reserved except as granted by this License.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the "Artistic License" which comes with this
# Kit, with the following modification:
# a) "executable(s)" should be interpreted to include
# "test case(s)"
# b) if you wish to make changes as defined in clause 2 and 3, and
# distribute a modified version of this package, then
# clauses 3c and 4c are required
# c) Clause 7 is rephrased as follows: "Subroutines supplied by you
# and linked into this Package shall not be considered part of this
# Package".
#
#
# X/OPEN, TRADING AS THE OPEN GROUP, AND IBM CORP. DISCLAIM ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL X/OPEN OR IBM CORP. BE
# LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# You should have received a copy of the Artistic License with this
# Kit, in the file named "Artistic".  If not, we'll be glad to provide one.


tet_startup="startup"			# startup function
tet_cleanup="cleanup"			# cleanup function
iclist="ic1"
ic1="tp1 tp2 tp3 tp4 tp5 tp6 tp7 tp8 tp9 tp10 tp11"

targetlocale="LTP_1.UTF-8"

LCVARS="LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES"

check_locale_existance()
{
	unset LANG
	unset LC_ALL

	chk_region=`echo $1 | cut -f 1 -d'.'`
	chk_codeset=`echo $1 | cut -f 2 -d'.' | tr [[:upper:]] [[:lower:]] | tr -d '-'`
	chk_locale_name="${chk_region}.${chk_codeset}"

	locale -a > locale_list 2> out.stderr
	while read line
	do
	  region=`echo ${line} | cut -f 1 -d'.'`
	  codeset=`echo ${line} | cut -f 2 -d'.' | tr [[:upper:]] [[:lower:]] | tr -d '-'`
	  locale_name="${region}.${codeset}"

	  if [ ${locale_name} = ${chk_locale_name} ] ; then
	    return 0
	  fi
	done < locale_list

	return 1
}

tp1()
{
	tet_infoline "* When no error occurred and the locale was successfully created, verify the exit value is 0."
	tet_infoline " "

	./localedef_priv -f UTF-8 -i LOCALEDEF_1 LOCALEDEF_1.UTF-8 > out.stdout 2> out.stderr
	EXIT_CODE=$?
	check_locale_existance "LOCALEDEF_1.UTF-8"
	if [ $EXIT_CODE = 0 -a $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "can't create locale correctly"
	   tet_infoline "eixt code of localedef is ${EXIT_CODE}"
	   tet_infoline "if exit code is more than 3, please check directory if user 'vsx0' has write permission of the locale directory "
	   tet_result FAIL
        fi
	
}

tp2()
{
	tet_infoline "* When warnings occurred and the locale was successfully created, verify the exit value is 1."
	tet_infoline " "

	./localedef_priv -c -f JOHAB -i LOCALEDEF_1 LOCALEDEF_2.johab > out.stdout 2> out.stderr
	EXIT_CODE=$?
	check_locale_existance "LOCALEDEF_2.johab"
	if [ $EXIT_CODE = 1 -a $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "can't create locale correctly"
	   tet_infoline "eixt code of localedef is ${EXIT_CODE}"
	   tet_infoline "if exit code is more than 3, please check directory if user 'vsx0' has write permission of the locale directory "
	   tet_result FAIL
        fi
}

tp3()
{
	tet_infoline "* When the locale specification exceeded implementation limits or the coded character set or sets used were not supported by the implementation, and no locale was created, verify the exit value is 2."
	tet_infoline " "

	./localedef_priv -f UTF-8 -i LOCALEDEF_3 LOCALEDEF_3.UTF-8 > out.stdout 2> out.stderr
	EXIT_CODE=$?
	check_locale_existance "LOCALEDEF_3.UTF-8"
	if [ $EXIT_CODE = 2 -a $? = 1 ]; then
	   tet_result PASS
        else
	   tet_infoline "created locale unexpectedly"
	   tet_infoline "This test should not create locale and exit code code sould be 2."
	   tet_infoline "eixt code of localedef is ${EXIT_CODE}"
	   tet_infoline "if exit code is more than 3, please check directory if user 'vsx0' has write permission of the locale directory "
	   tet_infoline " "
	   tet_infoline "*) This test result is waived now."
	   tet_result WARNING
        fi
	
}

tp4()
{
	tet_infoline "* When the capability to create new locales is not supported by the implementation, verify the exit value is 3."
	tet_infoline " "

	tet_infoline "This assertion is not tested."
	tet_infoline "We have no idea to produce these situation."
	tet_result UNTESTED
}

tp5()
{
	tet_infoline "* When warnings or errors occurred and no output was created, the exit value is more than 3."
	tet_infoline " "

	./localedef_priv -f JOHAB -i LOCALEDEF_1 LOCALEDEF_5.johab > out.stdout 2> out.stderr
	EXIT_CODE=$?
	check_locale_existance "LOCALEDEF_5.johab"
	if [ $EXIT_CODE -gt 3 -a $? = 1 ]; then
	   tet_result PASS
        else
	   tet_infoline "created locale unexpectedly"
	   tet_infoline "eixt code of localedef is ${EXIT_CODE}"
	   tet_result FAIL
        fi
}

tp6()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_CTYPE (e.g. isalpha) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_CTYPE=${LC_CTYPE}
	LANG=$targetlocale
	LC_CTYPE=$targetlocale
	export LANG;
	export LC_CTYPE;
	./test06
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "iswctype and/or wctype don't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_CTYPE=${OLD_LC_CTYPE}
	unset OLD_LC_CTYPE
	unset OLD_LANG
}

tp7()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_COLLATE (e.g. strcoll) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_COLLATE=${LC_COLLATE}
	LANG=$targetlocale
	LC_COLLATE=$targetlocale
	export LANG
	export LC_COLLATE
	
	./test07
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "wcscoll doesn't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_COLLATE=${OLD_LC_COLLATE}
	unset OLD_LC_COLLATE
	unset OLD_LANG
}

tp8()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_MONETARY (e.g. localeconv) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_MONETARY=${LC_MONETARY}
	LANG=$targetlocale
	LC_MONETARY=$targetlocale
	export LANG
	export LC_MONETARY
	
	./test08
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "localeconv() doesn't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_MONETARY=${OLD_LC_MONETARY}
	unset OLD_LC_MONETARY
	unset OLD_LANG
}

tp9()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_NUMERIC (e.g. localeconv) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_NUMERIC={$LC_NUMERIC}
	LANG=$targetlocale
	LC_NUMERIC=$targetlocale
	export LANG
	export LC_NUMERIC
	
	./test09
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "localeconv() doesn't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_NUMERIC=${OLD_LC_NUMERIC}
	unset OLD_LC_NUMERIC
	unset OLD_LANG
}

tp10()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_TIME (e.g. nl_langinfo) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_TIME=${LC_TIME}
	LANG=$targetlocale
	LC_TIME=$targetlocale
	export LANG
	export LC_TIME
	
	./test10
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "nl_langinfo(MON_2) doesn't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_TIME=${OLD_LC_TIME}
	unset OLD_LC_TIME
	unset OLD_LANG
}

tp11()
{
	tet_infoline "* When the locale was successfully created, check the function which is effected by LC_MESSAGE (e.g. nl_langinfo) is executed correctly."
	tet_infoline " "

	OLD_LANG=${LANG}
	OLD_LC_MESSAGES=${LC_MESSAGES}
	LANG=$targetlocale
	LC_MESSAGES=$targetlocale
	export LANG
	export LC_MESSAGES
	
	./test10
	if [ $? = 0 ]; then
	   tet_result PASS
        else
	   tet_infoline "locale is broken or"
	   tet_infoline "nl_langinfo(YESEXPR) doesn't behave correctly"
	   tet_result FAIL
        fi
	LANG=${OLD_LANG}
	LC_MESSAGES=${OLD_LC_MESSAGES}
	unset OLD_LC_MESSAGES
	unset OLD_LANG
}


startup() # startup function
{
	rm -f out.stdout out.stderr >/dev/null 2>&1
}


cleanup() # clean-up function
{
	rm -f out.stdout out.stderr >/dev/null 2>&1
}

# source common shell functions

. $TET_EXECUTE/LIB/i18nfuncs.sh
. $TET_EXECUTE/LIB/shfuncs.sh
. $TET_EXECUTE/LIB/lsbfuncs.sh

# execute shell test case manager - must be last line
. $TET_ROOT/lib/xpg3sh/tcm.sh
