#!/bin/sh

# FIXME: $1 and $2 have not been checked properly, they may contain
# harmful characters such as backquote and semicolon -- Abel

[ -r "$1" ] || exit 1

PATH=/bin:/usr/bin

uncompress=

if [ -x "/bin/file" -o -x "/usr/bin/file" ]; then
    uncompress=`file "$1" | sed -n "s/.*\(gzip\|bzip2\).*/\1/p; s/.*compress'd.*/gzip/p;"`
else
    echo "$1" | grep -q '\.g\?z$\|\.Z$' && uncompress=gzip
    echo "$1" | grep -q '\.bz2$' && uncompress=bzip2
fi

[ -z "$uncompress" ] && exit 2
if [ -z "$2" ]; then
    exec $uncompress -dc "$1"
else
    # make sure destination is clean
    rm -f "$2"
    [ -e "$2" ] && exit 3
    touch "$2"
    exec $uncompress -dc "$1" > "$2"
fi
