#!/bin/ksh
#
# $Revision: 1.2 $ $Date: 2012-11-05 21:42:29-05 $
# $Source: /home/vogelke/bin/RCS/compare-compressed,v $
# $Host: sys7.com $
# $UUID: 9304272e-071c-4d32-8eef-639eba6cb894 $
#
#<compare-compressed: if FILE and FILE.{xz,gz,bz2,Z} exist, uncompress
# and compare them.  If they match, delete the uncompressed one.

export PATH=/usr/local/bin:/bin:/sbin:/usr/sbin:/usr/bin
tag=${0##*/}

warn () { echo "$tag: WARN: $@"  >&2; }

# -----------------------------------------------------------------------
# What type of compression are we using?
# Print the program name if we recognize it; otherwise return failure.

ctype () {
    ext=$1
    rc=0

    case "$ext" in
        bz2) echo bunzip2 ;;
        gz)  echo gunzip ;;
        Z)   echo gunzip ;;
        xz)  echo unxz ;;
        *)   echo ''; rc=1 ;;
    esac

    return $rc
}

# -----------------------------------------------------------------------
# Accept a filename, see if it has a recognized extension.
# If so, it's compressed -- generate the regular filename (rname).
# If not, generate the compressed filename (cname).
# Then compare the two files.

check () {
    file=$1

    # Do we have a compressed filename?

    ext=${file##*.}   #; echo "[$file][$ext]"
    cname=$file
    rname=$(basename $file .$ext)
    prog=$(ctype $ext) || { rname=$file; cname=""; }

    test -f "$rname" || { warn "$rname: not found"; return; }

    # If cname is not set, we have a regular filename.  I'm not dealing
    # with pathological crap like both FILE.gz and FILE.xz exist, etc.

    case "$cname" in
        "") for ext in Z bz2 gz xz; do
                prog=$(ctype $ext)
                cname="$file.$ext"
                test -f "$cname" && break
            done ;;
        *) ;;
    esac

    test -f "$cname" || { warn "$cname: not found"; return; }

    case "$prog" in
        "") warn "$cname: unfamiliar compression"; return ;;
        *)  ;;
    esac

    # Now compare the two files.

    set X $($prog -c $cname | gmd5sum); csum=$2
    set X $(gmd5sum $rname);            rsum=$2
    test $csum = $rsum && echo rm $rname
}

# -----------------------------------------------------------------------
# Check each argument.

for file in $@; do
    check $file
done

exit 0
