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

targetlocale="LTP_1.UTF-8"
targetfilea="テキストファイルfind.txt"
targetfile="テキストファイルfind.txt fiand.txt fibnd.txt ficnd.txt fidnd.txt fivnd.txt fiwnd.txt fi1nd.txt fi2nd.txt fiznd.txt fiあnd.txt fiいnd.txt"

tp1()
{
	tet_infoline "* Verify this utility interprets a pathname which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale

	tmpdir=tmp.findテストディレクトリ
	mkdir $tmpdir
	touch -t 200507011530 $tmpdir/$targetfile
	find $tmpdir >out.stdout 2>out.stderr
	diff find.expout1 out.stdout 2>/dev/null 2>&1

	result_output_code $? "Can't interprets pathnames written in file-system-safe characters." 
	rm -rf $tmpdir
}

tp2()
{
	tet_infoline "* When -exec option is specified, this utility invokes another utility whose name is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale

	scriptname="scfindスクリプト.sh"
	echo "#!/bin/sh" > $scriptname
	echo "pwd" >> $scriptname 
	chmod 755 $scriptname
	touch -t 200507011530 $targetfile
	find .. -name $targetfilea -exec ./$scriptname \; >out.stdout 2>out.stderr
	test `pwd` = `cat out.stdout` >/dev/null 2>&1

	result_output_code $? "Can't invoke another utility whose name is written in file-system-safe characters." 
	rm -f $targetfile $scriptname
}

tp3()
{
	tet_infoline "* When -exec option is specified, this utility replaces '{}' with adapted pathnames which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name $targetfilea -exec ls -l {} \; >out.stdout 2>out.stderr
	fgrep $targetfilea out.stdout >/dev/null 2>&1

	result_output_code $? "Can't replace \`{}\' with adapted pathnames which is written in file-system-safe characters." 
	rm $targetfile
}

tp4()
{
	tet_infoline "* When -print option is specified, verify this utility prints filenames which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale

	tmpdir=tmp.findテストディレクトリ
	mkdir $tmpdir
	touch -t 200507011530 $tmpdir/$targetfilea
	find $tmpdir -print >out.stdout 2>out.stderr
	diff find.expout1 out.stdout >/dev/null 2>&1

	result_output_code $? "Can't print filenames written in file-system-safe characters." 
	rm -rf $tmpdir
}

tp5()
{
	tet_infoline "* If -name option is specified and the search pattern has ordinary file-system-safe characters, verify the character is matched itself."
	tet_infoline " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name $targetfilea -print >out.stdout 2>out.stderr
	diff find.expout5 out.stdout >/dev/null 2>&1

	result_output_code $? "Can't handle search pattern in ordinary file-system-safe characters." 
	rm $targetfile
}

tp6()
{
	tet_infoline "* If -name option is specified and the search pattern has '?', verify '?' is matched any file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name 'テキストファイ?find.txt' -print >out.stdout 2>out.stderr
	diff find.expout5 out.stdout >/dev/null 2>&1

	result_output_code $? "Can't handle search pattern including \`?'\." 
	rm $targetfile
}

tp7()
{
	tet_infoline "* If -name option is specified and the search pattern has '*', verify '*' is matched any string written in file-system-safe characters."
	tet_infoline " "
	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name 'テキ*nd.txt' -print >out.stdout 2>out.stderr
	diff find.expout5 out.stdout >/dev/null 2>&1

	result_output_code $? "Can't handle search pattern including \`*'\." 
	rm $targetfile
}

tp8()
{
	tet_infoline "* If -name option is specified and the search pattern has a matching list expression '[...]', verify the matching list expression is matched the character with the file-system-safe character which is in the list."
	tet_infoline " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name 'テキストファイ[ラリルレロ]find.txt' -print >out.stdout 2>out.stderr
	diff find.expout5 out.stdout >/dev/null 2>&1

	result_output_code $? "Can't handle search pattern including \`[...]'\." 
	rm $targetfile
}

tp9()
{
	tet_infoline "* If -name option is specified and the search pattern has a character class'[:class:]', verify the character class is matched a character which defined on the current locale."
	tet_infoline " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name 'fi[[:xdigit:]]nd.txt' -print >out.stdout 2>out.stderr

#	diff find.expout9 out.stdout >/dev/null 2>&1
	resultflag=0
	while read line
	do 
	  grep ${i} out.stdout >/dev/null 2>&1
	  if [ $? != 0 ]; then
		result_flag=1
	  fi
	done < find.expout9

	result_output_code $resultflag "Can't handle search pattern including character class \`[:class:]'\." 
	rm $targetfile
}

tp10()
{
	tet_infoline "* If -name option is specified and the search pattern has a range expression '[c-c] ', verify the range expressions is matched a character 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 " "

	export LANG=$targetlocale

	touch -t 200507011530 $targetfile
	find .. -name 'fi[あ-う]nd.txt' -print >out.stdout 2>out.stderr
#	diff find.expout10 out.stdout >/dev/null 2>&1
	resultflag=0
	while read line
	do 
	  grep ${i} out.stdout >/dev/null 2>&1
	  if [ $? != 0 ]; then
		result_flag=1
	  fi
	done < find.expout10

	result_output_code $? "Can't handle search pattern including range expression \`[c-c]'\." 
	rm $targetfile
}

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
