#!/bin/bash
#
# 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/>.
#

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

LIVE_INSTALL=0
IMAGE_INSTALL=0
RESCUE=0
if [[ "$LIVECMD $*" =~ "--rescue" ]]; then
    RESCUE=1
fi

if [ -z "$LIVECMD" ]; then
    LIVE_INSTALL=1
fi

if [[ "$LIVECMD $*" =~ "--image" ]]; then
    IMAGE_INSTALL=1
fi

if [[ "$LIVECMD $*" =~ "--liveinst" ]]; then
    LIVE_INSTALL=1
fi

if [[ "$LIVECMD $*" =~ "-T" ]] || [[ "$LIVECMD $*" =~ "--text" ]]; then
    TEXT_INSTALL=1
fi

export PIXMAPPATH="/usr/share/anaconda/pixmaps"

# create source dirs
if [ ! -e /run/install/source ]; then
    mkdir -p /run/install
    ln -sf /mnt/livecd /run/install/source
fi
if [ ! -e /mnt/install/image/LiveOS ]; then
    mkdir -p /mnt/install/image/LiveOS
    ln -sf /mnt/cdrom/livecd.squashfs /mnt/install/image/LiveOS/livecd.img
fi
if [ ! -e /run/install/isodir ]; then
    mkdir -p /run/install
    ln -sf /mnt/cdrom /run/install/isodir
fi

# 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

# Allow running another command in the place of anaconda, but in this same
# environment.  This allows storage testing to make use of all the module
# loading and lvm control in this file, too.
ANACONDA=${LIVECMD:=anaconda --liveinst --noselinux --method=hd://:///run/install/source $INSTLANG}

# load modules that would get loaded by the initramfs (#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 iscsi_tcp iscsi_ibft; do /sbin/modprobe $i 2>/dev/null ; done

if [ -f /etc/system-release ]; then
    export ANACONDA_PRODUCTNAME=$( cat /etc/system-release | cut -d" " -f 1 )
    export ANACONDA_PRODUCTVERSION=$( cat /etc/system-release | sed -r -e 's/^.* ([0-9\.]+).*$/\1/' )
fi
export ANACONDA_BUGURL=${ANACONDA_BUGURL:="https://bugs.sabayon.org/"}

export ANACONDA_ISFINAL="true"
export PATH=/sbin:/usr/sbin:$PATH
export PYTHONPATH=/usr/share/anaconda/site-python

if [ -x /usr/sbin/getenforce ]; then
    current=$(/usr/sbin/getenforce)
    /usr/sbin/setenforce 0 > /dev/null
fi

# Process cmdline args
for opt in `cat /proc/cmdline` $*; do
    case $opt in
    xdriver=*)
        ANACONDA="$ANACONDA --$opt"
        ;;
    updates=*)
        UPDATES="${opt#updates=}"
        ;;
    --updates=*)
        UPDATES="${opt#--updates=}"
        ;;
    inst.updates=*)
        UPDATES="${opt#inst.updates=}"
        ;;
    --inst.updates=*)
        UPDATES="${opt#--inst.updates=}"
        ;;
    esac
done

if [ -z "$DISPLAY" ]; then
    ANACONDA="$ANACONDA -T"
fi

# unmount anything that shouldn't be mounted prior to install
anaconda-cleanup $ANACONDA $*

# Prevents breakage if the hostname is changed during the install
xhost +si:localuser:root > /dev/null

if [ -x /usr/bin/udisks -a -n "$DISPLAY" -a -z "$SABAYON_DEBUG" -a -z "$TEXT_INSTALL" ]; then
    /usr/bin/udisks --inhibit -- $ANACONDA $*
else
    $ANACONDA $*
fi

if [ -e /tmp/updates ]; then rm -r /tmp/updates; fi
if [ -e /tmp/updates.img ]; then rm /tmp/updates.img; fi

# try to teardown the filesystems
anaconda-cleanup

rm -f /dev/.in_sysinit 2>/dev/null

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