#!/bin/sh -e
#
# Executed using the post-base-installer hooks after the base system
# is installed, and before most packages are installed.
#
# Pass the information needed by debian-edu-install into
# /target/etc/debian-edu/config.

. /usr/share/debconf/confmodule

log() {
    logger -t 10debian-edu-preseed "$@"
}

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
        log "error: unable to create /target/etc/debian-edu"
    fi
fi

# For the stable etch release, use '3.0r0 terra' as the version number
(
    echo "# Generated by debian-edu-profile-udeb"
    echo "VERSION=terra_rc5"
    echo "PROFILE=\"$PROFILE\""
    echo "LANGCODE=\"$LANGCODE\""
    echo "LOCALE=\"$LOCALE\""
) >> /target/etc/debian-edu/config

# Avoid hangs during installation of main-server (See #375077)
mkdir -p /target/var/lib/nss-ldap
touch /target/var/lib/nss-ldap/bind_policy_soft

if db_get mirror/suite && [ "$RET" ] ; then
    SUITE=$RET
fi

if db_get mirror/distribution && [ "$RET" ] ; then
    DISTRIBUTION=$RET
fi

# install debian-edu-install in chroot if a network install
if grep -q "^deb file:///cdrom" /target/etc/apt/sources.list ; then 
  log "Doing nothing, cd-based install should install debian-edu-config automaticly"
elif grep -qe "^deb.*local" /target/etc/apt/sources.list ; then 
  log "local apt-source is already defined, debian-edu-config should be present"
else
  # Check if there is a local source available from the same location
  # as the main source is
  MAINSOURCE=$(sed -ne 's:^deb \(.*\) \(.*\) main.*:\1:p' /target/etc/apt/sources.list | head -1)
  if wget -qO - $MAINSOURCE/dists/$DISTRIBUTION/Release | grep "local/binary" ; then 
    log "Adding local apt-source from the same location as main"
    sed -ne 's:^\(deb.*\)main.*:\1local:p' /target/etc/apt/sources.list >> /target/etc/apt/sources.list
      log "Updating Package list"
      chroot /target apt-get update
  fi
fi

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" ] ; 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

    # 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."
	in-target /etc/init.d/networking start || 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
}

configure_network

# Munin hangs during installation if bind is not working during installation
# Debian BTS #275024
# Setting the wanted named.conf before bind is installed seems to work
# around the problem
if [ "tjener.intern" = "$HOSTNAME" -a ! -f /target/etc/bind/named.conf ] ; then
  mkdir -p /target/etc/bind
  # try to avoid symlinking the configfile.
  #  ln -s debian-edu/named-bind9.conf /target/etc/bind/named.conf
  #provide our own defaultfile instead
   echo '#Defaults file for debian edu' > /target/etc/default/bind9 
   echo 'OPTIONS="-u bind -c /etc/bind/debian-edu/named-bind9.conf"' >>  /target/etc/default/bind9
   echo '# Set RESOLVCONF=no to not run resolvconf' >> /target/etc/default/bind9
   echo 'RESOLVCONF=yes' >> /target/etc/default/bind9
fi

exit 0
