#!/bin/bash

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_file_check()
{
	for file in $@
	do
	   echo "DEBUG:  \
diff $file $curdir/$file"
	   diff $file $curdir/$file #>/dev/null 2>&1
	   if [ $? -gt 0 ]; then
	     echo "FAIL: The archive file doesn't preserve file contents."
	     echo
	     return
           fi
        done
	echo PASS
	echo
}

tp4()
{
	echo "[cpio test#4]"
	echo "When -i option is specified, if extract files is selected in pattern form, an '*' is matched more than 0 file-system-safe characters."

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	echo "DEBUG:  \
ls $targetfiles | $cpio -o >$tmpdir/$targetarchive"
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	echo "DEBUG:  \
rm $targetfiles"
	rm $targetfiles

	echo "DEBUG:  \
cat $targetarchive | $cpio -i 'あ*.txt' >$tmpdir/out.stdout"
	cat $targetarchive | $cpio -i 'あ*.txt' >out.stdout 2>out.stderr

	cpiofh_file_check あ.txt あいう.txt
	cpiofh_finalize
}

tp5()
{
        echo "[cpio test #5]"
	echo "When -i option is specified, if extract files is selected in pattern form, '?' is matched 1 file-system-safe characters."

	export LANG=$targetlocale
	cpiofh_initialize
	cd $tmpdir

	echo "DEBUG:  \
ls $targetfiles | $cpio -o >$tmpdir/$targetarchive"
	ls $targetfiles | $cpio -o >$targetarchive 2>out.stderr

	echo "DEBUG:  \
rm $targetfiles"
	rm $targetfiles

	echo "DEBUG:  \
cat $targetarchive | $cpio -i 'あ??.txt' >$tmpdir/out.stdout"
	cat $targetarchive | $cpio -i 'あ??.txt' >out.stdout 2>out.stderr

	cpiofh_file_check あいう.txt
	cpiofh_finalize
}

tp6()
{
	echo "[cpio test #6]"
	echo "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."

	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

	cpiofh_file_check あ.txt い.txt
	cpiofh_finalize
}

tp7()
{
	echo "[cpio test #7]"
	echo "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."

	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

	cpiofh_file_check あ.txt い.txt
        cpiofh_finalize
}


# run tests

tp4
tp5
#tp6
#tp7

