#!/bin/sh

# Script to maintain a (target) tree based on the intrepid main kernel
# Author: Amit Kucheria (amit@ubuntu.com)
# Date: 11/07/08

# Assumptions:
#  1. Every rebase during devel-cycle is an ABI bump
#  2. The target tree's patches are always on top
#  3. As a consequence of 2., tags are not really useful
#  4. autogenerated patches are marked with AUTO; others are marked with SAUCE
#  5. Keep things in a state that they can be merged back into the main tree at the end of the dev cycle

targetDir=${1:?"Usage: $0 <target kernel dir>"}

# Configuration variables
patchDir=$targetDir/patches
masterTree=git://zinc.ubuntu.com/ubuntu/ubuntu-intrepid-lrm.git
masterName=ubuntu-intrepid-lrm
baseTag="Ubuntu-2.6.27-0.0"
pkgName="linux-restricted-modules-lpia"

remoteBranch=auto-tmp-remote
git=`which git`

mkdir -p $patchDir
cd $targetDir
rm -rf .dotest/

echo "* Check for uncommitted files..."
$git-status | grep -q "nothing to commit"
if [ ! $? -eq "0" ]; then
	echo "\tUncommitted changes found!\n\tClean out your tree before running this script. Exiting."
	exit 1;
fi
$git-checkout master
$git-reset --hard

echo "\n"
echo "* Fetching Ubuntu master tree..."
$git-branch -r | grep $masterName
if [ $? -eq "0" ]; then
	echo "\t$masterName is already used as a branch name in your tree. Please change the \$masterName variable in this script."
	exit 1;
fi
$git-remote add -f -t master $masterName $masterTree
$git-checkout -b $remoteBranch $masterName/master
$git-checkout master

echo "\n"
echo "* Exporting current work as patches..."
$git-format-patch -o $patchDir $baseTag
# Deleting tags is evil...
$git-tag -l | grep "Ubuntu-2.6.2*" | xargs $git-tag -d

# Debugging
mkdir -p .patches
cp $patchDir/*.patch .patches
# end Debugging

# Remove auto-generated patches
find $patchDir -type f -name "*UBUNTU-AUTO*" -print0 | xargs -0 rm -f

echo "\n"
echo "* Starting rebase"
$git-checkout $remoteBranch
$git-tag -m "Tip of ubuntu git on which we apply patches" $baseTag

# Move debian/ out of the way
$git-mv debian/ debian-main/
$git commit -s -m "UBUNTU: AUTO: Move upstream debian/ dir out of the way for a lpia-specific debian/"
$git-status

# Copy debian-main/ to debian/
cp -a debian-main/ debian/
$git-add debian/
$git-commit -a -s -m "UBUNTU: AUTO: Create a new debian/"
$git-status

# Delete upstream ABI, extra arch/flavours
#$git-rm -r debian/abi/*
$git-rm -r debian/config/*
$git-rm debian/control.d/vars.generic debian/control.d/vars.server
$git-rm debian/rules.d/amd64.mk debian/rules.d/i386.mk
$git-commit -a -s -m "UBUNTU: AUTO: Delete unncessary ABI/arch/flavours"
$git-status

# Start new changelog
$git-mv debian/changelog debian/changelog.intrepid
dch --create --package $pkgName --newversion 2.6.27-1.0
$git-add debian/changelog
$git-commit -a -s -m "UBUNTU: AUTO: Start new changelog"
$git-status

# Apply pending patches
$git-am patches/*
if [ -d .dotest/ ]; then
    echo "\tFailed to apply patches"
    echo "\tPlease fix the patches on branch $remoteBranch and then sync to the master branch"
    exit 1;
fi

echo "\n"
echo "If the final result looks good, then do the following to make it permanent:"
echo " * Run debian/scripts/misc/retag to retag"
echo " * git-checkout master"
echo " * git reset --hard $remoteBranch"
echo " * git remote rm $masterName"
echo " * rm -rf patches/"

