#!/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/>.

. /usr/share/pbuilder-scripts/pfuncs
setup_all $*

extra_args=""
bin_only="false"

for arg in $*; do
	if [ $arg = "--source" ]; then
		extra_args="$extra_args -S"
	elif [ $arg = "--binary-only" ]; then
		bin_only="true"
	elif [ $arg = "--debug" ]; then
		extra_args="$extra_args --set-envvar=DEB_BUILD_OPTIONS=debug,nostrip,noopt"
	elif [ $arg = "--help" ]; then
		echo "Usage: pbuild [--source, --debug, --help] [PROJECT]"
		echo "       You must be in the source directory"
		exit 0
	fi
done

if [ $bin_only = "true" ]; then
	debuild_args="-b $extra_args"
	pdebuild_args="--use-pdebuild-internal" # oddly, normal pdebuild explicitly forces source
else
	debuild_args="-i -I -sa $extra_args"
	pdebuild_args=""
fi

# Let builddir just be '..', since it's hard to say where we should put it.
# If the user's PBUILDER_SRCDIR_FORMAT is pkg, we'd have to guess package name
# from current directory, and I don't want to go there.  This is easier.
builddir=..

pdebuild --buildresult "$builddir" $pdebuild_args \
         --debbuildopts "\"$debuild_args\"" -- \
			   --basetgz "/var/cache/pbuilder/$proj.tgz" \
         --hookdir "/usr/share/pbuilder-scripts/hooks"

