#!/bin/bash
# This shell helper sets values from shell-sourceable /etc/os-release
# and runs /usr/sbin/anaconda.bin which originally was /usr/sbin/anaconda.
# anaconda.bin is in Python, /etc/os-release cannot be sourced nicely there.

# Mostly copied from /usr/sbin/liveinst

if [ -f /etc/system-release ]; then
    ANACONDA_PRODUCTNAME="$(sed -r -e 's/ *release.*//' /etc/system-release)"
    export ANACONDA_PRODUCTNAME
    ANACONDA_PRODUCTVERSION="$(sed -r -e 's/^.* ([0-9\.]+).*$/\1/' /etc/system-release)"
    export ANACONDA_PRODUCTVERSION
fi

if [ ! -f /.buildstamp ] && ! grep -q inst.variant= /proc/cmdline && [ -f /etc/os-release ] ; then
    variantid="$(grep VARIANT_ID /etc/os-release | tail -1 | cut -d= -f2)"
    if [ -n "$variantid" ]; then
		export ANACONDA_PRODUCTVARIANT="$variantid"
	fi
fi

# Try to sort out where the live device is
LIVE_BLOCK="${LIVE_BLOCK:-}"
if [ -z "$LIVE_BLOCK" ]; then
    if [ -b "/dev/mapper/live-base" ]; then
       LIVE_BLOCK="/dev/mapper/live-base"
    elif [ -b "/dev/mapper/live-osimg-min" ]; then
       LIVE_BLOCK="/dev/mapper/live-osimg-min"
    elif [ -e "/run/rootfsbase" ]; then
        LIVE_BLOCK="$(findmnt -n -o SOURCE /run/rootfsbase)"
    fi
fi

# --liveinst arg broke kickstart installation, so we should
# check if inst.ks setted in cmdline before begin installation.
# It's don't broke installation from GUI, because desktop icon
# run 'liveinst' script. 
LIVE_ARGS=""
if ! grep -q 'inst.ks' /proc/cmdline ; then
    LIVE_ARGS="--liveinst"
    if [ -n "$LIVE_BLOCK" ]; then
        LIVE_ARGS="$LIVE_ARGS --method=livecd:${LIVE_BLOCK}"
    fi
fi

if [ ! -f /.buildstamp ] && ! grep -q inst.product= /proc/cmdline ; then
	LIVE_ARGS="$LIVE_ARGS --product=ROSA"
fi

#export ANACONDA_PRODUCTVERSION="$(rpm -q --qf '%{VERSION}' anaconda-core | cut -d. -f1)"
. /etc/os-release
export ANACONDA_PRODUCTVERSION="$VERSION_ID"
export ANACONDA_BUGURL="https://bugzilla.rosalinux.ru"

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

# Make look and feel the same in all DEs.
# This will also help to make screenshots in different manuals
# be completely the same as the real look in different DEs.
export GTK_THEME=Adwaita:light

# Our patches check if this variable is set (no matter which value it has)
# Removed by sed on non-certified distros
#export ANACONDA_IS_NICKEL=1
if grep -q 'inst.nickel=0' /proc/cmdline ; then
    unset ANACONDA_IS_NICKEL
fi
if grep -q 'inst.nickel=1' /proc/cmdline ; then
    export ANACONDA_IS_NICKEL=1
fi

eula_conf=/etc/anaconda/conf.d/10-eula.conf
if env | grep -q =ru_
	then lang=ru
	else lang=en
fi
eula_file=/usr/share/licenses/rosa-distro/license_"$lang".txt
if [ -f "$eula_file" ]; then
	echo "[License]" > "$eula_conf"
	echo "eula = $eula_file" >> "$eula_conf"
fi

exec anaconda.bin ${LIVE_ARGS} "$@"
