#!/bin/sh
#
#	tardy - a tar post-processor
#	Copyright (C) 1996, 1999, 2001 Peter Miller;
#	All rights reserved.
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
#
# MANIFEST: Test the -clean functionality
#

umask 022

USER=${USER:-${LOGNAME:-`whoami`}}

#
# Make sure the C sort order is used.
#
unset LANG
unset LANGUAGE

work=/tmp/$$
PAGER=cat
export PAGER

here=`pwd`
if test $? -ne 0 ; then exit 1; fi

if test "$1" != "" ; then bin="$here/$1/bin"; else bin="$here/bin"; fi

fail()
{
	set +x
	echo 'FAILED test of the -clean functionality' 1>&2
	cd $here
	find $work -type d -user $USER -exec chmod u+w {} \;
	rm -rf $work
	exit 1
}
pass()
{
	set +x
	echo PASSED 1>&2
	cd $here
	find $work -type d -user $USER -exec chmod u+w {} \;
	rm -rf $work
	exit 0
}
trap \"fail\" 1 2 3 15

mkdir $work
if test $? -ne 0 ; then fail; fi
cd $work
if test $? -ne 0 ; then fail; fi

#
# test the -clean functionality
#
mkdir foo
if test $? -ne 0 ; then fail; fi
chmod 0755 foo
if test $? -ne 0 ; then fail; fi
for f in 'a&b|c;d' '[^abc]`' "'foo'" '(ha=ha)' "Names With Spaces"
do
	cp /dev/null "foo/$f"
	if test $? -ne 0 ; then fail; fi
	chmod 0644 "foo/$f"
	if test $? -ne 0 ; then fail; fi
done
tar cf infile foo
if test $? -ne 0 ; then fail; fi

$bin/tardy -unu 0 -gnu 0 -clean infile outfile
if test $? -ne 0 ; then fail; fi

$bin/tardy -list outfile /dev/null 2> test.out
if test $? -ne 0 ; then fail; fi

LC_ALL=C
export LC_ALL

sort +4 < test.out > test.out2
if test $? -ne 0 ; then fail; fi

cat > ok << 'fubar'
0755   0   0     0 foo/
0644   0   0     0 foo/--abc--
0644   0   0     0 foo/-foo-
0644   0   0     0 foo/-ha-ha-
0644   0   0     0 foo/Names-With-Spaces
0644   0   0     0 foo/a-b-c-d
fubar
if test $? -ne 0 ; then fail; fi

diff ok test.out2
if test $? -ne 0 ; then fail; fi

#
# Only definite negatives are possible.
# The functionality exercised by this test appears to work,
# no other guarantees are made.
#
pass
