#!/bin/sh
set -e

#  embootstrap -- create a bootstrap for emdebian builds
#
#  Copyright (C) 2007  Neil Williams <codehelp@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, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

CWD=`pwd`

. /usr/lib/pbuilder/pbuilder-modules

# move to our own function file later.
function check_dirs()
{
	if [ ! -d $BUILDPLACE ] ; then
		mkdir -p $BUILDPLACE
	fi
	if [ ! -d $BUILDRESULT ] ; then
		mkdir -p $BUILDRESULT
	fi
	if [ ! -d $APTCACHE ] ; then
		mkdir -p $APTCACHE
	fi
	if [ ! -d $BUILDPLACE/var/cache/apt/archives/ ] ; then
		mkdir -p $BUILDPLACE/var/cache/apt/archives/
	fi
}

# Need support for create (this script), update (unpack base.tgz) and login
# (sudo chroot /bin/sh) as well as the final empdebuild (emsource,emdebuild)
# in a separate script.

# This script will concentrate only on creating and updating the chroot.
# Parameters. 
# WORK=location of this script and the emenv script. (fixme)
# JAIL=location of the chroot jail
# MIRROR=the Debian primary mirror that will replace the default debootstrap mirror
# (ftp.debian.org is not a primary and only supports 2 architectures).
# TODO: needs command line options to set the rest of these parameters.
WORK=$CWD
BUILDPLACE=`perl -e 'use Cwd; use Emdebian::Tools; use Config::Auto; \
require "dpkg-cross.pl"; \
&read_config; \
my $w = &get_workdir; \
$w = cwd if (! -d $w); \
print $w;';`
BASETGZ="$BUILDPLACE/emdebian.tgz"
BASETGZ=`echo $BASETGZ | tr -s \/`
BUILDPLACE="$BUILDPLACE/pbuilder"
# tidy up // to /
BUILDPLACE=`echo $BUILDPLACE | tr -s \/`
MIRROR="ftp://ftp.uk.debian.org/debian/"
ARCH=`perl -e 'require "dpkg-cross.pl"; \
&read_config(); \
my $arch = &get_architecture(); \
die (qq/Could not determine the default architecture.\n/) if (!$arch); \
print $arch;';`

#pbuilder base values
DEBIAN_BUILDARCH=$ARCH
# cross-building chroot is same arch as host.
BUILDRESULT="$BUILDPLACE/result/"
# tidy up // to /
BUILDRESULT=`echo $BUILDRESULT | tr -s \/`
DISTRIBUTION=unstable
APTCACHE="$BUILDPLACE/aptcache/"
# tidy up // to /
APTCACHE=`echo $APTCACHE | tr -s \/`
AUTO_DEBSIGN=yes
APTCACHEHARDLINK="yes"

if [ $ARCH == "None" ] || [ ! $ARCH ]; then
	echo "embootstrap currently needs a default architecture in debconf"
	exit
fi
echo "Current default architecture: $ARCH"
echo "Checking for a user writeable tree in $BUILDPLACE"
check_dirs
echo "Preparing to create or update bootstrap - enter sudo password if requested"
export LANG=C
export LC_ALL=C
# for login support
#DEBOOTSTRAP_DIR=$WORK sudo debootstrap --keep-debootstrap-dir --verbose unstable $BUILDPLACE $MIRROR emdebian.buildd
DEBOOTSTRAP_DIR=$WORK sudo debootstrap --verbose unstable $BUILDPLACE $MIRROR emdebian.buildd

# need BUILDPLACE for pbuilder code
recover_aptcache

# emdebian.buildd installs emdebian-tools which sets up apt for the emdebian repository
sudo chroot "$BUILDPLACE" /usr/bin/apt-get update
# need a --force within emsetup.
sudo chroot "$BUILDPLACE" /usr/bin/emsetup -a $ARCH
sudo chroot "$BUILDPLACE" /usr/bin/apt-get -y --force-yes dist-upgrade

save_aptcache
sudo chroot "$BUILDPLACE" /usr/bin/apt-get clean || true
create_basetgz

#sudo chroot echo "Logging in to the emdebian jail"
#sudo chroot "$BUILDPLACE" /bin/sh

