#!/bin/sh
#
# This script sets up all the installed packages after installation.

set -e

pkglibdir=/usr/lib/debian-edu-install

. $pkglibdir/debian-edu-common

load_config

#set -x

# Check if a deb package is installed.  Return true if it is, and
# false if it isn't.
deb_installed() {
    RET=$( dpkg -s $1 2>/dev/null | awk '/Status\:/ {print $4}' )
    if [ "$RET" = "installed" ] ; then
	true
    else
	false
    fi
}

#
# Configure hostname if it is the default name
#
if [ -x /usr/bin/update-hostname-from-ip ] ; then
    hostname=`hostname`
    if [ localhost = "$hostname" ] ; then
        /usr/bin/update-hostname-from-ip
    else
        print "info: Hostname is not 'localhost', but '$hostname', leaving it as it is."
    fi
else
    error "Unable to find /usr/bin/update-hostname-from-ip."
fi


# run cfengine with the required parameters
if deb_installed debian-edu-config ; then
   if /usr/bin/cfengine-debian-edu -v ; then
       :
   else
       error "Failed to run /usr/bin/cfengine-debian-edu."
   fi
fi

# Eject CD, as it is not needed anymore.  Ejecting it as soon as
# possible to make it available for installing other machines.  It has
# to be done after 75apt-get and cfengine run (LTSP and lessdisk
# is installed from cfengine).
eject || true

# Set default language.
#
# This must be done after cfengine edited the files, because the kdm
# cfengine rules expect to find comments in /etc/kde2/kdm/kdmrc, while
# update-locale-config removes the comments.
#
# It also have to execute before dexconf is executed, because it will
# use the xfree86 keyboard layout set by update-locale-config.
if [ -x /usr/sbin/update-locale-config -a ! -x /usr/lib/base-config/menu/localization-base-preinst ]; then
   if /usr/sbin/update-locale-config "$LANG" ; then
       :
   else
       error "failed to run update-locale-config (LANG='$LANG')."
   fi
else
   error "failed to find update-locale-config."
fi

# Run Xdebconfigurator to set values in the debconf-db.  Next it will
# run dexconf to write a new XF86Config-4 file.

xdebconf="/usr/sbin/xdebconfigurator"
dexconf="/usr/bin/dexconf"

if deb_installed xserver-xfree86 ; then
  if [ -x "$xdebconf" ]; then 
     # run xdebconfigurator

     # Pass language choice to xdebconfigurator
     export LANG

     # First we find out which server package to use
     pkg=`xdebconfigurator  2>&1 |grep 'DEBIAN PACKAGE: '|awk '{print $3}'`

     if [ xserver-xfree86 != "$pkg" ] ; then
       # Install it.  If it already is installed, nothing should happen.
       # We do not want to ask questions now

       # Don't install it yet.  xdebconfigurator is not able to
       # activate it, and it will loop calling mdetect over and over
       # if it fail to detect a working mouse. [pere 2002-08-02]

       # This problem is equivalent to Debian bug #134445 which was
       # fixed in xserver-xfree86 v4.1.0-16.

       #DEBIAN_FRONTEND=noninteractive apt-get install -y $pkg

       error "Unable to install the required XFree86 server '$pkg' automatically."
     fi

     # Then we configure
     if $xdebconf -dcik; then
	 if [ -x "$dexconf" ] ; then
             # run dexconf, and keep going even if it fails.

             # an alternative to running dexconf would be to run
             # "dpkg-reconfigure xserver-xfree86" but in the end package
             # xserver-xfree86 will still use dexconf for writing the file.

	     if $dexconf ; then
		 :
	     else
		 error "failed to run $dexconf."
	     fi
	 fi
     else
	 error "failed to run $xdebconf."
     fi
  fi
fi

# Set up apache with php4 etc
#if deb_installed apache && deb_installed php4 ; then
   # run apache-fix-config
#   print "info: Configuring apache."
#   if /usr/sbin/apache-fix-config ; then
#       :
#   else
#       error "failed to run apache-fix-config."
#   fi
#else
#   print "info: Not configuring apache as the apache or php4 package is missing."
#fi

# Set the symlink /bin/sh to make sure bash is not used as /bin/sh
# bash uses nssinfo to get login info
#  - Set symlink
#  - runs dpkg-divert --add /bin/sh
# se  /usr/share/doc/bash/README.Debian.gz for more info
# ash is replaced with dash, and the diversion is done in
# debian-edu-config instead (using cfengine)
#if [ -x /bin/ash ] ; then 
#  dpkg-divert --add /bin/sh
#  ln -sf ash /bin/sh
#fi

# Workaround for laptops, making sure PCMCIA network is started before
# services in need of a network (sysklogd, bind, nscd, autofs, exim,
# inetd, etc).  Not sure if 11 is a good number, but it is the same as
# hotplug is using, and both detect hw and might enable the
# network. [pere 2004-03-16]
if [ -e /etc/init.d/pcmcia ] ; then
  update-rc.d -f pcmcia remove
  update-rc.d -f pcmcia defaults 11 # The default value is 20 in woody
fi

exit 0
