#!/bin/bash
# /usr/local/bin/backport
# backports a package without signing it
# paul@vandervlis.nl
#
# user needs sudo rights for apt-get, e.g.:
# paul ALL=(ALL) PASSWD: /usr/bin/apt-get
#
# /etc/apt/sources.list needs a line like this:
# deb-src http://ftp.nl.debian.org/debian/ testing main

# Variables:
export DEBFULLNAME="Paul van der Vlis"
export DEBEMAIL="paul@vandervlis.nl"
export RELEASE="wheezy-backports"

# ask for package when not on commandline
if test "$1" = ""; then
  read -p "package: " package
else
  package=$1
fi

# find name of source-package:
package=`apt-cache showsrc $package | grep "Package: " | cut -d" " -f2`
echo "Source package: $package"

# install build-dependencies
sudo /usr/bin/apt-get build-dep $package
if ! test $? = 0; then 
 # try to install the backport-version
 sudo /usr/bin/apt-get -t $RELEASE build-dep $package
 if ! test $? = 0; then exit; fi
fi

# create dir
if test -e $package; then echo "Directory excists"; fi
mkdir $package; cd $package
if ! test $? = 0; then exit; fi

# download sources
apt-get source $package
if ! test $? = 0; then exit; fi

# go to right directory
cd `find ./ -maxdepth 1 -type d | tail -n1`
if ! test $? = 0; then exit; fi

# Indicate in the changelog a backport revision number
# debchange --local ~bpo70+ --distribution $RELEASE "Rebuild for $RELEASE."
debchange --bpo --distribution wheezy-backports " "

# build package with or without signing
if test "$2" = "sign"; then
 dpkg-buildpackage
else
 dpkg-buildpackage -uc -us
fi
if ! test $? = 0; then exit; fi

# show debs
cd ../..
echo
echo "Created deb's:"
ls $package/*.deb
echo


