#! /bin/bash

# this script is called after the base.tgz is extract
# update packages (from base.tgz) to their newest version
# is almost Debian specific

echo "Updating base"

# first setup the environment in /target
if [ "$FAI_ACTION" = "install" -o "$FAI_ACTION" = "dirinstall" ]; then
    ctam
    if [ -f /etc/init.d/udev ]; then
      mkdir -p $target/run/udev
      mount --bind /run/udev $target/run/udev
    fi

    # if libc is upgraded init u is called in chroot environment and
    # then init will eat up much cpu time
    # fake some more programs
    [ -L $target/sbin/init ] || fai-divert -a /sbin/init
    fai-divert -a /usr/sbin/policy-rc.d
    # 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

export aptopt="-y -o Dpkg::Options::=--force-confnew"
$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
$ROOTCMD apt-get $aptopt dist-upgrade < /dev/null
$ROOTCMD apt-get clean
