#!/bin/bash

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 <linux-source-directory>" 1>&2
	exit 1
fi
master_dir="$1"

branch=$(cd "$master_dir" && git branch | grep "*" | sed "s/* //")
echo "Repository: $master_dir, Branch: $branch"

if [ "$branch" != "lts-backport-raring" ]; then
  echo "Not the correct repository and/or branch"
  exit
fi

here_version=`LC_ALL=C dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2`
here_series=`LC_ALL=C dpkg-parsechangelog | grep ^Distribution: | cut -d ' ' -f 2`
if [ "$here_series" = "UNRELEASED" ]; then
	here_series=`LC_ALL=C dpkg-parsechangelog --until "$here_version" | grep ^Distribution: | cut -d ' ' -f 2`
fi

if [ -f "$master_dir/debian/debian.env" ]; then
	branch=`sed -ne 's/DEBIAN=//p' <"$master_dir/debian/debian.env"`
	changelog="-l$branch/changelog"
else
	changelog=""
fi
master_version=`(cd "$master_dir" && LC_ALL=C dpkg-parsechangelog $changelog | grep ^Version: | cut -d ' ' -f 2)`
tag_version=`echo $master_version | cut -d '~' -f 1`

#echo "here_version: <$here_version>"
#echo "master_version: <$master_version>"
#echo "tag_version: <$tag_version>"

if dpkg --compare-versions "$here_version" lt "$master_version"; then
	dch --newversion "$master_version" "Master Version $tag_version"
	dch --distribution "$here_series" --release ""
	echo "Updated to version: $master_version"

	echo "git commit -s -m 'UBUNTU: Ubuntu-lts-$tag_version'"
	echo "git tag -s -m 'Ubuntu-lts-$tag_version' 'Ubuntu-lts-$tag_version'"
fi
