#!/bin/sh

TARBALL=$1
if [[ ! -e "$TARBALL" ]]; then echo "Enter the tarball as the option. It must be a .tar.gz file or this script will not work"; exit 1; fi
if [[ -e "packer/patches/differ.diff" ]]; then echo "A patch has already been generated by this program and is in packer/patches/differ.diff. It must be removed in order to run this program again."; exit 1; fi
if [[ ! -e "packer/patches" ]]; then echo "Packer has not been run in this directory. Exiting..."; exit 1; fi

echo Generating diff...
mkdir -p packer/patches
RDIR=`mktemp -d /tmp/differ-XXXXXX`
CWD=`pwd`
DIRNAME=`basename $CWD`
cd $RDIR
tar -xzf $CWD/$1
cp -r $CWD ${DIRNAME}n
cd ${DIRNAME}n
rm -rf debian rpm slack autopackage gentoo packer
cd ..
diff -ruN $DIRNAME ${DIRNAME}n > ${CWD}/packer/patches/differ.diff
cd $CWD
#rm -rf $RDIR
echo Patch outputted to packer/patches/differ.diff.
