#! /bin/sh -e
#
# Pass the information needed by debian-edu-install into
# /target/etc/debian-edu/config.

. /usr/share/debconf/confmodule

log() {
    logger -t 75debian-edu-profile "$@"
}

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

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

# 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
 
interfaces=/target/etc/network/interfaces

# 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

# Should it get the information from the netcfg package instead?
  
# 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
	Workstation|Standalone|Standalone-Extras)
 	    # Use this unless Server also was choosen.
	    if [ -z "$eth0" ] ; then
		eth0=dhcp
	    fi
	    ;;
	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
	    ;;
	Thin-Client-Server)
 	    # Use this unless Server also was choosen.
	    if [ -z "$eth0" ] ; then
		eth0=dhcp
	    fi
	    eth1=192.168.0.254:255.255.255.0:192.168.0.255:none
	    ;;
     esac
done
  
for interface in eth0 eth1 ; do
    eval "ifinfo=\$$interface"
    case $ifinfo in
	dhcp)
	    cat >> $interfaces <<EOF

auto $interface
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" 
  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

# 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

(
  echo "search intern"
  echo "nameserver $NAMESERVER"
) > /target/etc/resolv.conf

# Turn on devfs if devfsd is installed.  Using config file as a flag
# file to detect installed devfsd.
file=/target/etc/default/devfsd
if [ -f "$file" ] ; then
    log "Enabling devfs on /dev/."
    sed 's/^MOUNT=no/MOUNT=yes/' < "$file" > "$file.new" &&
        mv "$file.new" "$file"
fi

# /target/usr/bin/awk is a symlink into /etc/alternatives, ie nowhere in d-i.
# I hope mawk is always available
AWK=/target/usr/bin/mawk

# Convert ext2 to ext3.  This is a hack to work around the Skolelinux
# bug #270, Debian bug #177347.  This should be fixed in
# autopartkit/libparted instead.
for dev in `grep /target /proc/mounts | $AWK '/ext2/ {print $1}'` ; do
    log "Converting ext2 to ext3 on $dev."
    /target/sbin/tune2fs -j $dev 1>&2
    converted=yes
done
if [ yes = "$converted" ] ; then
    log "Updating ext2 to ext3 in /target/etc/fstab."
    sed s/ext2/ext3/ < /target/etc/fstab > /target/etc/fstab.ext3 &&
    mv /target/etc/fstab.ext3 /target/etc/fstab
else
    log "Not updating /target/etc/fstab."
fi

# Hack to work around bug #431, Debian bug #135334.
if grep -q ^se_NO /target/usr/share/i18n/SUPPORTED ; then
    :
else
    echo se_NO UTF-8 >> /target/usr/share/i18n/SUPPORTED
fi
