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

tp1()
{
	export LANG=LTP_1.UTF-8
	tet_infoline "* Verify this utility can handle the patterns which contain multibyte characters."
	tet_infoline " "

	echo 'def' | grep -e '\(ａｂｃ\)*def' > out.stdout 2> out.stderr

	if [ $? != 0 ] ; then # not match or error
		tet_infoline "Can't handle a pattern containing multibyte."
		tet_result FAIL # fail
		exit
	fi

	echo 'abc' | grep -e '\(ａｂｃ\)*def' > out.stdout 2> out.stderr

	if [ $? = 1 ] ; then
		tet_result PASS # success
	else # match or error
		tet_infoline "A pattern contains multibyte match incorrectly."
		tet_result FAIL # fail
		exit
	fi
}

tp2()
{
	export LANG=LTP_1.UTF-8
	tet_infoline "* Verify this utility can handle the files which contain multibyte patterns."
	tet_infoline " "

	echo 'def' | grep -f exp1.in > out.stdout 2> out.stderr

	if [ $? != 0 ] ; then # not match or error
		tet_infoline "Can't handle a pattern file containing multibyte."
		tet_result FAIL # fail
		exit
	fi

	echo 'abc' | grep -f exp1.in > out.stdout 2> out.stderr

	if [ $? = 1 ] ; then # not match
		tet_result PASS # success
	else # match or error
		tet_infoline "A pattern file contains multibyte match incorrectly."
		tet_result FAIL # fail
	fi
}

tp3()
{
	export LANG=LTP_1.UTF-8
	tet_infoline "* Verify this utility can match the strings which contain multibyte characters."
	tet_infoline " "

	echo 'ｏｋ' | grep -e 'ｏｋ' > out.stdout 2> out.stderr
	diff out.stdout mb.ok > /dev/null 2>&1

	if [ $? != 0 ] ; then
		tet_infoline "Can't match a multibyte string."
		tet_result FAIL # fail
		exit
	fi

	echo 'ｏk' | grep -e 'ｏｋ' > out.stdout 2> out.stderr

	if [ $? != 1 ] ; then
		tet_infoline "A multibyte pattern match incorrectly."
		tet_result FAIL # fail
		exit
	fi

	echo 'ｏｋ' | grep -f mb.ok > out.stdout 2> out.stderr
	diff out.stdout mb.ok > /dev/null 2>&1

	if [ $? != 0 ] ; then
		tet_infoline "Can't handle a pattern file containing multibyte."
		tet_result FAIL # fail
		exit
	fi

	echo 'ｏk' | grep -f mb.ok > out.stdout 2> out.stderr

	if [ $? = 1 ] ; then
		tet_result PASS # success
	else
		tet_infoline "A multibyte pattern file match incorrectly."
		tet_result FAIL # fail
	fi
}

tp4()
{
	export LANG=LTP_1.UTF-8
	tet_infoline "* Verify this utility can match the files which contain multibyte characters."
	tet_infoline " "

	grep -f mb.ok mb.ok > out.stdout 2> out.stderr
	diff out.stdout mb.ok > /dev/null 2>&1

	if [ $? != 0 ] ; then
		tet_infoline "Can't handle a file containing multibyte."
		tet_result FAIL # fail
		exit
	fi

	grep -e 'ｏｋ ' mb.ok > out.stdout 2> out.stderr

	if [ $? = 1 ] ; then
		tet_result PASS # success
	else
		tet_infoline "A multibyte file match incorrectly."
		tet_result FAIL # fail
	fi
}

tp5()
{
	export LANG=LTP_1.UTF-8
	tet_infoline "* Verify this utility can perform pattern matching in searches without regard to case if -i option is specified."
	tet_infoline " "

	echo 'ａｂＣＤ' | grep -i -e 'ａａＣＣ' > out.stdout 2> out.stderr

	if [ $? != 1 ] ; then # not match or error
		tet_infoline "The pattern \"ａａＣＣ\" with -i match incorrectly."
		tet_result FAIL # fail
		exit
	fi

	echo 'ａｂＣＤ' | grep -i -e 'ＡｂｃＤ' > out.stdout 2> out.stderr

	if [ $? != 0 ] ; then # not match or error
		tet_infoline "Can't handle a pattern \"ＡｂｃＤ\" with -i."
		tet_result FAIL # fail
		exit
	fi

	echo 'ａｂＣＤefGH' | grep -i -e 'ＡｂｃＤEfgH' > out.stdout 2> out.stderr

	if [ $? = 0 ] ; then
		tet_result PASS # success
	else # match or error
		tet_infoline "Can't handle a pattern \"ＡｂｃＤ\" with -i."
		tet_result FAIL # fail
		exit
	fi
}

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
