#!/bin/bash -ev
set -x
upstream_tag=v4.2.8
rpi_branch=rpi-4.2.y
rpi_patches=$(pwd)/rpi

rm -rf linuxgit
git clone -o linux-stable --reference ../linux-stable git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linuxgit
cd linuxgit
git remote add rpi https://github.com/raspberrypi/linux.git
git remote update

# create branch from tag and switch to it
git checkout -b target-version ${upstream_tag}

# merge the changes from the rpi branch
git merge --no-edit rpi/${rpi_branch}

# get a list of commits not present upstream
git_commits=$(git cherry ${upstream_tag} | awk '/^\+/{print $2}')

# generate one patch per commit, including comments, with an ordered sequence
# to preserve patch ordering.
i=100
rm -rf ${rpi_patches}
mkdir -p ${rpi_patches}
for c in $git_commits ; do
    git show ${c} > ${rpi_patches}/rpi_${i}_${c}.patch
    i=$((${i}+1))
done