#!/bin/sh
# DocumentId:	$Id: dpsyco-patch 2410 2007-03-21 12:17:27Z ola $
# Author:	$Author: ola $
#		Ola Lundqvist <opal@debian.org>
# Date:		$Date: 2007-03-21 13:17:27 +0100 (Wed, 21 Mar 2007) $
# Arguments:	$1	Source
#		$2	Destination
# Summary:
#	Patches a file structure.
#
# Copyright (C) 2001-2004 Ola Lundqvist <opal@debian.org>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

SOURCE="$1"
DEST="$2"
SKELSRC="$3"
FINDOPT="$4"
#LISTPRE="$5"

if [ -z "$LISTPRE" ] ; then
    LISTPRE=$(echo "$SOURCE" | sed -e "s|/|_|g;")"-"
fi

if [ ! -d "$SOURCE" ] ; then
    echo "Source $SOURCE is not a directory, exiting."
    exit 0
fi

if [ ! -d "$DEST" ] ; then
    echo "Destination $DEST is not a directory, exiting."
    exit 0
fi
DESTAPP=$(echo "$DEST" | sed -e "s|^//*||;")

CDIR=/var/lib/dpsyco-patch/$DESTAPP
mkdir -p $CDIR
OLDLISTF=$CDIR/${LISTPRE}patch.list
LISTF=$CDIR/${LISTPRE}tmppatch.list
NOWAPPLIEDF=$CDIR/${LISTPRE}tmppatch.nowapplied
BDIR=$CDIR/${LISTPRE}patches

touch $OLDLISTF

find $SOURCE -type f $FINDOPT | sed -e "s|^$SOURCE||;" | sed -e "s|^/||;" | grep -v "^$" | sort > $LISTF

SEPREM="< "
SEPADD="> "

# Compare the two patch-skel files and see what files that have been removed.
diff $OLDLISTF $LISTF | grep "$SEPREM" | sed -e "s|^$SEPREM||;" | cut -d '@' -f 1 | {
    while read FILE ; do
	# Reverse apply the removed patches.
	if [ -n "$SKELSRC" -a -e "$SKELSRC/$F" ] ; then
	    # No idea to reverse apply things that exist in skel...
	    true
	else
	    dpsyco-applypatch "$BDIR/$FILE" $DEST "-R"
	fi
    done
}
if [ -e $NOWAPPLIEDF ] ; then
    rm -f $NOWAPPLIEDF
fi
touch $NOWAPPLIEDF
# Compare the two patch-skel files and see what files that have been added.
diff $OLDLISTF $LISTF | grep "$SEPADD" | sed -e "s|^$SEPADD||;" | cut -d '@' -f 1 | {
    while read FILE ; do
	dpsyco-applypatch "$SOURCE/$FILE" $DEST "-N"
	echo $FILE >> $NOWAPPLIEDF
    done
}

# Compare the already applied files with the new ones.
diff $NOWAPPLIEDF $LISTF | grep "$SEPADD" | sed -e "s|^$SEPADD||;" | cut -d '@' -f 1 | {
    while read FILE ; do
	FILES=$(grep "^+++ " "$SOURCE/$FILE" | sed -e "s|^+++ ||" | sed "s|[[:space:]].*||")
	EXISTINSKEL=
	for F in $FILES ; do
	    if [ -e $SKELSRC/$F ] ; then
		EXISTINSKEL=yes
	    fi
	done
	if [ -n "$EXISTINSKEL" ] ; then
	    dpsyco-applypatch "$SOURCE/$FILE" $DEST "-N"
	fi
    done
}

rsync -rlptD -I --delete "$SOURCE/" "$BDIR"

# Store the new skel as the old one.
mv $LISTF $OLDLISTF
rm -f $NOWAPPLIEDF
