#!/bin/ksh
# @(#) tarz.ksh 1.0 94/04/04
# 92/10/09 john h. dubois iii (john@armory.com)
# 93/04/15 Added H option.  Fixed f option.
# 94/04/04 Added q option.

name=${0##*/}
Usage="Usage: $name [-fhHv] dirname ..."
check=true
H=
vflag=-v

while getopts :fhHv opt; do
    case $opt in
    h) echo \
"$name: archive a directory.
$Usage
The contents of each dirname are tarred and compressed into an archive in the
current directory with the name dirname.tar.Z.  Pathnames in the archives
are relative to dirname.
Options:
-h: Print this help.
-f: Overwrite dirname.tar and dirname.tar.Z if they exist.
    Normally, processing will be aborted if either exists.
-q: Quiet: Do not give v flag to compress.
-H: Give -H flag to compress (higher compression on SCO systems)."
	exit 0
	;;
    f)  check=false;;
    H) H=-H;;
    q) unset vflag;;
    +?)
	print -u2 "$name: options should not be preceded by a '+'."
	exit 1
	;;
    ?) 
	print -u2 "$name: $OPTARG: bad option.  Use -h for help."
	exit 1
	;;
    esac
done
 
# remove args that were options
let OPTIND=OPTIND-1
shift $OPTIND

if [ $# -lt 1 ]; then
    print -u2 "$Usage\nUse -h for help."
    exit
fi

OWD=$PWD

for dirname; do
    cd $OWD
    archname=${dirname##*/}.tar
    if $check; then
	if [ -f $archname ]; then
	    echo \
    "$archname: file exists.  Use the -f option to overwrite.  Skipping." 1>&2
	    continue
	fi
	if [ -f $archname.Z ]; then
	    echo "$archname.Z: file exists.  Skipping." 1>&2
	    continue
	fi
    fi
    if cd $dirname; then :; else
	echo "Could not cd to $dirname.  Not processed." 1>&2
	continue
    fi
    tar cf - * | compress $H $vflag > $OWD/$archname.Z
done
