#!/bin/sh
# Wrapper around pdebuild that uses the backports chroot.

pbuildopts=false
buildresult=..
debbuildopts=-i

while [ -n "$1" ] ; do
    option="$1"
    shift
    case $option in
    -B)
        debbuildopts="${debbuildopts} -B"
        ;;
    -s[aid])
        debbuildopts="${debbuildopts} $option"
        ;;
    --)
        pbuildopts=true
        break
        ;;
    --buildresult)
        buildresult="$1"
        shift
        ;;
    --debbuildopts)
        debbuildopts="${debbuildopts} $1"
        shift
        ;;
    *)
        if [ -d /var/cache/pbuilder/base-$option.cow ]; then
          dist=$option
        else
          echo "Option $option not supported yet, ask Russ if you need it" >&2
        fi
        ;;
    esac
done

if [ -z "$dist" ]; then
    echo "Distribution for which to build required." >&2
    exit 1
fi
if [ "$pbuildopts" = true ] ; then
    pdebuild --pbuilder cowbuilder --buildresult "$buildresult" \
        --debbuildopts "$debbuildopts" -- \
        --basepath "/var/cache/pbuilder/base-$dist.cow" \
        --distribution "$dist" --configfile "/etc/pbuilder/$dist" "$@"
else
    pdebuild --pbuilder cowbuilder --buildresult "$buildresult" \
        --debbuildopts "$debbuildopts" -- \
        --basepath "/var/cache/pbuilder/base-$dist.cow" \
        --distribution "$dist" --configfile "/etc/pbuilder/$dist"
fi

exit 0

DOCS=<<__END_OF_DOCS__

=for stopwords
Allbery backported cowbuilder pbuild pbuilder
pdebuild -sa -sd -si --use-network

=head1 NAME

pbuild - Wrapper around pdebuild that uses the appropriate chroot

=head1 SYNOPSIS

B<pbuild> [B<-B>] [B<-sa> | B<-si> | B<-sd>] [--debbuildopts <debbuild options>] [I<pdebuild options>] I<dist>

=head1 DESCRIPTION

B<pbuild> is a simple wrapper around B<pdebuild> that tells it to use
B<cowbuilder> and sets the appropriate chroot location and pbuilder
configuration.

=head1 OPTIONS

=over 4

=item B<-B>

Do a binary-only build.  Does not build a source package or any
architecture-independent packages.  This is normally used when building on
a second or subsequent architecture after already uploading a regular
build for the first architecture.

=item B<-sa>

=item B<-si>

=item B<-sd>

Control inclusion of the *.orig.tar.gz file in the *.changes file.  The
B<-si> behavior is the default: include the *.orig.tar.gz file in the
upload if and only if the Debian revision of the package version is
C<-1>.  For backported packages, you will normally want to use B<-sa>,
which forces inclusion of the *.orig.tar.gz file in the upload.  B<-sd>
forces the omission of the *.orig.tar.gz file.

The *.orig.tar.gz file must be included in the *.changes file and hence in
the upload if it isn't already present in the repository to which the
package is being uploaded.

=item B<--debbuildopts debbuild options>

Any extra dpkg-buildpackage options should be specified here. For example

    pbuild wheezy --debbuildopts '-Izzz -izzz'

=item B<--> I<pbuilder options>

Any arguments following a C<--> are passed through to the B<cowbuilder>
backend rather than to B<pdebuild>.  Use this to set B<cowbuilder> or
B<pbuilder> options.  For example, to allow network access during the
build:

    pbuild bookworm -- --use-network yes

Note that the C<--> itself is consumed by B<pbuild>; you do not need to
add a second C<--> for the options that follow it.

=back

=head1 SEE ALSO

cowbuilder(8), pdebuild(1)

=head1 AUTHORS

Russ Allbery <rra@stanford.edu>

=cut
