#!/bin/sh
#
# Simple script to kick off an install from a live CD
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# 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, see <http://www.gnu.org/licenses/>.
#

# load modules that would get loaded by the loader... (#230945)
for i in raid0 raid1 raid5 raid6 raid456 raid10 dm-mod dm-zero dm-mirror dm-snapshot dm-multipath dm-round-robin vfat dm-crypt cbc sha256 lrw xts ; do /sbin/modprobe $i 2>/dev/null ; done

# lxnay: load UEFI efivars (so that Anaconda can detect EFI systems)
efi_disabled=$(cat /proc/cmdline | grep noefi)
if [ -z "${efi_disabled}" ]; then
    modprobe efivars 2>/dev/null
fi

# NOTE (switches):
# --text => ncurses based installer
# --cmdline => cmdline based installer
# <no arguments> => autodetection

for arg in $*; do
    if [ "${arg}" = "--text" ]; then
        DO_TEXT="1"
        TEXT_OPT_PASSED="1"
    fi
done
[[ -z "${DISPLAY}" ]] && DO_TEXT="1"

export ANACONDA_PRODUCTNAME="Sabayon"
export ANACONDA_PRODUCTVERSION="$(cat /etc/sabayon-release | cut -d' ' -f 4)"
export ANACONDA_PRODUCTPATH="/mnt/livecd"
export ANACONDA_CHROOTPATH="/mnt/sysimage"
export ANACONDA_PRODUCTARCH="$(cat /etc/sabayon-release | cut -d' ' -f 3)"
export ANACONDA_BUGURL="http://bugs.sabayon.org"
export PIXMAPPATH="/usr/share/anaconda/pixmaps"

function kill_mounts() {
    # devkit-disks is now mounting lots of stuff.  for now, let's just try to unmount it all
    umount /media/* &> /dev/null
    if [ -d "${ANACONDA_CHROOTPATH}/dev" ]; then
        tmpdir=$(mktemp)
        rm -f $tmpdir
        mkdir $tmpdir
        mount --move ${ANACONDA_CHROOTPATH}/dev $tmpdir &> /dev/null
        umount -l $tmpdir &> /dev/null
    fi
    tac /proc/mounts | grep ^/dev | grep -v live | while read dev mntpoint rest; do
       umount $mntpoint 2>/dev/null
    done

    /sbin/swapoff -a
    sleep 2
    /sbin/lvm vgchange -an --ignorelockingfailure
    for i in /dev/md*; do
        if [ ! -b $i ]; then
            continue
        fi

        case "$i" in
            /dev/md*p*)
                ;;
            *)
                mdadm --stop $i >/dev/null 2>&1
                ;;
        esac
    done

    # close any luks device
    cryptsetup luksClose /dev/mapper/luks-* &> /dev/null

}

function setup_vbox() {
	if [ -x /usr/bin/VBoxClient-all ]; then
		/usr/bin/VBoxClient-all &> /dev/null
	fi
}

if [ -n "$DISPLAY" -a -n "$LANG" ]; then
    INSTLANG="--lang $LANG"
fi

if [ -x /usr/sbin/setenforce -a -e /selinux/enforce ]; then
    current=$(cat /selinux/enforce)
    /usr/sbin/setenforce 0
fi

ANACONDA="anaconda --liveinst --method=hd://://${ANACONDA_PRODUCTPATH}"
[[ -n "${DO_TEXT}" ]] && [[ -z "${TEXT_OPT_PASSED}" ]] && \
    ANACONDA="${ANACONDA} --text"
[[ ! -e /selinux/load ]] && ANACONDA="$ANACONDA --noselinux"

kill_mounts
setup_vbox

/sbin/udevadm control --env=ANACONDA=1

if [ -x /usr/bin/udisks ] && [ -z "${SABAYON_DEBUG}" ]; then
    /usr/bin/udisks --inhibit -- $ANACONDA $*
else
    $ANACONDA $*
fi

ANACONDA_RC="$?"

if [ -n "$current" ]; then
    /usr/sbin/setenforce $current
fi

kill_mounts

[[ -n "${DO_TEXT}" ]] && reset

if [ "${ANACONDA_RC}" = "100" ] || [ -f "/tmp/__anaconda_reboot__" ]; then
    # reboot
    reboot -f
fi
