#! /bin/sh
#
# tz		--- tar, compress and uuencode files and directories
#
# See also: shar
#
# Author: Oscar Nierstrasz (oscar@cui.unige.ch) -- 23/5/91

u='Usage: tz [options] <file|dir> ...
	-k	-- keep originals (default: mv to .junk)
	-tz	-- tar and compress (default)
	-tzu	-- tar, compress and uuencode
	-zu	-- just compress and uuencode
	-z	-- just compress
	-u	-- just uuencode '

mode=-tz
keep=n

case $# in
0 ) echo "$u" 1>&2 ; exit ;;
esac

for arg
do
	case $arg in
	-tz )	mode=$arg ; continue ;;
	-tzu )	mode=$arg ; continue ;;
	-zu )	mode=$arg ; continue ;;
	-u )	mode=$arg ; continue ;;
	-z )	mode=$arg ; continue ;;
	-k )	keep=y ; continue ;;
	-* )	echo "$u" 1>&2 ; exit ;;
	* )	dir=`dirname $arg`
		case $keep in
		n )	junk=$dir/.junk
			if test ! -d $junk
			then
				if test -f $junk
				then	echo "tz: $junk not a directory" 1>&2
					exit
				else	mkdir $junk
				fi
			fi ;;
		esac
	esac

	case $arg in
	*tar ) tar xvf $arg ;;
	*tar.Z ) zcat $arg | tar xvf - ;;
	*tar.Z.uu ) uudecode $arg
		tz=$dir/`basename $arg .uu`
		zcat $tz | tar xvf -
		rm $tz ;;
	*.Z.uu ) uudecode $arg
		z=$dir/`basename $arg .uu`
		uncompress $z ;;
	*.Z )	f=$dir/`basename $arg .Z`
		zcat $arg > $f ;;
	*.uu )	uudecode $arg ;;
	* )	case $mode in
		-tz )	tz=${arg}.tar.Z
			tar cvf - $arg | compress -c > $tz
			echo "created $tz" 1>&2 ;;
		-tzu )	tz=${arg}.tar.Z
			tar cvf - $arg | compress -c > $tz
			uuencode $tz $tz > $tz.uu
			rm $tz
			echo "created $tz.uu" 1>&2 ;;
		-zu )	z=${arg}.Z
			compress -c $arg > $z
			uuencode $z $z > $z.uu
			rm $z
			echo "created $z.uu" 1>&2 ;;
		-z )	z=${arg}.Z
			compress -c $arg > $z
			echo "created $z" 1>&2 ;;
		-u )	uuencode $arg $arg > $arg.uu
			echo "created $arg.uu" 1>&2 ;;
		esac ;;
	esac
	case $keep in
	n )	mv $arg $junk
		echo "tz: $arg moved to $junk" ;;
	esac
done

exit

