#!/bin/bash -ev
scriptdir=`dirname $(readlink -f "$BASH_SOURCE")`

git reset --hard

#git checkout buildnewlinuxpackage-deb
#DEBVERSION=`dpkg-parsechangelog -S version`
#PACKAGE=linux-`echo $NEWVERSION | cut -d '.' -f 1`.`echo $NEWVERSION | cut -d '.' -f 2`
#git checkout buildnewlinuxpackage-oldpkgonly
#OLDVERSION=`dpkg-parsechangelog -S version`
#
#git checkout buildnewlinuxpackage-pkgmerge
#NEWVERSION=`dpkg-parsechangelog -S version`
#TARGETSUITE=`dpkg-parsechangelog -S distribution`


git config --local user.name "Raspbian kernel package updater"
git config --local user.email root@raspbian.org
git config merge.conflictstyle diff3

git checkout buildnewlinuxpackage-stage2
touch debian/changelog

debian/rules clean || debian/rules clean
git add -f debian/*
git commit -a -m 'run clean target to update generated files in debian packaging and possiblly file permissions'

PACKAGE=`dpkg-parsechangelog -S source`
UPSTREAM=`dpkg-parsechangelog -S version | cut -d '-' -f 1`
rm -rf usextract
mkdir usextract
cd usextract
tar -xf ../../${PACKAGE}_${UPSTREAM}.orig.tar.*
mv linux-${UPSTREAM}/{,.[^.]}* .
rmdir linux-$UPSTREAM
export QUILT_PATCHES=../debian/patches                                             
quilt push -a --fuzz 0 || true                                                  
while quilt push; do                                                            
	quilt refresh                                                           
	quilt push -a --fuzz 0 || true                                          
done
unset QUILT_PATCHES
cd ..
rm -rf usextract

git commit -a -m 'defuzz patches'

dgit --quilt=auto quilt-fixup
