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

orgdir=`pwd`
targetlocale="LTP_1.UTF-8"
targetfile="LTParch.tar"
contentfiles="第1フォルダ/第2フォルダ/ファイルa.txt 第1フォルダ/ファイルb.txt ファイルc.txt"
appendfiles="第1フォルダ/ファイルd.txt"

tar_initialize()
{
	tmpdir=tmp.XXXXXX
	rm -rf $tmpdir
	mkdir $tmpdir
	cd $tmpdir
	mkdir -p 第1フォルダ/第2フォルダ
	for file in $contentfiles
	do
	    echo "$file" > $file
	done
	touch -t 200507011530 $contentfiles
	touch -t 200507011530 第1フォルダ 第1フォルダ/第2フォルダ
	cd $orgdir
}

tar_finalize()
{
    cd $orgdir
    rm -rf $tmpdir
}

tar_content_check()
{
        ctmpdir="content_tmp"
	mkdir $ctmpdir
	mv $1 $ctmpdir
	cd $ctmpdir
	cat $1 | tar x 2>/dev/null
	shift
	for file in $@
	do
	   diff $file ../$file >/dev/null 2>&1
           if [ $? -gt 0 ]; then
		tet_infoline "The archive file can't preserve file contents."
		echo FAIL
		return
           fi	
 	done
	echo PASS
}

tp1()
{
	tet_infoline "* When keyword 'r' is specified, verify this utility attaches files whose names are written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar cf $targetfile $contentfiles 2>out.stderr
	tar rf $targetfile $appendfile 2>out.stderr

	tet_result `tar_content_check $targetfile $contentfiles $appendfile`
	tar_finalize
}

tp2()
{
	tet_infoline "* When keyword 'c' is specified, verify this utility packed files whose names are written in file-system-safe characters into an archive."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar c $contentfiles > $targetfile 2>out.stderr

	tet_result `tar_content_check $targetfile $contentfiles`
	tar_finalize
}

tp3()
{
	tet_infoline "* When keyword 't' is specified, verify this utility prints filenames which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar c $contentfiles > $targetfile 2>out.stderr
	cat $targetfile | tar t >out.stdout 2>out.stderr
	diff out.stdout $orgdir/tar-fh.expout3 >/dev/null 2>&1

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

tp4()
{
	tet_infoline "* When keyword 'u' is specified, verify this utility appends a given file if the archive has a file which is same name as the given one and the given one is newer. The names of these files are written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar c $contentfiles > $targetfile 2>out.stderr
	touch -t 200507021530 $contentfiles
	tar uf $targetfile $contentfiles 2>out.stderr

	tet_result `tar_content_check $targetfile $contentfiles`
	tar_finalize
}

tp5()
{
	tet_infoline "* When keyword 'x' is specified, verify this utility extracts files whose names are written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar c $contentfiles > $targetfile 2>out.stderr
	tet_result `tar_content_check $targetfile $contentfiles`
	tar_finalize
}

tp6()
{
	tet_infoline "* When keyword 'f' is specified, verify this utility handles the filename which is written in file-system-safe characters."
	tet_infoline " "

	export LANG=$targetlocale
	tar_initialize

	cd $tmpdir
	tar c $contentfiles > $targetfile 2>out.stderr
	myarchive="マイアーカイブ.tar"
	mv $targetfile $myarchive
	tar tf $myarchive >out.stdout 2>out.stderr
	diff out.stdout $orgdir/tar-fh.expout3 >/dev/null 2>&1

	result_output_code $? "Can't handle filenames written in file-system-safe characters."
	tar_finalize
}

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


cleanup() # clean-up function
{
	rm -rf out.stdout out.stderr $targetfile >/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
