#!/bin/bash

#
# fedora-diffarchive
# Diff contents of two archives
#
# Author:  Ville Skyttä <ville.skytta at iki.fi>
# License: GPL
# $Id: fedora-diffarchive,v 1.2 2005/03/15 12:42:05 scop Exp $

set -e

unset CDPATH
tmpdir=
diffopts=

trap cleanup EXIT
cleanup()
{
    set +e
    [ -z "$tmpdir" -o ! -d "$tmpdir" ] || rm -rf "$tmpdir"
}

usage()
{
    cat <<EOF
Usage: `basename $0` [diff-options] from-archive to-archive
  diff-options  Options passed to diff(1), in addition to -r (default: -Nu).
                The first argument not starting with a '-' ends diff-options.
EOF
}

while true ; do
    case "$1" in
        -*) diffopts="$diffopts $1" ;;
        *)  break ;;
    esac
    shift
done
if [ $# -lt 2 ] ; then
    usage
    exit 1
fi

tmpdir=`mktemp -d /tmp/diffarchive.XXXXXX`

mkdir "$tmpdir/old" "$tmpdir/new"
fedora-extract -q -C "$tmpdir/old" "$1"
fedora-extract -q -C "$tmpdir/new" "$2"

# It would be nice if fedora-extract could do some of the chmods.
find "$tmpdir"/* -type d -exec chmod u+rx {} ';' # Note: -exec, not xargs here.
chmod -R u+rw "$tmpdir"/*
cd "$tmpdir"

# Did the archives uncompress into base dirs?
if [ `ls -1d old/* | wc -l` -eq 1 ] ; then
  old=`ls -1d old/*`
else
  old=old
fi
if [ `ls -1d new/* | wc -l` -eq 1 ] ; then
  new=`ls -1d new/*`
else
  new=new
fi

# Fixup base dirs to the same level.
if [ `basename "$old"` != `basename "$new"` ] ; then
  if [ "$old" != old ] ; then
    mv "$old" .
    old=`basename "$old"`
  fi
  if [ "$new" != new ] ; then
    mv "$new" .
    new=`basename "$new"`
  fi
fi

# Here we go.
diff -r ${diffopts:--Nu} "$old" "$new"
