#!/bin/bash

#
# This script is intended as a helper when updating from
# Xenial. When complete you should have a copy of
# the Raring master branch changelog with the correct release names.
#
SOURCE_RELEASE=xenial
SOURCE_RELEASE_BRANCH=${_SOURCE_RELEASE_BRANCH:=master-next}
DEBIAN_SOURCE=debian.master

TARGET_RELEASE=trusty
TARGET_RELEASE_BRANCH=xenial
TARGET_RELEASE_NUMBER=14.04
DEBIAN_TARGET=debian.${TARGET_RELEASE_BRANCH}

DEF_REPO=git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/${SOURCE_RELEASE}
RELEASE_REPO="${DEF_REPO}"
DEF_ARCHES="i386 amd64 armhf arm64 ppc64el powerpc"
FOREIGN_ARCHES="s390x"

#
# PPAs do not have a proposed pocket. The default assumes the
# real archive is the upload target.
#
POCKET=""
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} || exit 1
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.
	if [ -z "$SKIP_REBASE_IF_UPTODATE" ]; then
		exit 1
	fi
elif ! git rebase --onto ${MASTER_COMMIT} ${BASE_COMMIT}
then
	exit 1
fi

#
# Pick up any master branch changes to udeb modules or firmware.
#
rsync -av --delete ${DEBIAN_SOURCE}/d-i/ ${DEBIAN_TARGET}/d-i

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

#
# Turn off strong stack protection as it requires gcc 4.9+
#
sed -i -e 's/^.*CONFIG_CC_STACKPROTECTOR_REGULAR.*$/CONFIG_CC_STACKPROTECTOR_REGULAR=y/' -e 's/^CONFIG_CC_STACKPROTECTOR_STRONG.*$/# CONFIG_CC_STACKPROTECTOR_STRONG is not set/' ${DEBIAN_TARGET}/config/config.common.ubuntu
sed -i 's/CONFIG_CC_STACKPROTECTOR_STRONG/CONFIG_CC_STACKPROTECTOR_REGULAR/' ${DEBIAN_TARGET}/config/annotations

#
# Update package and DTB settings from master.
#
rsync -av ${DEBIAN_SOURCE}/rules.d/*.mk ${DEBIAN_TARGET}/rules.d/
# Remove the .mk files from the arch's that are not supported
for i in ${FOREIGN_ARCHES}
do
	rm -f ${DEBIAN_TARGET}/rules.d/${i}.mk
	git rm -f ${DEBIAN_TARGET}/rules.d/${i}.mk || true
done
# Disable ZFS as we have no tooling in trusty to mount these.
sed -i -e 's/^\(do_zfs[ \t]*=[ \t]\)true\(.*\)$/\1false\2/' debian.xenial/rules.d/*.mk

#
# Update modprobe.d from master
#
rsync -av --delete ${DEBIAN_SOURCE}/modprobe.d/ ${DEBIAN_TARGET}/modprobe.d

cp -p ${DEBIAN_SOURCE}/control.d/*.inclusion-list ${DEBIAN_TARGET}/control.d

cp -p ${DEBIAN_SOURCE}/reconstruct ${DEBIAN_TARGET}/reconstruct

fakeroot debian/rules clean updateconfigs

#
# A little hack to avoid touching the ABI files if we are called from
# cranky (which sets SKIP_REBASE_IF_UPTODATE).
#
if [ -z "$SKIP_REBASE_IF_UPTODATE" ]; then
	#
	# Download the ABI files from the previous build
	#
	PREV_VERSION=$(dpkg-parsechangelog -l${DEBIAN_TARGET}/changelog \
			-SVersion)
	echo "prev abi ver ${PREV_VERSION}"
	./debian/scripts/misc/getabis ${PREV_VERSION} || exit 1
	git add ${DEBIAN_TARGET}/abi
fi

# Stage the bits we changed.
rm -f ${DEBIAN_TARGET}/d-i/kernel-versions
git add ${DEBIAN_TARGET}/d-i ${DEBIAN_TARGET}/config \
	${DEBIAN_TARGET}/rules.d/*.mk ${DEBIAN_TARGET}/reconstruct \
	${DEBIAN_TARGET}/modprobe.d

#
# Update changelog from the source release changelog.
# Change the release pocket and ABI.
#
cp ${DEBIAN_SOURCE}/changelog ${DEBIAN_TARGET}/changelog
sed -i -e '1s/'${SOURCE_RELEASE}'.*;/'${TARGET_RELEASE}${POCKET}';/' -e '1s/^linux /linux-lts-'${SOURCE_RELEASE}' /' -e '1s/)/~'${TARGET_RELEASE_NUMBER}'.1)/' ${DEBIAN_TARGET}/changelog
git add ${DEBIAN_TARGET}/changelog

#
# Tell them what to do next ...
#
FINAL_VERSION_NAME="Ubuntu-lts-"`dpkg-parsechangelog -l${DEBIAN_TARGET}/changelog  | awk '/^Version:/ {print $2}'`
FINAL_VERSION_TAG=`echo "${FINAL_VERSION_NAME}" | sed -e 's/~/_/'`
echo "git commit -a -s -m 'UBUNTU: ${FINAL_VERSION_NAME}'"
echo "git tag -s -m '${FINAL_VERSION_NAME}' '${FINAL_VERSION_TAG}'"
