#!/bin/sh
#
#       <oem-preload-bootstrap>
#
#       Casper initramfs plugin.
#        - prepare on-media pool
#        - loads the ubiquity ubuntu bootstrap plugin into place
#        - ensures that it will run
#
#       Copyright 2008-2011 Dell Inc.
#           Mario Limonciello <Mario_Limonciello@Dell.com>
#           Hatim Amro <Hatim_Amro@Dell.com>
#           Michael E Brown <Michael_E_Brown@Dell.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
# vim:ts=8:sw=8:et:tw=0

PREREQ=""
DESCRIPTION="Running OEM Preload Bootstrap..."

prereqs ()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

. /scripts/casper-functions
load_confmodule

log_begin_msg "$DESCRIPTION"

export DEBIAN_HAS_FRONTEND=
export DEBCONF_REDIR=
export DEBIAN_FRONTEND=noninteractive

#Build custom pool (static and dynamic)
if [ ! -x /root/usr/share/ubuntu/scripts/pool.sh ]; then
    mkdir -p /root/usr/share/ubuntu/scripts/
    cp /scripts/pool.sh /root/usr/share/ubuntu/scripts/
fi
chroot /root /usr/share/ubuntu/scripts/pool.sh

#upgrade the existing packages.
chroot /root apt-get dist-upgrade --yes --force-yes --allow-unauthenticated

#If in factory process, we might have backed up an MBR to restore
#to the first hard drive
if [ -f /root/cdrom/factory/mbr.bin ];  then
    dd if=/root/cdrom/factory/mbr.bin of=/dev/sda
fi

#Emergency installer fixes
if [ -e /root/cdrom/scripts/emergency.sh ]; then
    . /root/cdrom/scripts/emergency.sh
fi

###Set up all preseeds###

# Now load all the defaults included in all installs
if [ -e /root/usr/share/ubuntu/casper/seeds/ubuntu.cfg ]; then
    casper-set-selections "/root/usr/share/ubuntu/casper/seeds/ubuntu.cfg"
fi

# Customized preseed by project for specific purpose.
if [ -e /root/cdrom/preseed/project.cfg ]; then
    casper-set-selections "/root/cdrom/preseed/project.cfg"
fi

# Clear out debconf database backup files to save memory.
rm -f /root/var/cache/debconf/*.dat-old

# Add a customizable hook for projects. (LP: #1200465)
if [ -e /root/usr/share/ubuntu/casper/initramfs-hook ] ; then
    /root/usr/share/ubuntu/casper/initramfs-hook
fi

log_end_msg

exit 0
