#!/bin/bash -ev
set -x
set -e
debdir=`pwd`
upstream_tag=v3.10.11
rpi_branch=rpi-3.10.y
patch_dir=${debdir}/debian/patches
rpi_patches=${patch_dir}/rpi

#make a backup of the quilt series so the user can restore it if this script fails
cp debian/patches/series debian/patches/series.bak

export QUILT_PATCHES=debian/patches
quilt pop -a || [ $? == 2 ]

 # add rempote repositories, and update (needs a around 500M)
rm -rf linuxgit
git clone -o linux-stable --reference ../linuxgit 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}

# record commits used to base patches on
git show ${upstream_tag} > ../debian/upstream-tag-used-to-generate-rpi-patches
git show rpi/${rpi_branch} > ../debian/rpi-branch

# 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}')

# Remove commit a5f196ca90a717c0c40c33c4ca815c6c606d19c2, it seems to already be applied in the debian "upstream" source.

git_commits=`echo ${git_commits} | sed s/a5f196ca90a717c0c40c33c4ca815c6c606d19c2//`

# generate one patch per commit, including comments, with an ordered sequence
# to preserve patch ordering.
i=1000
rm -rf ${rpi_patches}
mkdir -p ${rpi_patches}
for c in $git_commits ; do
    git show ${c} > ${rpi_patches}/rpi_${i}_${c}.patch
    # include dummy file in patch to ensure it has some content even after later processing (empty patches upset quilt)
    cat ../debian/patches/dummytemplate.diff | sed s/PATCHNAME/rpi_${i}_${c}/ >> ${rpi_patches}/rpi_${i}_${c}.patch
    i=$((${i}+1))
done

#pipe the output of git diff through filterdiff as it seems to confuse interdiff otherwise
git diff ${upstream_tag} | filterdiff > ../merged.diff
export QUILT_PATCHES=../debian/patches

# split quilt series and remove pi patches
cd ..
chmod 755 debian/splitseries.php
debian/splitseries.php
cd linuxgit

# create new quilt series, use just the new series for now for testing
ls ${rpi_patches} | sed s_^_rpi/_ > ${patch_dir}/series.fromgit
cp ${patch_dir}/series.fromgit ${patch_dir}/series

git reset --hard ${upstream_tag}
cd ..

rm -rf linuxtest linuxclean

rsync -a --exclude .git linuxgit/ linuxtest/
cp -al linuxgit linuxclean
rm -rf linuxclean/.git

cd linuxtest
export QUILT_PATCHES=../debian/patches
while quilt push -f || [ $? == 1 ]; do quilt refresh; done
cd ..

diff -urN linuxclean linuxtest | filterdiff -p1 -x '.pc/*' -x 'dummy/*' --clean > patched.diff || [ $? == 1 ]
echo 'this patch contains changes that the system could not split out into individual patches' > debian/patches/rpi/rpi_9999_other_changes.patch
interdiff -p1 patched.diff merged.diff | filterdiff -x '*.rej' --clean >> debian/patches/rpi/rpi_9999_other_changes.patch
cat debian/dummy.patch >> debian/patches/rpi/rpi_9999_other_changes.patch
echo rpi/rpi_9999_other_changes.patch >> ${patch_dir}/series.fromgit

#reassemble full quilt  series
cat ${patch_dir}/series.prefix ${patch_dir}/series.fromgit ${patch_dir}/series.suffix > ${patch_dir}/series



export QUILT_PATCHES=debian/patches

while quilt push; do quilt refresh; done

rm -rf linuxgit linuxclean linuxtest
rm merged.diff patched.diff
rm debian/patches/series.*

echo finished sucessfully

