#/bin/sh
#
# LI18NUX2K.L1/utils/msgfmt/msgfmt.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 tp12 tp13 tp14 tp15 tp16"

tp1()
{
	tet_infoline "Verify this utility creates message object"
	tet_infoline "file which can be gotten messages by gettext"
	tet_infoline "family library."
	tet_infoline " "

	mkdir -p ./LTP_1.UTF-8/LC_MESSAGES/

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt test1.po 2>/dev/null

	if [ ! -s ./openi18n.mo ] ; then
	    tet_infoline "A message object file doesn't exist."
	    tet_result UNRESOLVED
	    unset LANG
	    exit
	fi

	mv openi18n.mo ./LTP_1.UTF-8/LC_MESSAGES/

	./openi18n

	if [ $? -ne 0 ] ; then
		tet_infoline "The gettext library gets wrong message"
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi
	
	unset LANG
	rm -rf LTP_1.UTF-8
}

tp2()
{
	tet_infoline "When this utility creates message object file, "
	tet_infoline "verify that this utility does not change the "
	tet_infoline "portable object files."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	cp test2.po test2.po.orig

	msgfmt test2.po 2>/dev/null

	if [ ! -s ./test2.mo ] ; then
	    tet_infoline "A message object file doesn't exist."
	    tet_result UNRESOLVED
	    unset LANG
	    exit
	fi

	diff test2.po test2.po.orig

	if [ $? -ne 0 ] ; then
	    tet_infoline "The portable object file are changed."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp3()
{
	tet_infoline "When input file is -, verify this utility"
	tet_infoline "read standard input."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	cat test3.po | msgfmt - 2>/dev/null

	if [ ! -s ./test3.mo ] ; then
		tet_infoline "Cannot create message object file."
		tet_result FAIL 	# Fail
		unset LANG
		exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp4()
{
	tet_infoline "When '-D directory' or '--directory=directory' "
	tet_infoline "option is specified, verify that this utility "
	tet_infoline "searches the directory named directory."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	rm -f test3.mo
	mkdir -p ./foo
	mv test3.po ./foo

	msgfmt -D ./foo test3.po 2>/dev/null

	if [ ! -s ./test3.mo ] ; then
		tet_infoline "Cannot create message object file."
		tet_result FAIL 	# Fail
	else	
		tet_result PASS		# success
	fi
	
	mv ./foo/test3.po .
	rm -rf foo
	unset LANG
}

tp5()
{
	tet_infoline "When '-f' or '--use-fuzzy' option is not "
	tet_infoline "specified, verify that the fuzzy entries "
	tet_infoline "are not included into the output."
	tet_infoline " "

	mkdir -p ./LTP_1.UTF-8/LC_MESSAGES/

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt fuzzy.po 2>/dev/null

	if [ ! -s ./openi18n.mo ] ; then
	    tet_infoline "A message object file doesn't exist."
	    tet_result UNRESOLVED
	    unset LANG
	    exit
	fi

	mv openi18n.mo ./LTP_1.UTF-8/LC_MESSAGES/

	./openi18n

	if [ $? -eq 0 ] ; then
		tet_infoline "The gettext library gets wrong message"
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi
	
	unset LANG
	rm -rf LTP_1.UTF-8
}

tp6()
{
	tet_infoline "When '-f' or '--use-fuzzy' option is specified,"
	tet_infoline "verify that the fuzzy entries are included into"
	tet_infoline "the output."
	tet_infoline " "

	mkdir -p ./LTP_1.UTF-8/LC_MESSAGES/

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt -f fuzzy.po 2>/dev/null

	if [ ! -s ./openi18n.mo ] ; then
	    tet_infoline "A message object file doesn't exist."
	    tet_result UNRESOLVED
	    unset LANG
	    exit
	fi

	mv openi18n.mo ./LTP_1.UTF-8/LC_MESSAGES/

	./openi18n

	if [ $? -ne 0 ] ; then
		tet_infoline "The gettext library gets wrong message"
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi
	
	unset LANG
	rm -rf LTP_1.UTF-8
}

tp7()
{
	tet_infoline "When '-o output-file' or '--output-file=output-file'"
	tet_infoline "option is not specified, verify that the name of"
	tet_infoline "output file is the same as domainname."  
	tet_infoline " "

	mkdir -p ./LTP_1.UTF-8/LC_MESSAGES/

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt 3domain.po 2>/dev/null

	fail=0
	for mo in first.mo second.mo third.mo ; do
	    if [ ! -s ./${mo} ] ; then
		tet_infoline "A message object file ${mo} doesn't exist."
		fail=`expr ${fail} + 1`
	    fi
	done      

	if [ ${fail} -gt 0 ] ; then
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi

	unset LANG
}

tp8()
{
	tet_infoline "When '-o output-file' or '--output-file=output-file'"
	tet_infoline "option is specified, verify that the name of output"
	tet_infoline "file is output-file."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	msgfmt test3.po -o test8.mo 2>/dev/null

	if [ ! -s ./test8.mo ] ; then
		tet_infoline "Cannot create message object file."
		tet_result FAIL 	# Fail
		unset LANG
		exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp9()
{
	tet_infoline "When '-o output-file' or '--output-file=output-file'"
	tet_infoline "option is specified, verify that all domain"
	tet_infoline "directives and duplicate msgids in the portable"
	tet_infoline "object file is ignored." 
	tet_infoline " "
	tet_infoline "The behavior on this situation is unspecified."
	tet_result UNTESTED	# untested

# 
# Following script would not be executed because of the change of specification.
#

#	mkdir -p ./LTP_1.UTF-8/LC_MESSAGES/
#
#	LANG=LTP_1.UTF-8
#	export LANG
#
#	rm -f *.mo
#	msgfmt 3domain.po -o test9.mo 2>/dev/null
#
#	fail=0
#	for mo in first.mo second.mo third.mo ; do
#	    if [ -s ./${mo} ] ; then
#		tet_infoline "A message object file ${mo} exist wrongly."
#		fail=`expr ${fail} + 1`
#	    fi
#	done      
#
#	if [ ! -s ./test9.mo ] ; then
#	    tet_infoline "A message object file 'test9' doesn't exist."
#	    tet_result FAIL 	# Fail
#	    exit
#	fi
#
#	mv test9.mo ./LTP_1.UTF-8/LC_MESSAGES/openi18n.mo
#
#	./openi18n
#
#	if [ $? -ne 0 ] ; then
#		tet_infoline "The gettext library gets wrong message"
#		tet_result FAIL 	# Fail
#	else
#		tet_result PASS		# success
#	fi
#	
#	unset LANG
#	rm -rf LTP_1.UTF-8
}

tp10()
{
	tet_infoline "When '-o output-file' or"
	tet_infoline "'--output-file=output-file' option is specified"
	tet_infoline "and output-file is -, verify that output is"
	tet_infoline "written to standard output."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	rm -f *.mo 
	msgfmt test3.po -o - > test10.mo 2>/dev/null

	if [ -s ./test3.mo ] ; then
	    tet_infoline "A message object file 'test3.po' is created wrongly."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	if [ ! -s ./test10.mo ] ; then
	    tet_infoline "The output is not written to standard input."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp11()
{
	tet_infoline "When '--strict' option is not specified, verify"
	tet_infoline "that the output filename is the same as the"
	tet_infoline "domainname."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	rm -f *.mo test11
	msgfmt test3.po -o test11

	if [ ! -s ./test11 ] ; then
	    tet_infoline "The message object file 'test11' is not found."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp12()
{
	tet_infoline "When '--strict' option is specified and the"
	tet_infoline "suffix '.mo' is not present at domainname,"
	tet_infoline "verify that this utility added the suffix"
	tet_infoline "to the output filename."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	rm -f *.mo test12
	msgfmt test3.po -o test12 --strict

	if [ ! -s ./test12.mo ] ; then
	    tet_infoline "The message object file 'test12.mo' is not found."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp13()
{
	tet_infoline "When '--strict' option is specified and the"
	tet_infoline "suffix '.mo' is already present at domainname,"
	tet_infoline "verify that the output filename is same as the"
	tet_infoline "dommainname."
	tet_infoline " "
	
	LANG=LTP_1.UTF-8
	export LANG

	rm -f *.mo test13
	msgfmt test3.po -o test13.mo --strict

	if [ ! -s ./test13.mo ] ; then
	    tet_infoline "The message object file 'test13.mo' is not found."
	    tet_result FAIL 	# Fail
	    unset LANG
	    exit
	fi
	
	unset LANG
	tet_result PASS		# success
}

tp14()
{
	tet_infoline "When '-v' option is specified, verify that this"
	tet_infoline "utility detects and diagnoses input file"
	tet_infoline "anomalies which might represent translation"
	tet_infoline "errors."
	tet_infoline " "
	
	tet_infoline "The specification doesn't say how the msgfmt"
	tet_infoline "utility shows the result of the detection"
	tet_infoline "and diagnosis."

	tet_result UNTESTED	# untested
}

tp15()
{
	tet_infoline "When this utility creates message object"
	tet_infoline "files from portable object files successfully,"
	tet_infoline "verify that the exit value is 0."
	tet_infoline " "

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt test3.po 2>/dev/null

	if [ $? -gt 0 ] ; then
		tet_infoline "The exit value is not correct"
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi

	unset LANG
}

tp16()
{
	tet_infoline "When this utility does not create message"
	tet_infoline "object files from portable object files,"
	tet_infoline "verify that the exit value is higher than 0."
	tet_infoline " "

	LANG=LTP_1.UTF-8
	export LANG

	msgfmt >/dev/null 2>&1

	if [ $? -eq 0 ] ; then
		tet_infoline "The exit value is not correct"
		tet_result FAIL 	# Fail
	else
		tet_result PASS		# success
	fi

	unset LANG
}


startup() # startup function
{
	# Unset the environment variable concerned with locale.
	for i in `locale | cut -f 1 -d '='` ; do
	  unset ${i}
	done
	rm -rf ./LTP_1.UTF-8 *.mo *.orig
}


cleanup() # clean-up function
{
	rm -rf ./LTP_1.UTF-8 *.mo
}

# 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
