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

targetlocale="LTP_1.UTF-8"
targetfiles="あ.txt あいう.txt かき.txt い.txt m.txt n.txt o.txt p.txt"
targetarchive="../LTP_1.cpio"
curdir=`pwd`
cpio='cpio --format=newc'

cpiofh_initialize()
{
	cd $curdir
        tmpdir=tmp.XXXXXX
	rm -rf $tmpdir
	mkdir $tmpdir
	for file in $targetfiles
	do
	   echo "$file" > $tmpdir/$file
	   echo "$file" > $curdir/$file
 	   touch -t 200507011530 $tmpdir/$file $curdir/$file
	done
}

cpiofh_finalize()
{
    cd $curdir
    rm -rf $tmpdir
}


cpiofh_content_check()
{
        checktarget=$1
	shift
	for file in $@
	do
	   cat $checktarget | $cpio -i $file >out.stdout 2>out.stderr
	   diff $file $curdir/$file >/dev/null 2>&1
	   if [ $? -gt 0 ]; then
	     echo FAIL
	     tet_infoline "The archive file can't preserve file contents."
             return
           fi
        done
	echo PASS
}

cpiofh_file_check()
{
	for file in $@
	do
	   diff $file $curdir/$file >/dev/null 2>&1 
	   if [ $? -gt 0 ]; then
	     echo FAIL
	     tet_infoline "The archive file doesn't preserve file contents."
	     return
           fi
        done
	echo PASS
}

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

	export LANG=$targetlocale
	cpiofh_initialize

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	tet_result `cpiofh_content_check $targetarchive $targetfiles`
        cpiofh_finalize
}

tp2()
{
	tet_infoline "* When -o and -v option is specified, verify this utility prints the names of the affected files which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/cpio-fh.expout2"

	ls $targetfiles > $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -ov >$targetarchive 2>out.stderr
	head -n 8 out.stderr | diff - $expfile >/dev/null 2>&1

	result_output_code $? "Can't print file names written in file-system-safe-characters."
	rm $expfile
	cpiofh_finalize
}

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

	export LANG=$targetlocale

	cpiofh_initialize
	cd $tmpdir

	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm $targetfiles

	tet_result `cpiofh_content_check $targetarchive $targetfiles`
        cpiofh_finalize
}

tp4()
{
	tet_infoline "* When -i option is specified, if extract files is selected in pattern form, an '*' is matched more than 0 file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm $targetfiles

        cat $targetarchive | $cpio -i 'あ*.txt' >out.stdout 2>out.stderr
	
	tet_result `cpiofh_file_check あ.txt あいう.txt`
	cpiofh_finalize
}

tp5()
{
	tet_infoline "* When -i option is specified, if extract files is selected in pattern form, '?' is matched 1 file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm $targetfiles

	cat $targetarchive | $cpio -i 'あ??.txt' >out.stdout 2>out.stderr

	tet_result `cpiofh_file_check あいう.txt`
	cpiofh_finalize
}

tp6()
{
	tet_infoline "* When -i option is specified, if extract files is selected in pattern form, matching list expression '[...]' is matched any one of the enclosed file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm $targetfiles

	cat $targetarchive | $cpio -i '[あいう].txt' >out.stdout 2>out.stderr

	tet_result `cpiofh_file_check あ.txt い.txt`
	cpiofh_finalize
}

tp7()
{
	tet_infoline "* When -i option is specified, if extract files is selected in pattern form, a range expression '[c-c]' is matched any symbol between the pair (inclusive). 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
	cpiofh_initialize
	cd $tmpdir

	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm $targetfiles

	cat $targetarchive | $cpio -i '[あ-う].txt' >out.stdout 2>out.stderr

	tet_result `cpiofh_file_check あ.txt い.txt`
        cpiofh_finalize
}

tp8()
{
	tet_infoline "* When -i and -d option is specified, verify this utility creates the directory whose name is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	tmpfolder="フォルダ"
	mkdir $tmpfolder
	mv $targetfiles $tmpfolder
	ls $tmpfolder/* | $cpio -o >$targetarchive 2>out.stderr
	rm -rf $tmpfolder

	cat $targetarchive | $cpio -i -d 2>out.stderr
	cd $tmpfolder

	tet_result `cpiofh_file_check $targetfiles`
	cd ../../$tmpdir
        cpiofh_finalize
}

tp9()
{
	tet_infoline "* When -i and -t option is specified, verify this utility prints the filename which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/out.expout9"
	ls -1 $targetfiles > $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	cat $targetarchive | $cpio -i -t >out.stdout 2>out.stderr
	diff out.stdout $expfile >/dev/null 2>&1

	result_output_code $? "Can't print file names written in file-system-safe-characters."
	rm $expfile
        cpiofh_finalize
}

tp10()
{
	tet_infoline "* When -i and -v option is specified, verify this utility prints the filename which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/cpio-fh.expout10"
	ls -1 $targetfiles > $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr
	rm -f $targetfiles

	cat $targetarchive | $cpio -i -v >out.stdout 2>out.stderr
	head -n 8 out.stderr | diff - $expfile >/dev/null 2>&1

	result_output_code $? "Can't print file names written in file-system-safe-characters."
	rm $expfile
        cpiofh_finalize
}

tp11()
{
	tet_infoline "* When -p option is specified, verify this utility handles directory name which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/out.expout11"
	ls -1 $targetfiles > $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	outdir="出力場所"
	mkdir $outdir
	ls $targetfiles | cpio -p $outdir >/dev/null 2>&1
	ls $outdir >out.stdout 2>out.stderr
	diff out.stdout $expfile >/dev/null 2>&1

	result_output_code $? "Can't handle directory names written in file-system-safe-characters."
	rm -rf $expfile $outdir
        cpiofh_finalize
}

tp12()
{
	tet_infoline "* When -p and -d option is specified, verify this utility creates the directory whose name is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/out.expout12"
	ls -1 $targetfiles > $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	outdir="出力場所"
	ls $targetfiles | cpio -p -d $outdir >/dev/null 2>&1
	ls $outdir >out.stdout 2>out.stderr
	diff out.stdout $expfile >/dev/null 2>&1

	result_output_code $? "Can't handle directory names written in file-system-safe-characters."
	rm -rf $expfile $outdir
        cpiofh_finalize
}

tp13()
{
	tet_infoline "* When -p and -v option is specified, verify this utility prints the filename which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	cpiofh_initialize

	expfile="$curdir/out.expout13"
	rm -f $expfile
	outdir="出力場所"
	for file in `ls $targetfiles`
	do
	   echo "$outdir/$file" >> $expfile
	done
	echo "1 block" >> $expfile

	cd $tmpdir
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	mkdir $outdir
	ls $targetfiles | cpio -p -v $outdir >/dev/null 2>out.stderr
	diff out.stderr $expfile >/dev/null 2>&1

	result_output_code $? "Can't print file names written in file-system-safe-characters."
	rm -rf $expfile $outdir
        cpiofh_finalize
}

startup() # startup function
{
	rm -f out.stdout out.stderr >/dev/null 2>&1
}


cleanup() # clean-up function
{
	rm -rf $tmpdir $targetfiles >/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
