#!/bin/sh

. patchfns 2>/dev/null ||
. /usr/lib/patch-scripts/patchfns 2>/dev/null ||
. $PATCHSCRIPTS_LIBDIR/patchfns 2>/dev/null ||
{
	echo "Impossible to find my library 'patchfns'."
	echo "Check your install, or go to the right directory"
	exit 1
}

TMP=/usr/src/pull
if [ ! -d $TMP ]
then
	echo $TMP does not exist
	exit 1
fi

here=$(/bin/pwd)
cd $TMP
patch_names=$(echo *.patch)
if [ "$patch_names" = "*.patch" ]
then
	echo "no patches in $TMP"
	exit 1
fi
cd $here

tmp=$(mktemp /tmp/XXXXXX)

doit()
{	echo $1
	cp $TMP/$1 patches
	pcpatch $1
#	sed -e '/^diff/q' < $TMP/$1 > txt/$(stripit $1).txt

	if grep '^+++' patches/$1 > /dev/null
	then
		sed -e "s/^#$1/$1/" < series > $tmp
	else
		sed -e "s/^$1/\#$1/" < series > $tmp
	fi
	cat $tmp > series
}

for i in $patch_names
do
	doit $i
done

rm -f $tmp
rm -rf $TMP.old
mv $TMP $TMP.old
