# So you want to build a new Debian package for TorBirdy?
# These are the exact steps taken to update from TorBirdy 0.1.2 to 0.1.3
# 
sudo apt-get install git-buildpackage pristine-tar mozilla-devscripts zip
export VERSION=0.1.3
git clone ssh://anonscm.debian.org/git/pkg-mozext/torbirdy.git torbirdy
cd torbirdy
git checkout upstream
git checkout pristine-tar
git checkout master
cd ..
git clone git@github.com:ioerror/torbirdy.git torbirdy.upstream
cd torbirdy.upstream
git checkout $VERSION
git archive --format=tar --prefix="torbirdy_$VERSION.orig/" \ 
  -o ../torbirdy_$VERSION.orig.tar -v $VERSION
cd ../torbirdy
git checkout master
git import-orig --filter=*.git --pristine-tar --upstream-version=$VERSION \
  ../torbirdy_$VERSION.orig.tar
dch -v $VERSION-1 "New upstream release"
git add debian/changelog
git commit -m "stage release $VERSION"
# Make any other changes here ...
# stage them and commit them ...
# Now build it and ignore anything not in git:
git-buildpackage --git-ignore-new
# Test the resulting .deb
# Please tag this release, build and upload the Debian package
# Hooray!
# You're done!

#########################################################################
# The following notes are different ways to accomplish the same job
# They are potential alternative methods for doing the task above
# in the future.
#########################################################################

# First install some packages to do the job:
sudo apt-get install git-buildpackage pristine-tar mozilla-devscripts zip
# Or if using schroot
mysid=sid$RANDOM;
schroot -b -c sid -n $mysid; dd-schroot-cmd -c $mysid apt-get update;
dd-schroot-cmd -c $mysid apt-get build-dep torbirdy;
dd-schroot-cmd -c $mysid apt-get install git git-buildpackage \
  pristine-tar mozilla-devscripts zip ssh ca-certificates gitpkg
schroot -r -c $mysid

# Lunar suggests the following as the ideal workflow
# You may need to do this: gpg --recv-key 0x744301A2
# Set the version we're going to import
VERSION=0.1.3
gbp clone https://anonscm.debian.org/git/pkg-mozext/torbirdy.git
# Or if you're doing this with ssh keys:
# gbp clone ssh://anonscm.debian.org/git/pkg-mozext/torbirdy.git
cd torbirdy
git remote add torbirdy-upstream https://github.com/ioerror/torbirdy.git
# Or if you're doing this with ssh keys:
# git remote add torbirdy-upstream ssh://git@github.com:ioerror/torbirdy.git
git fetch torbirdy-upstream
git tag -v $VERSION
git checkout upstream
git merge --ff-only $VERSION
git tag upstream/$VERSION
git checkout master
git merge upstream/$VERSION
# Make any packaging changes here (eg: dch, etc)
git-buildpackage --git-ignore-new

##########################################
# Alternative ways to build this package #
##########################################

# Grab the current package's source:
git clone ssh://anonscm.debian.org/git/pkg-mozext/torbirdy.git torbirdy
# OR perhaps https://anonscm.debian.org/git/pkg-mozext/torbirdy.git
# checkout the various branches:
cd torbirdy
git checkout upstream
git checkout pristine-tar
git checkout master
# Grab upstream's latest git repo:
cd ..
git clone ssh://git@github.com:ioerror/torbirdy.git torbirdy.upstream
# OR perhaps https://github.com/ioerror/torbirdy.git
# Now create an orig.tar.bz of the upstream release
cd torbirdy.upstream
git checkout $VERSION # This is to ensure that we create a .tar of tag $VERSION
# Make a pristine tarball from the upstream repo:
git archive --format=tar --prefix="torbirdy_$VERSION.orig/" \ 
  -o ../torbirdy_$VERSION.orig.tar -v $VERSION
# Now import it
cd ../torbirdy
git checkout master
git import-orig --filter=*.git --pristine-tar --upstream-version=$VERSION \
  ../torbirdy_$VERSION.orig.tar
# update the debian/changelog
dch -v $VERSION-1 "New upstream release"
# Commit the changes
git add debian/changes
git commit -m "stage release $VERSION"
# Now build it and ignore anything not in git:
git-buildpackage --git-ignore-new

######################################
# Git expert version follows:        #
######################################
git clone https://anonscm.debian.org/git/pkg-mozext/torbirdy.git
cd torbirdy
git checkout upstream
git checkout pristine-tar
git checkout master
git remote add torbirdy.upstream https://github.com/ioerror/torbirdy.git
git fetch torbirdy.upstream
git checkout $VERSION
git checkout -b $VERSION-merge-madness
git merge -m 'directed acyclic graph trick' upstream -s ours
git branch -m upstream old-upstream
git branch -m $VERSION-merge-madness upstream
git branch -d old-upstream
git checkout upstream
git checkout master
# update the debian/changelog
dch -v $VERSION-1 "New upstream release"
# Commit the changes
git add debian/changes
git commit -m "stage release $VERSION"
# Merge upstream into master
git merge upstream
# create the pristine-tar in ../deb-packages/torbirdy/torbirdy_$VERSION.orig.tar.gz
# this also creates a source package in ../deb-packages/torbirdy/
gitpkg master upstream
# build the package
cd ../deb-packages/torbirdy/
dpkg-source -x torbirdy_$VERSION-1.dsc
cd torbirdy-$VERSION
DEBUILD_DPKG_BUILDPACKAGE_OPTS="-us -uc -I -i"
DEBUILD_LINTIAN_OPTS="-i -I --show-overrides"
debuild

#### Now if you want - you can pretend you did the same steps as above ....
git import-orig --filter=*.git --pristine-tar --upstream-version=$VERSION \
  ../deb-packages/torbirdy/torbirdy_$VERSION.orig.tar.gz
# build our deb and ignore anything not in git:
git-buildpackage --git-ignore-new
