#! /bin/bash

if [ "$FAI_ACTION" = "install" -o "$FAI_ACTION" = "dirinstall" ]; then
    # some packages must access /proc even in chroot environment
    mount -t proc proc $FAI_ROOT/proc
    # some packages must access /sys even in chroot environment
    mount -t sysfs sysfs $FAI_ROOT/sys
    # if we are using udev, also mount it into $FAI_ROOT
    [ -f /etc/init.d/udev ] && mount --bind /dev $FAI_ROOT/dev
    mount -t devpts devpts $FAI_ROOT/dev/pts
    # if libc is upgraded init u is called in chroot environment and
    # then init will eat up much cpu time
    fai-divert -a /sbin/init /usr/sbin/liloconfig /usr/sbin/invoke-rc.d
    # fake some more programs
    fai-divert -a /sbin/start-stop-daemon /usr/sbin/policy-rc.d
    cp /sbin/fai-start-stop-daemon $FAI_ROOT/sbin/start-stop-daemon
    # never start any daemons inside chroot during installtion
    cat > $FAI_ROOT/usr/sbin/policy-rc.d <<EOF
#!/bin/sh
exit 101
EOF
    chmod a+rx $FAI_ROOT/usr/sbin/policy-rc.d
fi

# update the apt-get information inside the nfsroot 

export aptopt='-y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"'
$ROOTCMD apt-get $aptopt update
$ROOTCMD apt-get $aptopt check
[ $? -ne 0 ] && $ROOTCMD apt-get $aptopt -f install </dev/null 
$ROOTCMD dpkg -C
[ $? -ne 0 ] && yes '' | $ROOTCMD dpkg --configure -a 
# using the above value, causes an error: "dpkg need action"
export aptopt=
# this may be removed in lenny
if $ROOTCMD aptitude --help | grep -q safe-upgrade; then
    $ROOTCMD aptitude -y safe-upgrade
else
    $ROOTCMD aptitude -y upgrade
fi

$ROOTCMD aptitude $aptopt -f -y dist-upgrade </dev/null
# update dpkg info which packages are available
tmp=$($ROOTCMD mktemp) || exit 88
$ROOTCMD apt-cache dumpavail > $FAI_ROOT/$tmp
$ROOTCMD dpkg --update-avail $tmp
rm -f $FAI_ROOT/$tmp
