#!/bin/sh -e

. /usr/share/debconf/confmodule

log() {
    logger -t edu-pre-pkgsel "info: $*"
}

error() {
    logger -t edu-pre-pkgsel "error: $*"
}

setup_tasksel_overrides() {
    # Modify tasksels desktop test so the normal Debian desktop
    # profiles are not installed. Use dpkg-divert to to switch to our
    # version
    sed -e 's/if desktop_hardware/# Do not install a desktop - added by debian-edu-install\nunmark\n\n&/' \
	< /target/usr/lib/tasksel/tests/desktop \
	> /target/usr/lib/tasksel/tests/desktop.edu
    chmod 755 /target/usr/lib/tasksel/tests/desktop.edu
    chroot /target dpkg-divert --package debian-edu-install --rename --quiet \
	--add /usr/lib/tasksel/tests/desktop
    ln -s ./desktop.edu /target/usr/lib/tasksel/tests/desktop

    # And for the standard system task too
    sed -e 's/^case/# Do not install standard system task - added by debian-edu-install\nexit 3\n\n&/' \
	< /target/usr/lib/tasksel/tests/new-install \
	> /target/usr/lib/tasksel/tests/new-install.edu
    chmod 755 /target/usr/lib/tasksel/tests/new-install.edu
    chroot /target dpkg-divert --package debian-edu-install --rename --quiet \
	--add /usr/lib/tasksel/tests/new-install
    ln -s ./new-install.edu /target/usr/lib/tasksel/tests/new-install
}

configure_network() {
# Write a functional /target/etc/network/interfaces
    if [ -z "$PROFILE" ]; then
	PROFILE=Workstation
    fi

    # Default hostname is 'localhost'
    HOSTNAME=localhost

    # Default DNS server is tjener.intern
    NAMESERVER=10.0.2.2

    DNSDOMAIN=intern
    MAILNAME=postoffice.intern

    interfaces=/target/etc/network/interfaces

    # Should it get the information from the netcfg package instead?
    eth0=dhcp
    autoeth0="auto eth0"
    eth1=dhcp
    autoeth1=

    # Hm, what if both server and workstation is choosen?  Choose the
    # server config for eth0.
    for value in `echo $PROFILE |sed 's/ /-/g' | sed 's/,-/ /g'`; do
	case $value in
	    Standalone)
 	        # Leave network configuration to network-manager on Standalone
		eth0=dhcp
		autoeth0="allow-hotplug eth0"
		eth1=dhcp
		autoeth1="allow-hotplug eth1"
		RUNXSERVER=true
		DNSDOMAIN=
		MAILNAME=
		;;
	    Workstation)
   	        # Use this unless Server also was choosen.
		if [ -z "$eth0" ] ; then
		    eth0=dhcp
		    autoeth0="auto eth0"
		fi
		RUNXSERVER=true
		;;
	    Main-Server)
 	        # Override for workstations combining as servers
		eth0=10.0.2.2:255.255.254.0:10.0.3.255:10.0.2.1
		HOSTNAME=tjener.intern
		NAMESERVER=127.0.0.1
		autoeth0="auto eth0"
		;;
	    Thin-Client-Server)
 	        # Use this unless Server also was choosen.
		if [ -z "$eth0" ] ; then
		    eth0=dhcp
		    autoeth0="auto eth0"
		fi
		eth1=192.168.0.254:255.255.255.0:192.168.0.255:none
		autoeth1="auto eth1"
		RUNXSERVER=true
		;;
	esac
    done
  
    # Every host need the loopback interface
    cat > $interfaces <<EOF
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# This file was created by debian-edu-profile during the Debian installation

# The loopback interface
auto lo
iface lo inet loopback
EOF
    if [ "$DNSDOMAIN" ] && [ "$NAMESERVER" = "127.0.0.1" ] ; then
	cat >> $interfaces <<EOF
    dns-search $DNSDOMAIN
EOF
    fi

    for interface in eth0 eth1 ; do
	eval "ifinfo=\$$interface"
	eval "ifauto=\$auto$interface"
	case $ifinfo in
	    dhcp)
		cat >> $interfaces <<EOF

$ifauto
iface $interface inet dhcp
EOF

		;;
	    [0-9]*)
	        address=`echo $ifinfo | cut -d: -f1`
		netmask=`echo $ifinfo | cut -d: -f2`
		broadcast=`echo $ifinfo | cut -d: -f3`
		gateway=`echo $ifinfo | cut -d: -f4`
		cat >> $interfaces <<EOF

auto $interface
iface $interface inet static
    address $address
    netmask $netmask
    broadcast $broadcast
EOF
		if [ none != "$gateway" ] ; then
		    cat >> $interfaces <<EOF
    gateway $gateway
EOF
		fi
		cat >> $interfaces <<EOF
# The commented lines below is to be used if a DHCP server is in use
#iface $interface inet dhcp
EOF
		;;
	    *)
	        # Nothing to do?
	        ;;
	esac
    done
    (
	echo "127.0.0.1       localhost.localdomain localhost" 
	echo "::1             localhost       ip6-localhost ip6-loopback"
	echo "fe00::0         ip6-localnet"
	echo "ff00::0         ip6-mcastprefix"
	echo "ff02::1         ip6-allnodes"
	echo "ff02::2         ip6-allrouters"
	echo "ff02::3         ip6-allhosts"
    ) > /target/etc/hosts

    echo "$HOSTNAME" > /target/etc/hostname
    in-target /etc/init.d/hostname.sh start

    # Update hostname based on reverse DNS entry of current IP, unless
    # installing main-server.
    if echo $PROFILE | grep -q Main-Server ; then
	:
    else
	in-target /etc/init.d/update-hostname start
    fi

    # Avoid hardcoding entries on the clients, to make sure IP address
    # range can be changed on the clients by changing DHCP
    # configuration on the server.
    if [ "tjener.intern" = "$HOSTNAME" ] ; then
	(
	    echo
	    echo "10.0.2.2        tjener.intern tjener" 
	) >> /target/etc/hosts
    fi

    # Set /etc/mailname if it is missing
    if [ "$MAILNAME" ] && [ ! -f /target/etc/mailname ] ; then
	echo "$MAILNAME" > /target/etc/mailname
    fi

    # Make sure that the interfaces are there for the cfengine run if
    # network isn't already configured
    if route | grep -q default ; then
	log "Not restarting network, as it seem to be up already."
    else
	log "Restarting network to prepare for cfengine run."
	# Redirecting fd 3 as a workaround for skolelinux bug #1229.
	# make sure the redirecting happen inside the chroot, as
	# in-target need to talk to debconf.
	in-target /bin/sh -c "/etc/init.d/networking start 3> /dev/null" || true
    fi

    # Update hostname based on DHCP/DNS if enabled
    if [ -x /etc/init.d/update-hostname ] ; then
	in-target /etc/init.d/update-hostname start || true
    fi
}

set_kerberos_masterpwd() {
    if db_get passwd/root-password-crypted && [ "$RET" ] ; then
	log "No clear text root password, unable to pass it on as kerberos master"
    else
	db_get passwd/root-password
	file=/tmp/kerberos-masterpwd-preseed
	cat > $file <<EOF
debian-edu-config debian-edu-config/kdc-password password $RET
debian-edu-config debian-edu-config/kdc-password-again password $RET
EOF
	debconf-set-selections $file
	rm $file
    fi
}

db_get mirror/protocol || true
PROTOCOL="$RET"
if [ "$PROTOCOL" = "http" ]; then
    db_get mirror/http/proxy
    http_proxy="$RET" || true
    if [ "$http_proxy" ]; then
	export http_proxy
    fi
fi

db_get debian-edu-install/profile
PROFILE="$RET"

db_get debian-installer/language
LANGCODE="$RET"

db_get debian-installer/locale
LOCALE="$RET"

if [ ! -d /target/etc/debian-edu ] ; then
    if mkdir /target/etc/debian-edu ; then
        :
    else
        error "unable to create /target/etc/debian-edu"
    fi
fi

set_kerberos_masterpwd || true

# For the stable etch release, use '3.0r0 terra' as the version number
VERSION="$(cat /usr/lib/debian-edu-install/version || true)"
if [ -z "$VERSION" ] ; then
    error "missing /usr/lib/debian-edu-install/version, incomplete /etc/debian-edu/config created."
fi

(
    echo "# Generated by debian-edu-profile-udeb"
    echo "VERSION=\"$VERSION\""
    echo "PROFILE=\"$PROFILE\""
    echo "LANGCODE=\"$LANGCODE\""
    echo "LOCALE=\"$LOCALE\""
) >> /target/etc/debian-edu/config

if edu-is-testinstall ; then
    echo "TESTINSTALL=\"true\"" >> /target/etc/debian-edu/config
fi

edu-etcvcs commit

log "asking for a few extra packages to be installed"
# debian-edu-archive-keyring - our local archive keyring
# education-tasks            - education tasksel tasks
# debian-edu-install         - the debian-edu preseeding files
failed=""
for p in debian-edu-archive-keyring education-tasks debian-edu-install
do
    if apt-install $p ; then
	:
    else
	template=debian-edu-install/errors-pkg-installation
	db_subst $template PACKAGES "$p"
	db_fset $template seen false
	db_capb "" # Disable "Go Back" button
	db_settitle $template-title
	db_input critical $template || [ $? -eq 30 ]
	db_go
	db_capb backup
	reboot
    fi
done

edu-etcvcs commit

# Clean up file added in base-installer-late, now that
# debian-edu-config is installed (pulled in via debian-edu-install).
rm -f /target/etc/apt/apt.conf.d/90squid-di

setup_tasksel_overrides

edu-etcvcs commit

# Configure network also on DVDs to make sure cfengine find a working
# network.
configure_network

edu-etcvcs commit

hook=/target/usr/share/debian-edu-config/d-i/pre-pkgsel
if [ -x $hook ] ; then
    log "Running debian-edu-config pre-pkgsel hook"
    $hook
fi

exit 0
