#!/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 19debian-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

(
    echo "# Generated by debian-edu-profile-udeb"
    echo "VERSION=terra_alpha"
    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
  fi
  log "Updating Package list"
  chroot /target apt-get update
  log "Fetching debian-edu-install"
  if chroot /target apt-get -qy install debian-edu-install ; then 
    log "installation of debian-edu-install was ok"
  else
    log "installation of debian-edu-install failed, continuing anyway"
  fi
fi

# 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
 
MAILNAME=postoffice.intern

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
    dns-search intern

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)
 	    # Dont set autoeth0 on Standalone
	    if [ -z "$eth0" ] ; then
		eth0=dhcp
		autoeth0=""
	    fi
	    RUNXSERVER=true
	    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
  
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

# 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

# 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
  ln -s debian-edu/named-bind9.conf /target/etc/bind/named.conf
fi

# Let resolvconf handle /etc/resolv.conf, it does a better job
#(
#  echo "search intern"
#  echo "nameserver $NAMESERVER"
#) > /target/etc/resolv.conf

# Detect installed kernel (2.4, 2.6 or ...) 
kernelsymlink=/target/vmlinuz
KERNEL=UNKNOWN
if [ -h "$kernelsymlink" ] ; then 
  case "$(basename $(readlink $kernelsymlink))" in 
    vmlinuz-2.6.*)
      KERNEL=KERNEL26
      log "Detected 2.6-kernel"
      ;;
    vmlinuz-2.4.*)
      KERNEL=KERNEL24
      log "Detected 2.4-kernel"
      ;;
    *)
      KERNEL=UNKNOWN 
      log "Unknown kernel version"
      ;;
  esac
fi

# Turn on devfs if devfsd is installed.  Using config file as a flag
# file to detect installed devfsd.
# but only if 2.4 kernel is used
file=/target/etc/default/devfsd
if [ -f "$file" -a "$KERNEL" = "KERNEL24" ] ; 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

log "Found suite '$SUITE' codename '$DISTRIBUTION'."

# 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

# 2.6-kernel on i386 seems to need mousedev loaded during boot
if [ "$RUNXSERVER" -a "$KERNEL" = "KERNEL26" ] ; then
    case $(uname -m) in 
      i386|i686)
        echo "mousedev" >> /target/etc/modules
        ;;
    esac
fi

# Load yenta_socket and ds from /etc/modules if it's a laptop
if grep -q yenta /proc/bus/pci/devices ; then 
  echo "yenta_socket" >> /target/etc/modules
  echo "ds" >> /target/etc/modules
fi

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

exit 0
