#!/bin/sh
# -*- indent-tabs-mode: t; tab-width: 2 -*-
#
# Copyright 2009 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJ=$1
PKG=$2

if [ -z "$1" ]; then
	echo "You must specify a project and a package"
	exit 1
elif [ -z "$2" ]; then
	echo "You must specify a package"
	exit 1
fi

SRCDIR=${PBUILDER_SRCDIR:-$HOME/Projects}
SRCDIR=$(readlink -m "$SRCDIR")

FORMAT=${PBUILDER_SRCDIR_FORMAT:-flat}

case "$FORMAT" in
flat)
	SRCDIR="$SRCDIR/$PROJ"
	;;
pkg)
	SRCDIR="$SRCDIR/$PROJ/$PKG"
	;;
*)
	echo "Didn't understand PBUILDER_SRCDIR_FORMAT=$FORMAT"
	echo "Using flat format."
	SRCDIR="$SRCDIR/$PROJ"
	;;
esac

mkdir -p "$SRCDIR"

uid=$(id -u)
gid=$(id -g)

sudo pbuilder execute --basetgz "/var/cache/pbuilder/$PROJ.tgz" --bindmounts "$SRCDIR" -- $(which pget-helper) "$SRCDIR" "$PKG" $uid $gid

