#!/bin/sh
#
# Upgrade an existing package
# Christoph Lameter, December 24, 1996
#
# upgrade [-v <Version>] <new upstream archive>[.tar.gz|tgz|zip]
# upgrade [-v <Version>] -p <patch>.gz
#
# Has to be called from sourcearchive position

LIB=/usr/lib/deb-make

# Match Pattern to extract a new version number from a given filename.
# I already had to fiddle with this a couple of times so I better put it up
# at front:
MPATTERN="[a-zA-Z0-9_+]*-*\([0-9]*\.[0-9\.]*\)"

set -e

# Process Parameters
while [ "$1" ]; do
	case $1 in
	-v)	shift
		NEW_VERSION="$1"
		;;
	-p)	shift
		PATCH="$1"
		;;
	*)	ARCHIVE=$1
		;;
	esac
	shift
done

# Get Parameters from current source archive

# Look for debian/changelog
until [ -f debian/changelog ]; do
        cd ..
        if [ `pwd` = "/" ]; then
		echo "Cannot find debian/changelog anywhere."
		exit 1
	fi
done

# Figure out package info we need
LINE=`head -1 debian/changelog`
PACKAGE=`expr "$LINE" : '\(.*\) (.*)'`
VERSION=`expr "$LINE" : '.* (\(.*\))'`
UVERSION=`expr "$VERSION" : '\(.*\)-[0-9]*'`

if [ "$PATCH" ]; then
	if [ "$1" ]; then
		echo "You can only specify a patch or a sourcecodearchive not both!"
		exit 1
	fi
	# do the patching
	case "$PATCH" in
		*.gz)	zcat $PATCH >/tmp/$$
			;;
		*)	cp $PATCH /tmp/$$
			;;
	esac
	if [ "$NEW_VERSION" = "" ]; then
		# Figure out the new version
		X=`expr "$PATCH" : ".*/\([^/]*\)"`
		if [ ! $? ]; then
			echo "New Version not recognized from given filename"
			echo "Please run uupdate with the -v option"
			exit 1
		fi
		NEW_VERSION=`expr "$X" : "$MPATTERN"`
		echo "New Release will be $NEW_VERSION-1"
	fi
	# Clean package
	build clean
	cd ..
	tar zxf $PACKAGE\_$UVERSION.orig.tar.gz
	cd $PACKAGE-$UVERSION.orig
	if patch -p1 </tmp/$$; then
		rm `find . -name "*.orig"`
		cd ..
		mv $PACKAGE-$UVERSION.orig $PACKAGE-$NEW_VERSION.orig
		echo "-- Originals successfully patched"
		cp -a $PACKAGE-$UVERSION $PACKAGE-$NEW_VERSION
		cd $PACKAGE-$NEW_VERSION
		if patch -p1 </tmp/$$; then
			echo "Success. The supplied diffs worked fine."
		else
			echo "The diffs from did not apply cleanly!"
			X="`find . -name "*.rej"`"
			echo "Rejected diffs are in $X"
		fi
		chmod a+x debian/rules
		rm -f `find . -name "*.orig"` /tmp/$$
		dch -v "$NEW_VERSION" "New upstream release"
		echo "Remember: Your current directory is the OLD sourcearchive!"
		echo "Do a \"cd ../$PACKAGE-$NEW_VERSION\" to see the new package"
		exit
	else
		echo "Patch failed to apply to original sources $UVERSION"
		cd ..
		rm -r $PACKAGE-$UVERSION.orig /tmp/$$
		exit 1
	fi
else
	# This is an orginal sourcearchive
	if [ "$ARCHIVE" = "" ]; then
		echo "upstream sourcearchive not specified"
		exit 1
	fi
	echo -n "Old Release $PACKAGE $VERSION "
	# Figure out the new version
	X=`expr "$ARCHIVE" : ".*/\([^/]*\)"`
	# Figure out the type of archive
	case "$X" in
		*.tar.gz)	UNPACK="tar zxf"
				X=`expr "$X" : "\(.*\).tar.gz"`
			;;
		*.tgz)		UNPACK="tar zxf"
				X=`expr "$X" : "\(.*\).tgz"`
			;;
		*.tar)		UNPACK="tar xf"
				X=`expr "$X" : "\(.*\).tar"`
			;;
		*.zip)		UNPACK="unzip"
				X=`expr "$X" : "\(.*\).zip"`
			;;
		*)
			echo "Sorry: Unknown Archive type"
			exit 1
	esac
	if [ "$NEW_VERSION" = "" ]; then
		NEW_VERSION="`expr "$X" : "$MPATTERN"`"
		if [ ! $? ]; then
			echo "New Version not recognized from given filename"
			echo "Please run uupdate with the -v option"
			exit 1
		fi
	fi
	echo "New Release will be $NEW_VERSION-1"
	cd ..
	mkdir tmp
	cd tmp
	echo "-- Untarring the new sourcecode archive $ARCHIVE"
	$UNPACK $ARCHIVE
	cd ..
	mv tmp/* $PACKAGE-$NEW_VERSION
	rm -rf tmp
	if [ -e $PACKAGE-$NEW_VERSION.orig ]; then
		echo "Original sourcetree already exists in $PACKAGE-$NEW_VERSION.orig"
		exit 1;
	fi
	cp -a $PACKAGE-$NEW_VERSION $PACKAGE-$NEW_VERSION.orig
	cd $PACKAGE-$NEW_VERSION
	if zcat ../$PACKAGE\_$VERSION.diff.gz | patch -Np1 ; then
		echo "Success. The diffs from version $VERSION worked fine."
	else
		echo "The diffs from version $VERSION did not apply cleanly!"
		X="`find . -name "*.rej"`"
		echo "Rejected diffs are in $X"
	fi
	chmod a+x debian/rules
	rm -f `find . -name "*.orig"`
	dch -v "$NEW_VERSION" "New upstream release"
	echo "Remember: Your current directory is the OLD sourcearchive!"
	echo "Do a \"cd ../$PACKAGE-$NEW_VERSION\" to see the new package"
fi
