#
# Work out where the user's pc/, patch/ and txt/ directories live.
#
# If the user specified PATCHSCRIPTS in environment then use that (it's
# probably a relative path)
#
# If there is a directory ./patch-scripts then use that
#
# Otherwise use "."
#

PATCHSCRIPTS_LIBDIR=$(dirname $0)

if [ x$PATCHSCRIPTS != x ]
then
	P=$PATCHSCRIPTS
elif [ -d ./quilt ]
then
	P=./quilt
elif [ -d ./patch-scripts ]
then
	P=./patch-scripts
elif [ -d ./patches ]
then
	P=.
fi

DB="$P/applied-patches"

__cat_series()
{
	grep -v '^#' $1
}

cat_series()
{
	__cat_series $P/series
}

top_patch()
{
	tail -1 $DB
}

die()
{
	echo error: $*
	exit 1
}

is_numeric()
{
	if echo $1 | egrep '^[0-9]*$' > /dev/null
	then
		return 0
	fi
	return 1
}

is_applied_last()
{
	name="$(stripit $1)"
	top_patch >$DB.1
	if grep "^$name$" "$DB.1" > /dev/null 2>&1
	then
		rm $DB.1
		return 0
	else
		rm $DB.1
		return 1
	fi
}

is_applied()
{
	name=$(stripit "$1")
	if grep "^$name$" "$DB" > /dev/null 2>&1
	then
		return 0
	else
		return 1
	fi
}

is_in_series()
{
	name=$(stripit "$1")
	if grep "^$name.patch$" $P/series > /dev/null 2>&1
	then
		return 0
	else
		return 1
	fi
}

can_apply()
{
	pn=$(stripit $1)
	if ! grep "^+++" "$P/patches/$pn.patch" > /dev/null
	then
		return 0
	elif patch -p1 --dry-run -i "$P/patches/$pn.patch" -f
	then
		return 0
	else
		return 1
	fi
}

can_remove()
{
	if ! grep "^+++" $P/patches/"$1".patch > /dev/null
	then
		return 0
	elif patch -R -p1 --dry-run -i $P/patches/"$1".patch -f
	then
		return 0
	else
		return 1
	fi
}

stripit()
{
	ret=$(basename $1)
	ret=$(echo $ret | sed -e 's/\.patch$//')
	ret=$(echo $ret | sed -e 's/\.pc$//')
	ret=$(echo $ret | sed -e 's/\.txt$//')
	echo $ret
}

remove_from_db()
{
	tmpfile=$(mktemp /tmp/p_XXXXXX)
	name="$1"
	sed -e "/^$name$/d" < "$DB" > $tmpfile
	mv $tmpfile "$DB"
}

remove_from_series()
{
	tmpfile=$(mktemp /tmp/p_XXXXXX)
	name=$(stripit "$1")
	sed -e "/^$name.patch$/d" < $P/series > $tmpfile
	cp -H $tmpfile $P/series
	rm -f $tmpfile
}

next_patch()
{
	top=$($PATCHSCRIPTS_LIBDIR/toppatch)
	last_in_series=$(stripit $(cat_series | tail -1))
	if [ "$last_in_series" == "$top" ]
	then
		return 0
	fi
	todo=$(cat_series | grep -C1 "^$top\.patch" | tail -1)
	if [ x$todo = x ]
	then
		todo=$(cat_series | head -1)
	fi
	echo $todo
}

file_in_patch()
{
	file=$1
	patch=$2

	if [ -e $P/pc/$patch.pc ]
	then
		if grep "^"$file"$" $P/pc/$patch.pc > /dev/null
		then
			return 0
		fi
	fi
	return 1
}

# copy_file_to_bup filename patchname
copy_file_to_bup()
{
	file=$1
	patch=$2
	bup="$file"~"$patch"

	if [ -e $bup ]
	then
		echo "Cannot install file $file in patch $patch: backup $bup exists"
		exit 1
	fi

	if [ -e $file ]
	then
		cp $file "$file"~"$patch"
	else
		echo "file $file appears to be newly added"
	fi
}

mergebase_name()
{
	file=$1
	PATCH_NAME=$2
	echo $(dirname "$file")/.$(basename "$file")~mergebase~"$PATCH_NAME"
}

delete_mergebase_files()
{
	PATCH_NAME=$1
	FILES=$(cat $P/pc/$1.pc)
	for file in $FILES
	do
		rm -f $(mergebase_name "$file" $1)
	done
}

install_file_in_patch()
{
	file=$1
	patch=$2

	copy_file_to_bup $file $patch
	echo $file >> $P/pc/$patch.pc
}

need_file_there()
{
	if [ ! -e $1 ]
	then
		echo "File $1 does not exist"
		exit 1
	fi
}

desc()
{
	state=0
	while read x
	do
		if [ x"$x" = xDESC ]
		then
			state=1
		elif [ x"$x" = xEDESC ]
		then
			state=0
		elif [ $state = 1 ]
		then
			echo "  $x"
		fi
	done
}

body()
{
	file=$1

	did_stuff=0
	while read x
	do
		if [ x"$x" = xEDESC ]
		then
			cat
			did_stuff=1
		fi
	done < $file

	if [ $did_stuff = 0 ]
	then
		cat $file
	fi
}

check_patch()
{
	if grep "^+.*[ 	]$" $P/patches/$(stripit $1).patch
	then
		echo warning: $1 adds trailing whitespace
	fi
}
