#!/bin/sh
#
#       <SUCCESS-SCRIPT>
#
#       Called in the event that postinstall (chroot) steps complete successfully
#
#       Copyright 2008-2010 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.

# BE AWARE THAT THIS SCRIPT IS CALLED FROM OUTSIDE THE CHROOT.
# The OS is mounted under $TARGET

. /usr/share/ubuntu/scripts/fifuncs "target"

set -x
set -e

IFHALT "(SUCCESS) Clean-Up .."

#Fixup APT how we want it for a successful oem-config run
rm -f $TARGET/var/lib/apt/lists/*Packages
rm -f $TARGET/etc/apt/sources.list.d/ubuntu.list

#https://bugs.launchpad.net/ubuntu/+source/choose-mirror/+bug/550694
sed -i "s/http:\/\/.*.archive.ubuntu.com/http:\/\/archive.ubuntu.com/" $TARGET/etc/apt/sources.list

if [ "$BOOTDEV" = "$TARGETDEV" ]; then
    if [ -d "/isodevice" ]; then
        RP="/isodevice"
    else
        RP="/cdrom"
    fi
    mount -o remount,rw $RP
    IFHALT "Resetting locale state"
    [ -s $RP/preseed/ubuntu-recovery.cfg ] && sed -i "/debian-installer\/locale/d" $RP/preseed/ubuntu-recovery.cfg 

    IFHALT "Resetting grub fail state"
    [ -s $RP/boot/grub/grubenv ] && rm -f $RP/boot/grub/grubenv
    mkdir -p $RP/boot/grub

    if grub-editenv $RP/boot/grub/grubenv list | grep recordfail; then
        grub-editenv $RP/boot/grub/grubenv unset recordfail
    fi

    if ! grub-editenv $RP/boot/grub/grubenv list | grep rectype; then
        grub-editenv $RP/boot/grub/grubenv set rectype=hdd
    fi

    # Set 'ondiskrec' flag to avoid red grub menu of on-disk recovery. (LP: #1054250)
    if ! grub-editenv $RP/boot/grub/grubenv list | grep ondiskrec; then
        grub-editenv $RP/boot/grub/grubenv set ondiskrec=1
    fi

    # Set bootable flag on legacy parition
    [ ! -d /sys/firmware/efi ] && /dev/shm/set_bootable

    IFHALT "Clean up FI Env..."
    rm -rf $RP/scripts/chroot-scripts/os-post/95-set_UTC_TZ.sh
    rm -rf $RP/misc/dell-unsupported.py
    rm -rf $RP/misc/run-tz-fix
    rm -rf $RP/misc/tz_offset.py
    rm -rf $RP/misc/drmk.zip
    rm -rf $RP/command.com
    rm -rf $RP/autoexec.bat
    rm -rf $RP/dellbio.bin
    rm -rf $RP/dellrmk.bin
    rm -rf $RP/linld.com
    rm -rf $RP/cmd.cfg
    rm -rf $RP/up.zip
    rm -rf $RP/up.tgz
    rm -rf $RP/upimg.bin
    rm -rf $RP/upimg.gz
    rm -rf $RP/SDR
    rm -rf $RP/*.SDR
    rm -rf $RP/srv
    rm -rf $RP/factory

    #If we have another script to run, do it
    if [ -f $RP/scripts/chroot-scripts/SUCCESS-SCRIPT ]; then
        $RP/scripts/chroot-scripts/SUCCESS-SCRIPT
        rm -f $RP/scripts/chroot-scripts/SUCCESS-SCRIPT
        rm -f $RP/scripts/chroot-scripts/FAIL-SCRIPT
        IFHALT "(SUCCESS) Report to logs.. Done!"
    fi

fi

# Call ubuntu-recovery-command for running general tasks
if [ -e /usr/share/ubuntu/scripts/ubuntu-recovery-command ]; then
    /usr/share/ubuntu/scripts/ubuntu-recovery-command ubiquity success
fi

IFHALT "(SUCCESS) Clean-Up .. Done!"
