#!/bin/sh -
smaller () {
a=`du -k "$1" | awk '{ print $1 }'`
b=`du -k "$2" | awk '{ print $1 }'`
test $a -lt $b
}
checkfile () {
if test $tflag -eq 1; then
gzip -qt < "$1"
fi
}
process () {
prefix="${1%.Z}"
filez="$prefix".Z
filegz="$prefix".gz
if test ! -e "$filez"; then
echo "$prog: $filez does not exist"
return 1
fi
if test ! -f "$filez"; then
echo "$prog: $filez is not a regular file"
return 1
fi
if test -e "$filegz" -a $fflag -eq 0; then
echo "$prog: $filegz already exists"
return 1
fi
tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
echo "$prog: cannot create tmp file"
return 1
}
trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM
if uncompress -f -c < "$filez" | gzip -f -c $gzipflags > "$tmp"; then
if test $kflag -eq 1 && smaller "$filez" "$tmp"; then
echo -n "$prog: $filez is smaller than $filegz"
echo "; keeping it"
rm -f "$tmp"
return 0
fi
if ! checkfile "$tmp"; then
echo "$prog: integrity check of $tmp failed"
rm -f "$tmp"
return 1;
fi
if ! cp -fp "$filez" "$filegz"; then
echo "$prog: warning: could not keep mode of $filez"
fi
if ! cp "$tmp" "$filegz" 2> /dev/null; then
echo "$prog: warning: could not keep mode of $filez"
if ! cp -f "$tmp" "$filegz" 2> /dev/null; then
echo "$prog: could not copy $tmp to $filegz"
rm -f "$filegz" "$tmp"
return 1
fi
fi
if ! touch -fr "$filez" "$filegz"; then
echo -n "$prog: warning: could not keep timestamp of "
echo "$filez"
fi
rm -f "$filez" "$tmp"
else
echo "$prog: failed to process $filez"
rm -f "$tmp"
return 1
fi
}
prog=`basename "$0"`
usage="usage: $prog [-ftv9K] file ..."
fflag=0
tflag=0
kflag=0
gzipflags=
while getopts :ftv9PK i; do
case $i in
f) fflag=1;;
t) tflag=1;;
v) gzipflags="-v $gzipflags";;
9) gzipflags="-9 $gzipflags";;
P) ;;
K) kflag=1;;
\?) echo "$usage"; exit 1;;
esac
done
shift $((OPTIND - 1))
if test $ echo "$usage"
exit 1
fi
rc=0
while test $ if ! process "$1"; then
rc=$?
fi
shift
done
exit $rc