#!/bin/bash
#
# re-gzip: for each arg foo, if foo.gz already exists, rm foo if it is the same
#
# because gzip inexplicably doesn't act this way already
#
# useful if you have e.g. unix mboxes or log files that are always appended
# to and sometimes get gzipped and rsync'd elsewhere while still growing.
#
# useful as a workaround for this situation:
#   $ gzip foo
#   gzip: foo.gz already exists; do you wish to overwrite (y or n)?
#
# Gerald Oskoboiny, 5 Sep 2007
#
# $Id: re-gzip,v 1.4 2010/08/04 19:02:07 gerald Exp $
#

for f in "$@"; do
    if [ "$f" != "${f%.gz}" ]; then
        continue
    fi
    if [ -f "$f.gz" ]; then
	n=`cat $f | wc -l`
	if [ `zcat "$f.gz" | head -n $n | md5sum | awk '{print $1}'` \
	   =  `cat "$f"                 | md5sum | awk '{print $1}'` ]; then
	    rm "$f"
	else
	    echo "skipping $f because it differs from $f.gz" >&2
	fi
    else
	gzip "$f"
    fi
done

