#!/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=quantal
SOURCE_RELEASE_BRANCH=master
DEBIAN_SOURCE=debian.${SOURCE_RELEASE_BRANCH}

TARGET_RELEASE=quantal
TARGET_RELEASE_BRANCH=lowlatency
DEBIAN_TARGET=debian.${TARGET_RELEASE_BRANCH}

DEF_REPO=git://kernel.ubuntu.com/ubuntu/ubuntu-${SOURCE_RELEASE}.git
RELEASE_REPO="${DEF_REPO}"

#
# 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
                        }
                }
        '
`
MASTER_TAG=`
	git log -1 --pretty=format:%s ${MASTER_COMMIT} | \
		sed -e 's/^UBUNTU: //'
`
#
# 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
                        }
                }
        '
`
BASE_TAG=`
	git log -1 --pretty=format:%s ${BASE_COMMIT} | \
		sed -e 's/^UBUNTU: //'
`
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

# Start a new release.  Note we do not use or need ABI files as we are
# locked step ABI version wise with master.
debian/rules startnewrelease
git commit -s -F debian/commit-templates/newrelease ${DEBIAN_TARGET}

# Insert the rebase information.
./debian/scripts/misc/insert-ubuntu-changes ${DEBIAN_TARGET}/changelog \
	${BASE_TAG} ${MASTER_TAG}
git commit -s -m "UBUNTU: rebase to ${MASTER_TAG}" ${DEBIAN_TARGET}/changelog

#
# Update changelog from the source release changelog.  Copy over the ABI
# number as we share the header files.
#
VERSION=`sed -n '1s/^linux.*(\(.*\-[0-9]*\).*).*$/\1/p' ${DEBIAN_SOURCE}/changelog`
sed -in '1s/^\(linux.*(\)\(.*\-[0-9]*\)\(.*).*\)$/\13.5.0-8\3/' ${DEBIAN_TARGET}/changelog
git commit -s -F debian/commit-templates/bumpabi ${DEBIAN_TARGET}/changelog

#
# ALL DONE: close the release up.
#
VERSION=`sed -n '1s/^linux.*(\(.*\)).*$/\1/p' ${DEBIAN_TARGET}/changelog`
TAG="Lowlatency-${VERSION}"
debian/rules insertchanges
sed -in "1s/UNRELEASED/${TARGET_RELEASE}${POCKET}/" ${DEBIAN_TARGET}/changelog
git commit -s -m "UBUNTU: ${TAG}" ${DEBIAN_TARGET}/changelog

echo ""
echo "*** verify and tag the release."
echo "    git tag -s -m ${TAG} ${TAG}"
