#!/bin/sh
#
# Copyright © 2006-2011 Jonas Smedegaard <dr@jones.dk>
# Description: Create Copy-On-Write environment
#
# 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, 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., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.
#
# Depends: cowdancer, sudo

set -e

PRG=$(basename "$0")

showhelp() {
	cat <<EOF
Usage:   $PRG POOL [ DISTRIBUTION [ OTHERMIRROR ... ] ]

Examples:
    $PRG sid
    $PRG sid+mm sid 'deb http://debian-multimedia.org/ sid main'
    $PRG sid-ia32 sid --arch i386
EOF
}

exit1() {
	echo "ERROR: $1"
	exit 1
}

ask() {
	echo -n "$1 (y/N)? "
	read response
	case "$response" in
	    y|Y)
		:
		;;
	    *)
		return 1
		;;
	esac
	return
}

if [ $# -eq 0 ]; then
	showhelp
	exit1 "not enough parameters"
fi

pool=
distro=
arch=
newline='
'
while [ $# -gt 0 ]; do
	case $1 in
		-h|--help)
			showhelp
			exit 0
			;;
		--arch|-a)
			arch="$2"
			shift 2 || exit1 "option \"--arg\" requires an argument"
			;;
		--)
			shift
			break
			;;
		--*|-*)
			exit1 "options (apart from --arch) are curently unsupported"
			;;
		*)
			if [ -z "$pool" ]; then
				pool="$1"
			elif [ -z "$distro" ]; then
				distro="$1"
			else
				othermirrors="${othermirrors:+$othermirrors$newline}$1"
			fi
			shift
			;;
	esac
done

if [ -z "$pool" ]; then
	exit1 "no pool provided"
fi
if [ -z "$distro" ]; then
	distro="$pool"
fi

basepath="/var/cache/pbuilder/cow-$pool"
aptcache=""
buildresult="$HOME/src/pbuild-$pool/"

makepaths=''
for path in "$buildresult"; do
	if [ ! -d "$path" ]; then
		echo "W: Needed path \"$path\" does not exist"
		makepaths='yes'
	fi
done
if [ "$makepaths" = 'yes' ]; then
	[ "$force" = 'yes' ] \
		|| ask 'Create the missing path(s)' \
		|| exit1 "Cannot continue without those missing paths"
	mkdir -p "$buildresult"
fi

if [ -d "$basepath" ]; then
	echo "W: Needed path \"$basepath\" already exists"
	[ "$force" = 'yes' ] \
		|| ask 'Remove the path' \
		|| exit1 "Cannot continue with that path already there"
	sudo rm -rf "$basepath"
fi

if [ -n "$arch" ]; then
echo "$othermirrors" | perl -e "while (<>) {/\w/ and unshift @m, '--othermirror', \$_};"\
"exec 'sudo', 'cowbuilder', '--create', '--buildresult', '$buildresult', '--distribution', '$distro', '--basepath', '$basepath', '--aptcache', '$aptcache', '--debootstrapopts', '--arch', '--debootstrapopts', '$arch', @m"
else
echo "$othermirrors" | perl -e "while (<>) {/\w/ and unshift @m, '--othermirror', \$_};"\
"exec 'sudo', 'cowbuilder', '--create', '--buildresult', '$buildresult', '--distribution', '$distro', '--basepath', '$basepath', '--aptcache', '$aptcache', @m"
fi