#!/bin/bash

# FULLDIFF
# by CHELU (mental@gmail.com or evil@chelu.ro)
#
# CHANGELOG
#
# FULLDIFF 
# 0.01 - 2004-05-24 CHELU
# - first version, yay (uses dif and fdif)
#
# 0.02 - 2004-05-26 CHELU
# * log showed message for FROMREV too, add +1 to it to show correct log
# * use - instead of _ to separate revision numbers
#
# 0.03 - 2004-05-27 CHELU
# * add date and current working directory name to PATCHFILE
#
# 0.1 - 2004-06-11 CHELU
# * use svn cat to get files from actual revision instead of (wrongly) using wc files
# * make TOREV parameter optional, default to HEAD revision
# * work standalone
# * strip \n from line endings
#
# 0.11 - 2004-06-14 CHELU
# * put gz as extension for gzipped patchfile instead of the wrong tgz
#

# TODO
# * by default sa verifice daca exista deja patchfile si sa il foloseasca pe cel existent
# * adaugat options:
# -q - sa nu mai afiseze lista de fisiere zgip-ate
# -f - sa nu foloseasca patchfile-ul existent
# -v - sa puna optiuni de verbose pe unde se poate
# -h - show usage options


# get and check parameters
FROMREV=$1
TOREV=$2

if [ $# -lt 1 ]; then
	echo Usage: fulldiff FROMREV[ TOREV]
	exit
fi

# get HEAD revision number if user is a lazy bastard
# i think there's a bug that makes svn st -N not be -N after all, so this might take a while
if [ -z "$2" ]; then
	echo FULLDIFF: User is lazy bastard... Getting HEAD revision number
	TOREV=`svn st -u -N | grep "Status against revision:" | sed "s/Status against revision:\ *//"`
fi

# initialize things
DATE=`date +%Y-%m-%d`
CWD=`basename $(pwd)`
# same as above
#CWD=`pwd | sed 's/.*\///'`
REPOPATH=`svn info | grep URL: | sed "s/URL: //"`

PATCHFILE=${DATE}_${CWD}_r${FROMREV}-${TOREV}.patch
GZIPPATCHFILE=${PATCHFILE}.gz
TARFILE=${PATCHFILE}.files.tgz
LOGFILE=${PATCHFILE}.log
TMPDIR=${PATCHFILE}.tmpdir
TMPFILE=${PATCHFILE}.tmpfile

echo FULLDIFF: Creating patchfile from ${FROMREV} to ${TOREV}
# use external diff since svn diff on windows is "broken" 
# and it's output might occasionaly break patch on linux
svn diff --diff-cmd diff -x "-U 3 --binary" -r ${FROMREV}:${TOREV} > ${PATCHFILE}

echo FULLDIFF: Gzipping patchfile
gzip -c -6 ${PATCHFILE} > ${GZIPPATCHFILE}

echo FULLDIFF: Gzipping all files listed in patchfile
mkdir ${TMPDIR}
grep  'Index: .*$' $PATCHFILE | sed 's/Index: //' > ${TMPFILE}

FILELIST=`cat ${TMPFILE}`
for FILE in ${FILELIST}
do
	IDIR=`dirname $FILE`
	IFILE=`basename $FILE`
	mkdir -p ${TMPDIR}/${IDIR}
	svn cat -r${TOREV} ${REPOPATH}/${FILE} > ${TMPDIR}/${FILE}
done

# make 2 steps since tar for windows can't compress
cd ${TMPDIR}
tar -cf ../${TARFILE}.tar *
cd ../
gzip -c -6 ${TARFILE}.tar > ${TARFILE}

rm ${TMPFILE}
rm ${TARFILE}.tar
rm -rf ${TMPDIR}

echo FULLDIFF: Creating logfile
# splits svn log header line output
# 1         2       3     4              5              6
# revision, author, date, time+timezone, date-readable, logmessage line count
let FROMREVPLUSPLUS="FROMREV + 1"
svn log -r ${TOREV}:${FROMREVPLUSPLUS} | grep -E '((. .*$)|(r[0-9]+))' | sed 's/r\([0-9]*\) | \([^|]*\) | \([^ ]*\) \([^ ]* [^ ]*\) (\([^\)]*\)) | \(.*\)/\3/' > ${LOGFILE}



