#!/bin/sh
# DocumentId:	$Id: dpsyco-patch,v 1.3 2001/09/07 09:06:25 ola Exp $
# Author:	$Author: ola $
#		Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		$Date: 2001/09/07 09:06:25 $
# Arguments:	$1	Source
#		$2	Destination
# Summary:
#	Patches a file structure.

SOURCE="$1"
DEST="$2"
FINDOPT="$3"
LISTPRE="$4"

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
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||;" | {
    while read FILE ; do
	# Reverse apply the removed patch.
	dpsyco-applypatch "$BDIR/$FILE" $DEST "-R"
    done
}
# Compare the two patch-skel files and see what files that have been added.
diff $OLDLISTF $LISTF | grep "$SEPADD" | sed -e "s|^$SEPADD||;" | {
    while read FILE ; do
	# Apply the new patch.
	dpsyco-applypatch "$SOURCE/$FILE" $DEST "-N"
    done
}

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

# Store the new skel as the old one.
mv $LISTF $OLDLISTF
