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

LIVE_INSTALL=0
IMAGE_INSTALL=0

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

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

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

# Start swap on ZRAM service. Do this for LiveOS only, rather than enabling
# it on the target system, for the time being.
systemctl start zram.service

# 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 --graphical}"

# 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 | sed -r -e 's/ *release.*//' )
    export ANACONDA_PRODUCTVERSION=$( cat /etc/system-release | sed -r -e 's/^.* ([0-9\.]+).*$/\1/' )
fi

# set PRODUCTVARIANT if this is a Fedora Workstation live image
# FIXME really, livemedia-creator should include .buildstamp in live
# images, if it did, we could remove this:
# https://github.com/weldr/lorax/issues/350
if [ -f /etc/os-release ]; then
    variantid=$( grep VARIANT_ID /etc/os-release | tail -1 | cut -d= -f2)
    if [ "$variantid" = "workstation" ]; then
        export ANACONDA_PRODUCTVARIANT="Workstation Live"
    fi
fi

if [ $IMAGE_INSTALL = 1 ]; then
    export ANACONDA_PRODUCTVERSION=$(rpmquery -q --qf '%{VERSION}' anaconda | cut -d. -f1)
fi

export ANACONDA_BUGURL=${ANACONDA_BUGURL:="https://bugzilla.redhat.com/bugzilla/"}

RELEASE=$(rpm -q --qf '%{Release}' --whatprovides system-release)
if [ "${RELEASE:0:2}" = "0." ]; then
    export ANACONDA_ISFINAL="false"
else
    export ANACONDA_ISFINAL="true"
fi

export PATH=/sbin:/usr/sbin:$PATH

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

if [ -z "$(sestatus | grep enabled)" ]; then
    ANACONDA="$ANACONDA --noselinux"
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=}"
        ;;
    --updates)
        title="updates error"
        text="liveinst requires --updates=<url> instead of --updates <url>"
        if which zenity &> /dev/null; then
            zenity --error --no-markup --title="$title" --text="$text"
        else
            echo "$title" >&2
            echo "$text" >&2
        fi
        exit 1
        ;;
    ks=*|kickstart=*|--ks=*|--kickstart=*|inst.ks=*|--inst.ks=*|inst.kickstart=*|--inst-kickstart=*)
        title="Configuration not supported"
        text="Kickstart is not supported on live installs.  This installation will continue interactively."
        if which zenity &> /dev/null; then
            zenity --warning --title="$title" --text="$text"
         else
            echo "$title" >&2
            echo "$text" >&2
         fi
        ;;
    rescue|--rescue)
        title="Configuration not supported"
        text="Rescue mode is not supported on live media.  Please use the normal system tools to recover your system."
        if which zenity &> /dev/null; then
            zenity --warning --title="$title" --text="$text"
         else
            echo "$title" >&2
            echo "$text" >&2
         fi
         exit 1
        ;;
    esac
done

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

# Set up the updates, if provided.
if [ ! -z "$UPDATES" ]; then
    if [ -e /tmp/updates.img -o -e /tmp/updates ]; then
        title="Updates already exist"
        text="updates= was provided, but an updates image already exists.  Please remove /tmp/updates.img and /tmp/updates and try again."
        if which zenity &> /dev/null; then
            zenity --error --title="$title" --text="$text"
        else
            echo "$title" >&2
            echo "$text" >&2
        fi
        exit 1
    fi

    curl -o /tmp/updates.img $UPDATES

    # We officially support two updates.img formats:  a filesystem image, and
    # a compressed cpio blob.
    if [ ! -z "$(file /tmp/updates.img | grep 'gzip compressed data')" ]; then
        ( cd / ; gzip -dc /tmp/updates.img | cpio -idu )
    else
        mkdir /tmp/updates.disk
        mount -t auto /tmp/updates.img /tmp/updates.disk
        cp -Rt / /tmp/updates.disk/*
        umount /tmp/updates.disk
        rmdir /tmp/updates.disk
    fi

    export PYTHONPATH=/tmp/updates:$PYTHONPATH
    export LD_LIBRARY_PATH=/tmp/updates:$LD_LIBRARY_PATH
    export PATH=/tmp/updates:$PATH
fi

if [ -z $LC_ALL ]; then
    # LC_ALL not set, set it to $LANG to make Python's default encoding
    # detection work
    export LC_ALL=$LANG
fi

# Force the X11 backend since sudo and wayland do not mix
export GDK_BACKEND=x11

if [ -x /usr/bin/udisks ]; 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 if this was an image install
if [ $IMAGE_INSTALL = 1 ]; then
    anaconda-cleanup
fi

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

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