#!/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

# This one does not work, please redesign
check_mode=`cat /proc/cmdline | grep -i debian-edu-expert` || /bin/true
template="debian-edu-install/profile"

if [ -n "$check_mode" ] ; then
	template=debian-edu-install/profile-expert
fi

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'"

# FIXME
# Need to create some partitioning templates, based on which profile is selected

# only the standalone profile should have local users
if echo "$RET" | grep -q Standalone ; then 
    log "Not disabling of a local user on the standalone profile"
else
    if db_set passwd/make-user false ; then 
        log "Successfully disabled creation of a local user"
        db_fset passwd/make-user seen true || true
    else
        log "Failed to disabled creation of a local user"
    fi
    if db_set clock-setup/utc true ; then 
        log "Always use UTC system clock"
        db_fset clock-setup/utc seen true || true
    else
        log "Failed to disabled creation of a local user"
    fi
fi

# disable special handling of standalone, since we now are using partman anyway
#if [ 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

