#!/bin/bash
# -*- indent-tabs-mode: t; tab-width: 2 -*-
#
# Copyright 2009, 2010 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/>.

DEFAULT_ARCH="i386"
DEFAULT_DIST="$(lsb_release -c | cut -f2)"
DEFAULT_COMPONENTS="main restricted universe multiverse"

ARCH="$DEFAULT_ARCH"
DIST="$DEFAULT_DIST"
COMPONENTS="$DEFAULT_COMPONENTS"
EXTRAPACKAGES=""

usage()
{
	echo
	echo "Usage: pcreate [-c COMPONENTS] [-e \"EXTRA PACKAGES\"] [-a ARCH]"
	echo "               [-d DIST] [-s SOURCES] PROJECT"
	echo
	echo "Default components are '$DEFAULT_COMPONENTS'".
	echo
	echo "Default architecture is '$DEFAULT_ARCH'".
	echo
	echo "Default distribution is '$DEFAULT_DIST'".
	echo
	echo "EXTRA PACKAGES are a double quoted, space separated list of" 
	echo "packages which will be installed after chroot creation."
	echo
	echo "SOURCES is the starting contents of the sources list."
	echo

	exit 1
}

while getopts 'a:c:e:d:s:' OPTION
do
	case $OPTION in
	a)
		ARCH="$OPTARG"
		;;
	c)
		COMPONENTS="$OPTARG"
		;;
	e)
		EXTRAPACKAGES="$OPTARG"
		;;
	d)
		DIST="$OPTARG"
		;;
	s)
		SOURCES="$OPTARG"
		;;
	?)
		usage
		;;
	esac
done
shift $(($OPTIND - 1))

PROJ=$1
if [ -z "$PROJ" -o -z "$ARCH" -o -z "$DIST" ]; then
		usage
fi

TMPFILE=$(mktemp)

# -s file.list means you want to start with file.list as the initial contents
if [ -z "$SOURCES" ] ; then
 echo \
 '# Put your sources.list entries here

 ' > "$TMPFILE"
else
 cat $SOURCES > "$TMPFILE"
fi


# Let user add custom, project-specific lines
sensible-editor "$TMPFILE"

if [ "$DIST" = "sid" ]; then # should be larger list of Debian names
	MIRROR="ftp://ftp.debian.org/debian"
elif [ "$ARCH" = "i386" -o "$ARCH" = "amd64" ]; then
	MIRROR="http://archive.ubuntu.com/ubuntu"
else 
	MIRROR="http://ports.ubuntu.com/ubuntu-ports"
fi

# if arm, need qemu
if [ "$ARCH" = "armel" ]; then
	# get the host OS release and adapt
  r=$(lsb_release -rs)
  r=${r%%\.*}
   if (( r > 10 )) ; then
     echo "Installing qemu-user-static for armel-on-i386 support (natty or later)"
     sudo apt-get install qemu-user-static || exit 1
	   DEBOOTSTRAP_ARGS="--debootstrap qemu-debootstrap --debootstrapopts --variant=buildd --debootstrapopts --arch=$ARCH"
   else
		 echo "Installing qemu-arm-static for armel-on-i386 support (maverick or earlier)"
     # sudo apt-get install qemu-user-static || exit 1
		 sudo apt-get install qemu-arm-static || exit 1
     # Don't add debootstrapopts options, build-arm-chroot uses --arch itself and gets
     # confused with any extra options
	   DEBOOTSTRAP_ARGS="--debootstrap build-arm-chroot"
	fi
else
	DEBOOTSTRAP_ARGS="--debootstrapopts --variant=buildd --debootstrapopts --arch=$ARCH"
fi

SRCDIR=${PBUILDER_SRCDIR:-$HOME/Projects}
mkdir -p "$SRCDIR/$PROJ"




# Now, we didn't tell pbuilder about the user's input sources.list.  We'll set
# that up as an extra step below (this allows user to input https sources even
# in hardy, which didn't install apt-transport-https by default)

#set -x
sudo sh -c "pbuilder create --basetgz \"/var/cache/pbuilder/$PROJ.tgz\" --distribution \"$DIST\" $DEBOOTSTRAP_ARGS --components \"$COMPONENTS\" --mirror \"$MIRROR\" --othermirror \"deb-src $MIRROR $DIST $COMPONENTS\" && pbuilder execute --save-after-exec --basetgz \"/var/cache/pbuilder/$PROJ.tgz\" --bindmounts \"$(dirname $TMPFILE)\" -- $(which pcreate-helper) \"$TMPFILE\" \"$EXTRAPACKAGES\""
#set +x

rm "$TMPFILE"
