#!/bin/sh -e

. /usr/share/debconf/confmodule

# Use manual partitioning on standalone, to make it easier for people
# to set up dualboot on their home machine
manual_standalone=true

template="debian-edu-install/profile"

log() {
    logger -t profilechooser "info: $*"
}

log "choosing profile"

RET=""
looplimit="xxx"
loopcount=""

while test -z "$RET" ; do
    db_fset "$template" seen false || true
    db_input critical "$template"  || [ $? -eq 30 ]
    db_go || true
    db_get "$template" || true
    loopcount="x$loopcount"
    if test "$loopcount" = "$looplimit" ; then
	break;
    fi
done

log "profile = '$RET' loopcount='$loopcount'"

if [ "$(uname -m)" = "ppc" ] ; then 
  log "Arch PowerPC detected"
elif [ true = "$manual_standalone" ] && echo "$RET" | grep -q Standalone ; then
	# For standalone installs, skip autopartkit, let the regular
	# partitioner be used.
	db_set autopartkit/hide true
fi

# The latest version of languagechooser insert nb_NO instead of no_NO.
# nb_NO one is missing in Woody, so we rewrite it to no_NO.  This is a
# workaround for debian bug #260873.
# This is a hack that should go away when nb_NO is available
# everywhere. [pere 2004-07-22]

# This code need to be executed here and pass the info found on to the
# prebaseconfig script, and not in the prebaseconfig script itself,
# because the CD is unmounted when the prebaseconfig script is running.

# Based on function from base-installer
set_mirror_info () {
    if [ -f /cdrom/.disk/base_installable ]; then
	PROTOCOL=file
	MIRROR=""
	DIRECTORY="/cdrom/"
    else
	mirror_error=""

	db_get mirror/protocol || mirror_error=1
	PROTOCOL="$RET"

	db_get mirror/$PROTOCOL/hostname || mirror_error=1
	MIRROR="$RET"

	db_get mirror/$PROTOCOL/directory || mirror_error=1
	DIRECTORY="$RET"

	if [ "$mirror_error" = 1 ] || [ -z "$PROTOCOL" ] || [ -z "$MIRROR" ]; then
	    log "Missing mirror settings.  Can not find DISTRIBUTION name."
	    return
	fi
    fi

    if db_get mirror/suite && [ "$RET" ] ; then
	SUITE=$RET
    fi
    # Find the distribution codename
    APTLISTDIR=/var/tmp
    mkdir -p $APTLISTDIR
    if [ file = "$PROTOCOL" ]; then
	cp /cdrom/dists/$SUITE/Release $APTLISTDIR/tmp || nogetrel="/cdrom/dists/$SUITE/Release"
    else
	if [ "$PROTOCOL" = "http" ]; then
	    db_get mirror/http/proxy
	    http_proxy="$RET" || true
	    if [ "$http_proxy" ]; then
		export http_proxy
	    fi
	fi

	wget "$PROTOCOL://$MIRROR$DIRECTORY/dists/$SUITE/Release" -O $APTLISTDIR/tmp || nogetrel="$PROTOCOL://$MIRROR$DIRECTORY/dists/$SUITE/Release"
    fi

    if [ "$nogetrel" != "" ]; then
	log "Unable to locate '$nogetrel'.  Can not find DISTRIBUTION name."
	return
    fi

    DISTRIBUTION=`grep ^Codename: $APTLISTDIR/tmp | cut -d' ' -f 2`
    rm $APTLISTDIR/tmp
    db_set mirror/distribution "$DISTRIBUTION"
}

set_mirror_info

