#/bin/sh
#
# LI18NUX2K.L1/utils/locale/locale.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"

targetlocale="LTP_1.UTF-8"

LCVARS="LC_CTYPE LC_COLLATE LC_MONETARY LC_NUMERIC LC_TIME LC_MESSAGES"

# Make sure LANG and LC_* variables are unset.
locale_initialize()
{
	unset LANG
	for var in $LCVARS
	do
	   unset $var
	done
	unset LC_ALL
}

# Extract locale name
extract_name()
{
	category=$1
	file=$2

	line=`grep "^${category}" ${file}`
	name=`echo ${line} | cut -f 2 -d= | tr '[:lower:]' '[:upper:]' | sed -e 's/"//g' -e 's/-//g'`
	echo ${name}
}

# Check the output of locale is correct or not.
check_outputs()
{
	actfile=output.$$
	expfile=$1
	ret=0

	locale > ${actfile}

	for category in ${LCVARS}
	do
	  actual=`extract_name ${category} ${actfile}`
	  expect=`extract_name ${category} ${expfile}`

	  if test x${actual} != x${expect}
	  then
	    tet_infoline "The value of ${category} is invalid."
	    tet_infoline " actual: ${actual}"
	    tet_infoline " expect: ${expect}"
	    ret=1
	  fi
	done

	rm ${actfile}

	return ${ret}
}

tp1()
{
	tet_infoline "When LANG is set and other internationalization"
	tet_infoline "environment variables are not set, verify if this"
	tet_infoline "utility outputs the value of LANG in all categories."
	tet_infoline " "
	
	locale_initialize
	export LANG=$targetlocale

	check_outputs locale.expout1
	result_output_code $? "Can't print the value of LANG in all categories."
}

tp2()
{
	tet_infoline "When an internationalization environment variable except"
	tet_infoline "LANG and LC_ALL is set and LC_ALL is not set, verify if"
	tet_infoline "this utility outputs the value of the environment"
	tet_infoline "variable at a category whose name is as same as each"
	tet_infoline "environment variable."
	tet_infoline " "

	locale_initialize
        export LANG=VSX4L0
        for var in $LCVARS
        do
           export $var=$targetlocale
        done

	check_outputs locale.expout1
	result_output_code $? "Can't print the value of each category."
}

tp3()
{
	tet_infoline "When LC_ALL is set, verify if this utility outputs the"
	tet_infoline "value of LC_ALL in all categories."
	tet_infoline " "

	locale_initialize
	export LC_ALL=$targetlocale

	check_outputs locale.expout1
	result_output_code $? "Can't print the value of LC_ALL in all categories."
}

tp4()
{
	tet_infoline "When -a option is specified, verify if this utility"
	tet_infoline "writes information about all available public locales."
	tet_infoline " "

	locale_initialize
	export LANG=$targetlocale

        locale -a >out.stdout 2>out.stderr
	(grep "^C" out.stdout && grep "^${targetlocale%.*}" out.stdout) >/dev/null 2>&1

	result_output_code $? "Can't print available locales."
}

tp5()
{
	tet_infoline "When -c option is specified, verify if this utility"
	tet_infoline "writes the names of selected locale categories."
	tet_infoline " "

	locale_initialize
	export LANG=$targetlocale

      	resultflag=0
	while read keyword category
	do
	  if [ $category != `locale -c $keyword | head -1` ]; then
                tet_infoline "`locale -c $keyword | head -1` should be $category"
		resultflag=1
	  fi
	done < locale.expout5

	result_output_code $resultflag "Can't print category of selected locale keyword."
}

tp6()
{
	tet_infoline "When -k option is specified, verify if this utility"
	tet_infoline "writes the names and values of selected keywords."
	tet_infoline " "

	locale_initialize
	export LANG=$targetlocale

      	resultflag=0
	while read line
	do
	  keyword=`echo $line | cut -d= -f1`
	  if [ "$line" != "`locale -k $keyword`" ]; then
                tet_infoline "`locale -k $keyword` should be $line"
		resultflag=1
	  fi
	done < locale.expout6

	result_output_code $resultflag "Can't print names and values of selected locale keyword."
}

tp7()
{
	tet_infoline "When -m option is specified, verify if this utility"
	tet_infoline "writes names of available charmaps correctly."
	tet_infoline " "

	locale_initialize
	export LANG=$targetlocale

	charmap=`locale charmap`
        locale -m >out.stdout 2>out.stderr
        if [ $? != 0 ]; then
           tet_infoline "`cat out.stderr`"
           tet_result FAIL
           exit
        fi
	grep -E "^$charmap" out.stdout >/dev/null 2>&1

	result_output_code $? "Can't print available charmap."
}

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
