#!/bin/sh -e

. /usr/share/debconf/confmodule
. /lib/preseed/preseed.sh # for $logfile

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

#Detect debian-edu-expert mode
if grep -iq debian-edu-expert /proc/cmdline ; then
   expert=true
else
   expert=false
fi

#Treat a expert install as a debian-edu-expert mode
if grep -iq 'priority=low' /proc/cmdline ; then
   expert=true
else
   expert=false
fi


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

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

log "choosing profile"
log "Debian-edu-expert = $expert"

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

#ask profile question
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
	exit 1;
    fi
done
EDUPROFILE=$RET
log "full profile is '$EDUPROFILE' loopcount='$loopcount'"


preseedfile=/tmp/debian-edu-preseed.$$
touch $preseedfile
add_preseed() {
    owner="$1"
    template="$2"
    type="$3"
    value="$4"
    echo $owner $template $type "$value" >> $preseedfile
}

#ask if the user want to repartition the hard drives, if not we will not install
#not in expert mode
RET=""
if test "$expert" = false ; then
   while test "$RET" != true ; do
	db_fset "debian-edu-install/wipe_out_my_harddrive" seen false || true
	db_input critical "debian-edu-install/wipe_out_my_harddrive" || true
	db_go || true
	db_get "debian-edu-install/wipe_out_my_harddrive" || true
	if test "$RET" = true ; then
           log "User accepted wiping out harrdrive"
	fi
   done
fi

#ask and preseed popcon here to save the need to monitor the install
RET=""
db_fset "debian-edu-install/participate-popcon" seen false || true
db_input critical "debian-edu-install/participate-popcon" || true
log "do we want popcon?"
db_go || true 
db_get "debian-edu-install/participate-popcon" || true
if test "$RET" = true ; then
	add_preseed popularity-contest popularity-contest/participate boolean true
	log "popcon: yes!"
elif test "$RET" = false ; then
	add_preseed popularity-contest popularity-contest/participate boolean false
	log "popcon: no!"
fi


tasksel=
preseed=
#if a value is unset it breaks the case esac later on
workstation=false
ltspserver=false
server=false
networked=false
standalone=false
barebone=false

for value in `echo $EDUPROFILE |sed 's/ /-/g' | sed 's/,-/ /g'`; do
    case $value in
    Workstation)
        networked=true
        workstation=true
        tasksel="${tasksel:+$tasksel, }DebianEdu networked workstation packages"
        log "Added task '$value'"
        ;;
    Thin-Client-Server)
        networked=true
        workstation=true
        ltspserver=true
        tasksel="${tasksel:+$tasksel, }DebianEdu networked thin client server packages"
        log "Added task '$value'"
        ;;
    Main-Server|Server)
        networked=true
        server=true
        tasksel="${tasksel:+$tasksel, }DebianEdu main server packages"
        log "Added task '$value'"
        ;;
    Standalone)
        standalone=true
        tasksel="DebianEdu standalone workstation packages"
        log "Added task '$value'"
        ;;
    Barebone)
        networked=true
	barebone=true
        log "Added task '$value'"
        ;;
    *)
        error "unknown profile '$value'"
        ;;
    esac
done

# Make sure the default values have this priority, with lower number
# priority overriding higher number
#  1 main-server
#  2 thin-client-server
#  3 workstation
#  4 networked (Common for non-standalone)
#  5 standalone
#  6 common

preseeds="common"

if test "$standalone" = true ; then
    preseeds="$preseeds standalone"
fi
if test "$networked" = true ; then
    preseeds="$preseeds networked"
fi
if test "$workstation" = true ; then
    preseeds="$preseeds workstation"
fi
if test "$ltspserver" = true ; then
    preseeds="$preseeds thin-client-server"
fi
if test "$server" = true ; then
    preseeds="$preseeds main-server"
fi

# FIXME
# Need to create some partitioning templates, based on which profile
# is selected
#
#  preseed correct partman recipe
#

#use first available disk no matter what ?

automatic_partitioning() {
    log "Using automatic_partitioning"
    case "$server-$workstation-$ltspserver-$standalone" in
    "true-false-false-false") # single main server
        log "Preseeding partman for Single Main-server"
        db_set "partman-auto/choose_recipe" Debian-Edu Main-Server
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/90edumain
        ;;
    "false-true-true-false") # single ltspserver
        log "Preseeding partman for Single Thin-Client-Server"
        db_set "partman-auto/choose_recipe" Debian-Edu Thin-Client-Server
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/94edultsp

        # Configure the enable-nat script from debian-edu-config to
        # enable NAT on Thin-Client-Servers but not on Combi-Servers
        add_preseed debian-edu-config debian-edu-config/enable-nat boolean true
        ;;
    "false-true-false-false") # single workstation
        log "Preseeding partman for Single Workstation"
        db_set "partman-auto/choose_recipe" Debian-Edu Workstation
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/96eduwork
        ;;
    "true-false-true-false") #mainserver & ltspserver
        log "Preseeding partman for Combo Main-server and Thin-Client-Server"
        db_set "partman-auto/choose_recipe" Debian-Edu Main-Server and Thin-Client-Server
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/91edumain+ltsp

        # Configure slbackup here in case we have a combo server installation
        add_preseed slbackup slbackup/client_location string "/etc /skole/tjener/home0 /opt/ltsp/i386/etc /var/backups"
        ;;
    "true-true-false-false") # mainserver & workstation
        log "Preseeding partman for Combo Main-Server and Workstation"
        db_set "partman-auto/choose_recipe" Debian-Edu Main-Server and Workstation
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/92edumain+ws
        ;;
    "true-true-true-false") # mainserver & ltspserver & workstation
        log "Preseeding partman for Combo Main-server, Thin-Client-Server, and workstation"
        db_set "partman-auto/choose_recipe" Debian-Edu Main-Server and Thin-Client-Server
        db_set "partman-auto/expert_recipe_file" /lib/partman/recipes/91edumain+ltsp

        ;;
    "false-false-false-true") # standalone
        log "Preseeding partman for Single Standalone, select no recipe they get to partition manualy."
        # have no idea if this works, must test
        db_fset "partman-auto/choose_recipe" seen false || true
        db_input critical "partman-auto/choose_recipe"  || [ $? -eq 30 ]
        ;;
   esac

   if [ false = "$standalone" ] ; then
       # if not standalone set recipe selection to seen
       db_fset "partman-auto/choose_recipe" seen true || true
       db_set "partman-auto-lvm/new_vg_name" vg_system

       # we dont want to see this
       db_set "partman/choose_partition" "Finish partitioning and write changes to disk"
       db_fset "partman/choose_partition" seen true || true

       # use first available disk
       db_set "partman-auto/disk" /dev/discs/disc0/disc
       #and method lvm 
       db_set "partman-auto/method" lvm
       db_fset "partman-auto/method" seen true || true
       db_set "partman-lvm/confirm" true
       db_fset "partman-lvm/confirm" seen true || true

       # we do not want to see the confirm write to disk, or the purge lvm questions. We have asked this already
       db_set "partman/confirm_write_new_label" true || true
       db_set "partman/confirm" true || true
       db_fset "partman/confirm" seen true || true
       db_set "partman-auto/purge_lvm_from_device" true || true
       db_fset "partman-auto/purge_lvm_from_device" seen true || true
       
   fi
}

# use manual partitioning in debian-edu-expert mode
if [ false = "$expert" ] ; then
	automatic_partitioning
	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/"
	SUITE=stable
    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

log "Preseeding"

# Test if this has working pcmcia, and if so assume that it's a laptop
if [ -e /proc/bus/pccard/drivers ]; then
    log "Found working pcmcia, assume it's a laptop"
    tasksel="${tasksel:+$tasksel, }Laptop, DebianEdu laptop packages"
fi

if [ "$EDUPROFILE" ] ; then
   log "Preseeding target with '$EDUPROFILE'"
   add_preseed debian-edu-install debian-edu-install/profile multiselect "$EDUPROFILE"
else
   log "No profile to preseed"
fi

if [ "$tasksel" ] ; then
    log "Preseeding tasksel with '$tasksel'"
    add_preseed tasksel tasksel/first multiselect "$tasksel"
else
    log "No tasksel to preseed"
fi

for preseed in $preseeds ; do
    if [ -f /usr/lib/debian-edu-install/defaults.$preseed ] ; then
        log "Found preseeding for '$preseed'"
        cat /usr/lib/debian-edu-install/defaults.$preseed >> $preseedfile
    else
        log "Unable to find preseeding for '$preseed'"
    fi
done

# Enable the firstboot script to report installation errors.  Preseeded
# here and not in defaults.common to make sure it only is set on first time
# installation using debian-installer
add_preseed debian-edu-install debian-edu-install/run-firstboot boolean true

# Load into the cdebconf database used by d-i
debconf-set-selections $preseedfile || \
    log "Failed to load preseed values from $preseedfile"

# Get the post-base-installer.d fragment from preseed to pass relevant
# values into the debconf database in /target when the base system is
# installed.
touch $logfile # Create if missing, to make sure >> work
cat $preseedfile >> $logfile

# rm $preseedfile

# Make sure that the kernel question doesn't show up
db_fset "base-installer/kernel/image" seen true || true
db_fset "base-installer/kernel/which-kernel" seen true || true

# Hide tune2fs to force parman-ext3 to use mkfs.ext3 and thus
# get -O resize_inode enabled in the file system.
if [ -x /sbin/tune2fs ]; then
    mv /sbin/tune2fs /sbin/tune2fs.dontuse
fi

# I doubt it work to fset xserver-xorg and x11-common here, as it
# would only affect the d-i database and not the database in
# /target/. [pere 2006-09-25]

# Try to avoid the xserver questions which will be solved by
# xdebconfigurator later on
db_fset "xserver-xorg/config/display/modes" seen true || true

# Avoid message which sometimes appears with newer x11-common
# due to upgrade issues
db_fset "x11-common/upgrade_issues" seen true || true
