#!/bin/sh
#
#       <99ubuntu>
#
#       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 Ubuntu Recovery 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

#Force ubiquity to run in automatic regardless if there are ubiquity options in /proc/cmdline (except single user mode)
if ! grep -q "single" /proc/cmdline 2>&1 >/dev/null; then
    sed -i "s/ubiquity=\$/ubiquity=1/; s/\$automatic\ \$choose/--automatic/" /root/etc/init/ubiquity.conf
fi
#if they use a ubiquity icon it needs to run in automatic
if [ -f /root/etc/init.d/casper ]; then
    sed -i "s/prompt=1$/prompt=/;" /root/etc/init.d/casper
fi
sed -i "s/Exec=ubiquity/Exec=ubiquity --automatic/" /root/usr/share/applications/ubiquity-gtkui.desktop 2>/dev/null || true

#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

#install if not installed, otherwise this will upgrade
chroot /root apt-get install ubuntu-recovery -y --no-install-recommends
#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
elif [ -e /root/isodevice/scripts/emergency.sh ]; then
    . /root/isodevice/scripts/emergency.sh
fi
if [ -d /root/cdrom/scripts/emergency-scripts ]; then
    for script in /root/cdrom/scripts/emergency-scripts/[0-9]*; do
        . $script
    done
elif [ -d /root/isodevice/scripts/emergency-scripts ]; then
    for script in /root/isodevice/scripts/emergency-scripts/[0-9]*; do
        . $script
    done
fi

###Set up all preseeds###
# First test for and load override / configurations preseed
# - needs to be loaded first so that we know if we are dual boot
if [ -e /root/cdrom/preseed/ubuntu-recovery.cfg ]; then
    casper-set-selections "/root/cdrom/preseed/ubuntu-recovery.cfg"
fi

# Now load all the defaults included in all installs
casper-set-selections "/root/usr/share/ubuntu/casper/seeds/ubuntu.cfg"

# If we have a dual boot option, load the dual boot preseed
if db_get ubuntu-recovery/dual_boot && [ "$RET" = true ] ; then
    casper-set-selections "/root/usr/share/ubuntu/casper/seeds/dual.cfg"
fi

# Lastly, reload the override / configurations preseed so that it is allowed to override stuff from ubuntu.cfg and dual.cfg
if [ -e /root/cdrom/preseed/ubuntu-recovery.cfg ]; then
    casper-set-selections "/root/cdrom/preseed/ubuntu-recovery.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/ubuntu-recovery-initramfs-hook ] ; then
    /root/usr/share/ubuntu/ubuntu-recovery-initramfs-hook
fi

log_end_msg

exit 0
