#! /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 "$@"
}

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

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

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

db_get debian-installer/language
LANGCODE=$RET

db_get debian-installer/locale
LOCALE=$RET

if [ "$DISTRIBUTION" != "woody" ] ; then
  log "This is not woody, let me check for locale no_NO -> nb_NO conversion"
  if [ "$LOCALE" = "no_NO" ] ; then 
    log "Changing locale from no_NO -> nb_NO"
    LOCALE=nb_NO
    db_set debian-installer/locale $LOCALE
  fi
fi

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?
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|Standalone-Extras)
 	    # Dont set autoeth0 on Standalone
	    if [ -z "$eth0" ] ; then
		eth0=dhcp
		autoeth0=""
	    fi
	    ;;
	Workstation)
 	    # Use this unless Server also was choosen.
	    if [ -z "$eth0" ] ; then
		eth0=dhcp
		autoeth0="auto eth0"
	    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
	    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"
	    ;;
     esac
done
  
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" 
  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
    # Also generate a new initrd.img Debian Bug #295456
    #chroot /target dpkg-reconfigure -pcritical -fNoninteractive \
    #                  kernel-image-$(basename /target/lib/modules/*)
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

if [ "$DISTRIBUTION" = "woody" ] ; then
    CONF=/target/root/dbootstrap_settings
    if grep -q '"nb_NO"' $CONF ; then
	log "Rewriting nb_NO locale to no_NO."
	sed 's/"nb_NO"/"no_NO"/' < $CONF > $CONF.new && mv $CONF.new $CONF
    else
	log "Not rewriting nb_NO to no_NO."
    fi
else
    log "Found suite '$SUITE' codename '$DISTRIBUTION'."
    FILES="/root/dbootstrap_settings /etc/environment /etc/locale.gen"
    DOLOCALES=false
    for file in $FILES ; do
	tfile=/target$file
	if grep -q '"no_NO"' $tfile ; then
	    log "Rewriting no_NO locale to nb_NO in $tfile."
	    sed 's/"no_NO"/"nb_NO"/' < $tfile > $tfile.new && mv $tfile.new $tfile
	    DOLOCALES=true
	elif grep -q 'no_NO ' $tfile ; then
	    log "Rewriting no_NO locale to nb_NO in $tfile."
	    sed 's/no_NO /nb_NO /' < $tfile > $tfile.new && mv $tfile.new $tfile
	    DOLOCALES=true
	else
	    log "Not rewriting no_NO to nb_NO in $tfile."
	fi
    done
    if [ true = "$DOLOCALES" ] ; then 
        log "Regenerating locales"
        chroot /target locale-gen
    else
        log "Not regenerating locales"
    fi
fi

# this is a workaround for mount on woody not resolving symlinks 
# (#285607 in BTS)
if [ "$DISTRIBUTION" = "woody" ] ; then
    CONF=/target/etc/fstab
    if grep -q '/media/cdrom0' $CONF ; then
	log "Rewriting /media/cdrom0 mountpoint to /cdrom."
	sed 's:/media/cdrom0:/cdrom:' < $CONF > $CONF.new && mv $CONF.new $CONF
    else
	log "Not rewriting /media/cdrom0 mountpoint to /cdrom."
    fi
fi
