#!/bin/bash

#
# This script is intended as a helper when updating from
# Lucid mvl-dove. When complete you should have a copy of
# the mvl-dove changelog with the correct release names.
#
SOURCE_RELEASE=lucid
SOURCE_RELEASE_BRANCH=mvl-dove
DEBIAN_SOURCE=debian.${SOURCE_RELEASE_BRANCH}

TARGET_RELEASE=maverick
TARGET_RELEASE_BRANCH=lucid-mvl-dove
DEBIAN_TARGET=debian.${TARGET_RELEASE_BRANCH}

DEF_REPO=git://kernel.ubuntu.com/ubuntu/ubuntu-${SOURCE_RELEASE}.git
RELEASE_REPO="${DEF_REPO}"
DEF_ARCHES="armel"
FOREIGN_ARCHES="i386 amd64 ia64 powerpc sparc config.common.ports"

#
# PPAs do not have a proposed pocket. The default assumes the
# real archive is the upload target.
#
POCKET="-proposed"
IGNORE_ABI=""
IGNORE_MODULES=""

usage="$0 [-r RELEASE_REPO] [-p]"

#
# command line options:
# [-r RELEASE_REPO] - override default ${SOURCE_RELEASE} git repository.
# [-p] - Assume the upload target is a PPA

while getopts ":r:pim" opt; do
	case $opt in
	r ) RELEASE_REPO="$OPTARG" ;;
	p ) POCKET="" ;;
	\? ) echo usage: ${usage}; exit ;;
	esac
done
shift $(($OPTIND - 1))

if [ ! -d ${DEBIAN_TARGET} ]
then
	echo You must run this sript from the top directory of this repository.
	exit 1
fi

#
# Fetch the upstream branch.
#
git fetch ${RELEASE_REPO} ${SOURCE_RELEASE_BRANCH} || exit 1

#
# Find the most recent tag on ${SOURCE_RELEASE} ${SOURCE_RELEASE_BRANCH}, then
# rebase against it. This avoids the case where there have been some
# commits since the last official tag.
#
MASTER_COMMIT=`git log --pretty=one FETCH_HEAD | \
    awk '
	/Ubuntu-/ {
		if (match($0, /UBUNTU: Ubuntu-[0-9]/)) {
				print $1
				exit
                        }
                }
        '
`
#
# Find the current merge point where ${SOURCE_RELEASE} was based.
#
BASE_COMMIT=`git log --pretty=one | \
    awk '
	/Ubuntu-/ {
		if (match($0, /UBUNTU: Ubuntu-[0-9]/)) {
				print $1
				exit
                        }
                }
        '
`
if [ "${MASTER_COMMIT}" = "${BASE_COMMIT}" ]
then
	echo Already up to date.
	exit 1
fi

if ! git rebase --onto ${MASTER_COMMIT} ${BASE_COMMIT}
then
	exit 1
fi

#
# Update configs from master
#
rsync -av --delete ${DEBIAN_SOURCE}/config/ ${DEBIAN_TARGET}/config

#
# Update changelog from the source release changelog. Change the release pocket and ABI.
# The lucid source ABI is in the 200 range, and the maverick ABI is in the 400 range, so
# just extract the source ABI and add 200.
#
cp ${DEBIAN_SOURCE}/changelog ${DEBIAN_TARGET}/changelog
sed -i -e '1s/'${SOURCE_RELEASE}'/'${TARGET_RELEASE}'/' ${DEBIAN_TARGET}/changelog
version=`head -n1 ${DEBIAN_SOURCE}/changelog|sed -e 's/.*(//' -e 's/).*$//'`
source_abi=`echo $version|sed -e 's/^.*-//' -e 's/\..*$//'`
let dest_abi=$source_abi+200
sed -i -e '1s/'${source_abi}'/'${dest_abi}'/' ${DEBIAN_TARGET}/changelog
git add ${DEBIAN_TARGET}/changelog

#
# Finally, rerun updateconfigs.
#
fakeroot debian/rules clean updateconfigs
