#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Create-Diff
# ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

directories="Documentation misc package architecture target scripts"

if [ "$1" = "-editor" ] ; then
        editor="$2" ; shift ; shift
fi

if [ "$#" -lt 2 ] ; then
	echo ; echo "  Usage: $0 [ -editor <editor> ] <old-dir> <new-dir> [ {file|directory} ]"
	echo
	echo "  Creates a unified diff over two ROCK source trees. You can"
	echo "  send this diff to the mailing list if you want your changes"
	echo "  to be included."
	echo
	echo "  Please do create seperate diffs for changes which do not"
	echo "  belong to each other."
	echo ; exit 1
else
	olddir=$1; shift
	newdir=$1; shift
	[ "$1" ] && directories="$*"
fi

if ! perl -e 'exit 0' ; then
	perl() { cat ; }
fi

eval "`grep -A 10 '\[BEGIN\]' $olddir/scripts/parse-config | grep -B 10 '\[END\]'`"

[ "$editor" = "" ] && echo "[ Generated by ./scripts/Create-Diff for ROCK Linux $rockver ]" > /tmp/$$

for x in $directories ; do
	x=${x#./}; x=${x%/}
	if [ -d $x ]; then
		FLAGS="-rduN"
		name_for_diff="$x/."
		source_for_sed="$x/./"
		target_for_sed="$x/"
	else
		FLAGS="-duN"
		name_for_diff="$x"
		source_for_sed=""
		target_for_sed=""
	fi
	diff -x CVS -x '.svn' -x '.#*' -x '*.mine' -x '*.r[1-9][0-9]*' \
		$FLAGS "$olddir"/$name_for_diff "$newdir"/$name_for_diff | grep -v '^diff ' | \
	sed "s,^--- $olddir/$source_for_sed,--- rock-old/$target_for_sed," | \
	sed "s,^+++ $newdir/$source_for_sed,+++ rock-new/$target_for_sed," | \
	perl -pe '$fn=$1 if /^--- rock-old\/(\S+)\s/;
		$_="" if $fn eq "Documentation/FAQ";
		$_="" if $fn eq "Documentation/LSM";
		$_="" if $fn =~ /~$/'
done | perl -w `dirname $0`/patch-pp.pl "$newdir" >> /tmp/$$

if [ "$editor" = "" ]; then 
	cat /tmp/$$
	rm /tmp/$$
else
	eval "$editor /tmp/$$"
	rm /tmp/$$
fi