#!/bin/sh
# DocumentId:	$Id: dpsyco-applypatch,v 1.5 2001/09/10 20:32:07 ola Exp $
# Author:	$Author: ola $
#		Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		$Date: 2001/09/10 20:32:07 $
# Arguments:	$1	Patch file.
#		$2	Destination.
#    (optional) $3	Extra patch options.
# Summary:
#	Patches a file structure.

PF="$1"
DESTR="$2"
PATCHOPT="$3"

if [ -z "PF" ] ; then
    echo "No patch file specified, exiting."
    exit 0
fi
if [ -z "DESTR" ] ; then
    echo "No destination specified, exiting."
    exit 0
fi

DEST=$(echo "$DESTR" | sed -e "s|/$||;")

TF=/var/lib/dpsyco-patch/tmp

if [ ! -f $PF ] ; then
    echo "Can not find patch file $PF."
else
    FILES=$(grep "^+++ " $PF | sed -e "s|^+++ ||" | sed "s|[[:space:]].*||")
    if [ -e $TF ] ; then
	rm -f $TF
    fi
    echo "#\!/bin/sh" > $TF
    for FILE in $FILES ; do
	find "$DEST/$FILE" -prune -printf "chown %u.%g %p\nchmod %m %p\n" >> $TF
    done
    patch -d $DESTR $PATCHOPT -s -f -T -p0 < $PF
    if [ -e $TF ] ; then
	chmod +x $TF
	$TF
	rm -f $TF
    fi
fi
