# $Id: vserver-build.debootstrap,v 1.16 2006/01/22 12:36:07 ensc Exp $	--*- sh -*--

# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

function cleanUp
{
    test ! -d "$workdir"  || $_RM -rf "$workdir"
}

function init
{
    workdir=
    mirror=
    trap "cleanUp" EXIT
}

function initVariables
{
    base.init

    test -n "${mirror-unset}" || {
	local aux
	findFile aux "$__CONFDIR"/.defaults/apps/debootstrap/mirror ''
	test -z "$aux" || read mirror <"$aux"
    }
}

function initFilesystem
{
    base.initFilesystem "$1"

    mkdir -p "$SETUP_CONFDIR"/apps/pkgmgmt
    touch "$SETUP_CONFDIR"/apps/pkgmgmt/internal
}

function findDebootstrap
{
    test ! -x "$DEBOOTSTRAP" || return 0

    pushd . &>/dev/null
    DEBOOTSTRAP=$(which debootstrap 2>/dev/null) || {
        dir=$($_MKTEMPDIR -p /var/tmp debootstrap.XXXXXX)
        workdir=$dir
        dst=$dir/debootstrap.deb
        
        findFile DEBOOTSTRAP_URI "$__CONFDIR"/.defaults/apps/debootstrap/uri "$__PKGLIBDEFAULTDIR"/debootstrap.uri
        read tmp <$DEBOOTSTRAP_URI
        case "$tmp" in
    	(/*)			ln -s "$tmp" "$dst";;
    	(http://*|ftp://*)
	    echo $"\
Could not find local version of 'debootstrap'; downloading it from
${tmp}..."
	    $_WGET -nv -O "$dst" "$tmp" || {
		$_CAT <<EOF >&2
ERROR: Could not download the debootstrap package from

   $tmp

Usually, this means that Debian released a new version which is unknown
to util-vserver and removed the known ones. To fix this, go to

   http://ftp.debian.org/debian/pool/main/d/debootstrap/

(or a nearby mirror) and search the URL for the most recent *.deb
package matching your platform. Then, put this URL into

   $__CONFDIR/.defaults/apps/debootstrap/uri

and retry the vserver-build command again.
EOF
		exit 1
	    }
	    ;;
    	(*)			echo $"Unsupported URI scheme '$tmp'" >&2
    				exit 1;;
        esac
        cd $dir
        ar x "$dst"
        tar xzf data.tar.gz
        export DEBOOTSTRAP_DIR=`pwd`/usr/lib/debootstrap
        DEBOOTSTRAP=`pwd`/usr/sbin/debootstrap

	local arch
	arch=$(uname -i 2>/dev/null) || \
	    arch=$(arch 2>/dev/null) ||
	    arch=

	## Wash the calculated architecture
	case $arch in
	    (i?86|athlon)	arch=i386;;
	esac

	test -z "$arch" || echo "$arch" >$DEBOOTSTRAP_DIR/arch
    }
    popd &>/dev/null

    test -x "$DEBOOTSTRAP" || { echo $"Can not find debootstrap at '$DEBOOTSTRAP'" >&2; exit 1; }
}

function fixupDebian
{
    $_RM -rf "$1"/dev
    $_MV "$1"/dev.X "$1"/dev
}


###   main starts here <---


tmp=$(getopt -o '+d:m:' --long debug -n "$0" -- "$@") || exit 1
eval set -- "$tmp"

init

while true; do
    case "$1" in
	(-d)		DISTRIBUTION=$2; shift;;
	(-m)		mirror=$2;       shift;;
	(--debug)	set -x;;
	(--)		shift; break ;;
	(*)		echo "vserver-build.debootstrap: internal error: unrecognized option '$1'" >&2
			exit 1
			;;
    esac
    shift
done

getDistribution '' 1

initVariables  
initFilesystem "$OPTION_FORCE"

setup_writeOption "$VSERVER_NAME"
setup_writeInitialFstab

findDebootstrap

test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE" "$SETUP_CONFDIR"  "$UTIL_VSERVER_VARS"
mv "$VDIR"/dev "$VDIR"/dev.X
"$DEBOOTSTRAP" "$@" "$DISTRIBUTION" "$VDIR" "$mirror" || :	## HACK: ignore all errors...
fixupDebian "$VDIR"
test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"

base.setSuccess
