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

tp1()
{
	tet_infoline "* Verify if the shell judges the existence of files and directories whose names are written in portable filename character set with test command."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	read tp1_file < files
	echo "test ${tp1_file}" > tp1.sh
	cd ${TMP_DIR}
	${TEST_SHELL} ../tp1.sh
	if [ $? = 0 ] ; then
		tet_result PASS # success
	else
		tet_infoline "can't recognize filename: ${tp1_file}"
		tet_result FAIL # fail
	fi
	unset tp1_file
	cd ${ROOT_DIR}
}

tp2()
{
	tet_infoline "* Verify if an iteration statement (e.g. for) handles strings written in portable filename character set."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	tp2_cmd="for f in"
	{
	    while read line; do
                tp2_cmd="${tp2_cmd} ${line}";
	    done
	} < files
	tp2_cmd="${tp2_cmd} ;do echo \$f;done";
	echo $tp2_cmd > tp2.sh
	${TEST_SHELL} tp2.sh > result
	diff files result > out.stdout 2> out.stderr
	if [ ! -s out.stdout -a ! -s out.stderr ] ; then
		tet_result PASS # success
	else
		tet_infoline "can't handle arguments correctly"
		tet_infoline "out.stdout `cat out.stdout`"
		tet_infoline "out.stderr `cat out.stderr`"
		tet_result FAIL # fail
		exit
	fi
	unset line
	rm result
}

tp3()
{
	tet_infoline "* Verify if a string written in portable filename character set is stored in a variable."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	read tp3_file < files
	echo "hoge=${tp3_file}" > tp3.sh
	echo 'echo ${hoge}' >> tp3.sh
	${TEST_SHELL} tp3.sh > out.stdout
	read result < out.stdout
	if [ "X${tp3_file}" = "X${result}" ] ; then
		tet_result PASS # success
	else
		tet_infoline "can't store into variable"
		tet_infoline "expected ${tp3_file} but result ${result}"
		tet_result FAIL # fail
	fi
	unset tp3_file
	unset result
}

tp4()
{
	tet_infoline "* Verify if an ordinary character included portable filename character set is matched itself."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	read tp4_file < files
	echo "${TMP_DIR}/${tp4_file}" > tp4.sh
	chmod 755 ${TMP_DIR}/${tp4_file}
	${TEST_SHELL} tp4.sh > out.stdout > out.stderr
	if [ ! -s out.stdout -a ! -s out.stderr ] ; then
		tet_result PASS # success
	else
		tet_infoline "doesn't match file with ${TMP_DIR}/${tp4_file}"
		tet_result FAIL # fail
	fi
	unset tp4_file
}

tp5()
{
	tet_infoline "* Verify if a '?' is matched any characters included in the portable filename character set."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	sort files > tp5.exp
	echo "\\ls ????? | sort" > tp5.sh
	cd ${TMP_DIR}
	${TEST_SHELL} ${ROOT_DIR}/tp5.sh > ${ROOT_DIR}/out.stdout
	cd ${ROOT_DIR}
	diff tp5.exp out.stdout > result
	if [ ! -s result ] ; then
		tet_result PASS # success
	else
		tet_infoline "does not matched with '?????'"
		tet_infoline "diff is `cat result`"
		tet_result FAIL # fail
	fi
	rm tp5.exp
}

tp6()
{
	tet_infoline "* Verify if an '*' is matched any strings written in the portable filename character set."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	sort files > tp6.exp
	echo "\\ls * | sort" > tp6.sh
	cd ${TMP_DIR}
	${TEST_SHELL} ${ROOT_DIR}/tp6.sh > ${ROOT_DIR}/out.stdout
	cd ${ROOT_DIR}
	diff tp6.exp out.stdout > result
	if [ ! -s result ] ; then
		tet_result PASS # success
	else
		tet_infoline "does not matched with '*'"
		tet_infoline "diff is `cat result`"
		tet_result FAIL # fail
	fi
	rm tp6.exp
}

tp7()
{
	tet_infoline "* Verify if a matching list expression '[...]' is matched the characters which are in the list. The listed characters are included in the portable filename character set."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	sort files > tp7.exp
	echo "\\ls [-._abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789][-._abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789][-._abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789][-._abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789][-._abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789] | sort" > tp7.sh
	cd ${TMP_DIR}
	${TEST_SHELL} ${ROOT_DIR}/tp7.sh > ${ROOT_DIR}/out.stdout
	cd ${ROOT_DIR}
	diff tp7.exp out.stdout > result
	if [ ! -s result ] ; then
		tet_result PASS # success
	else
		tet_infoline "does not matched"
		tet_infoline "diff is `cat result`"
		tet_result FAIL # fail
	fi
	rm tp7.exp
}


tp8()
{
	tet_infoline "* Verify if a character class '[:class:]' is matched characters according to the current locale."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	OLD_LC_CTYPE=${LC_CTYPE}
	OLD_LC_COLLATE=${LC_COLLATE}
	OLD_LC_ALL=${LC_ALL}
	OLD_LANG=${LANG}
	LANG=LTP_1.UTF-8
	LC_ALL=${LANG}
	LC_CTYPE=${LANG}
	LC_COLLATE=${LANG}
        export LANG
        export LC_ALL
        export LC_CTYPE
        export LC_COLLATE

	echo "echo [[:xdigit:]] [[:hyphen:]]" > tp10.sh
	echo "0 1 2 3 4 a b c z -" > tp10.exp
	cd ${TMP_DIR2}
	${TEST_SHELL} ${ROOT_DIR}/tp10.sh > ${ROOT_DIR}/out.stdout
	cd ${ROOT_DIR}
	diff tp10.exp out.stdout > result
	if [ ! -s result ] ; then
		tet_result PASS # success
	else
		tet_infoline "diff is `cat result`"
		tet_infoline "examined '[[:xdigit:]] [[:hyphen:]]'"
		tet_infoline "expected '0 1 2 3 4 a b c z -'"
		tet_infoline "in the locale LC_CTYPE = ${LC_CTYPE}"
		tet_infoline "[[:xdigit:]] should contain 'z'('Z')"
		tet_infoline "[[:hyphen:]] should be '-'"
		tet_result FAIL # fail
	fi
	LANG=${OLD_LANG}
	LC_ALL=${OLD_LC_ALL}
	LC_CTYPE=${OLD_LC_CTYPE}
	LC_COLLATE=${OLD_LC_COLLATE}
	unset OLD_LANG
	unset OLD_LC_CTYPE
	unset OLD_LC_ALL
	rm -rf tp10.exp tp10.sh
}

tp9()
{
	tet_infoline "* Verify if a range expression '[c-c]' is matched one of the characters which is in the range. The range expression may not be matched multi-character collating element and the range expression can be based on code point order instead of collating element order."
	tet_infoline " "

	tet_infoline "test shell: ${TEST_SHELL}"
	OLD_LC_CTYPE=${LC_CTYPE}
	OLD_LC_COLLATE=${LC_COLLATE}
	OLD_LC_ALL=${LC_ALL}
	OLD_LANG=${LANG}
	LANG=LTP_1.UTF-8
	LC_ALL=${LANG}
	LC_CTYPE=${LANG}
	LC_COLLATE=${LANG}
        export LANG
        export LC_ALL
        export LC_CTYPE
        export LC_COLLATE

	echo "echo [u-x]" > tp11.sh
	echo "v w x" > tp11.exp
	cd ${TMP_DIR2}
	${TEST_SHELL} ${ROOT_DIR}/tp11.sh > ${ROOT_DIR}/out.stdout
	cd ${ROOT_DIR}
	diff tp11.exp out.stdout > result
	if [ ! -s result ] ; then
		tet_result PASS # success
	else
		tet_infoline "diff is `cat result`"
		tet_infoline "examined '[u-x]'"
		tet_infoline "expected 'v w x'"
		tet_infoline "[u-x] should expand to 'u v w x'"
		tet_result FAIL # fail
	fi
	LANG=${OLD_LANG}
	LC_ALL=${OLD_LC_ALL}
	LC_CTYPE=${OLD_LC_CTYPE}
	LC_COLLATE=${OLD_LC_COLLATE}
	unset OLD_LANG
	unset OLD_LC_ALL
	unset OLD_LC_CTYPE
	unset OLD_LC_COLLATE
	rm -rf tp11.exp tp11.sh
}

startup() # startup function
{
	# this is the shell for test
	TEST_SHELL=bash
	TMP_DIR=tmp
	TMP_DIR2=tmp2
	ROOT_DIR=${PWD}
	${ROOT_DIR}/mkfiles > files
	mkdir ${TMP_DIR}
	mkdir ${TMP_DIR2}
	rm -f out.stdout out.stderr > /dev/null 2>&1
	cd ${TMP_DIR}
	touch `cat ${ROOT_DIR}/files` > /dev/null 2>&1
	cd ${ROOT_DIR}
	cd ${TMP_DIR2}
	touch 0 1 2 3 4 a b c ch v w x y z - _
	cd ${ROOT_DIR}
}


cleanup() # clean-up function
{
	rm -rf  ${TMP_DIR} > /dev/null 2>&1
	rm -rf  ${TMP_DIR2} > /dev/null 2>&1
	rm -f out.stdout out.stderr > /dev/null 2>&1
	rm -f tp1.sh tp2.sh tp3.sh tp4.sh tp5.sh tp6.sh tp7.sh
}

# 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
