#!/bin/bash
#
# Run debootstrap and add a few other files needed to create a working
# sbuild chroot.
# Copyright © 2004 Francesco P. Lovergine <frankie@debian.org>.
# Copyright © 2007 Roger Leigh <rleigh@debian.org>.
#
# 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, either version 2 of the License, or
# (at your option) any later version.
#
# 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/>.
#
#######################################################################

set -e

if [ $(id -u) != 0 ]; then
    echo "You need to run this command as root"
    exit 2
fi

if [ $# -ne 3 ]; then
    echo "$0 <suite> <target-dir> <debian-mirror-url>"
    echo "	E.g. $0 sarge /path/to/chroot http://http.us.debian.org/debian"
    exit 1
fi

SUITE=$1
TARGET=$2
MIRROR=$3

/usr/sbin/debootstrap \
    --variant=buildd \
    --include=fakeroot,build-essential \
    "$SUITE" "$TARGET" "$MIRROR"

cat <<EOF >"$TARGET/etc/apt/sources.list"
# $SUITE
deb $MIRROR/ $SUITE main contrib non-free
deb-src $MIRROR/ $SUITE main contrib non-free

EOF

echo "I: Configured APT sources.list:"
echo "────────────────────────────────────────────────────────────────────────"
cat "$TARGET/etc/apt/sources.list"
echo "────────────────────────────────────────────────────────────────────────"

cat <<EOF
I: Please add any additional APT sources to $TARGET/etc/apt/sources.list
I: Successfully set up $SUITE chroot.
I: Please add the following entry to /etc/schroot/schroot.conf:
────────────────────────────────────────────────────────────────────────
[$SUITE-sbuild]
type=directory
description=Debian $SUITE autobuilder
location=$TARGET
priority=3
groups=root,sbuild
root-groups=root,sbuild
run-setup-scripts=true
run-exec-scripts=true
────────────────────────────────────────────────────────────────────────
I: Run the add_sbuild_user script to add new sbuild users.
EOF

exit 0
