#!/bin/bash

PROBLEM() {
    local msg="$1"
    local problem=(
        eos_yad --form --image=$ICO_ERROR
        --title="ARM installer problem"
        --text="$progname: $msg"
        --button=yad-quit:1
    )
    "${problem[@]}"
}

DIE() {
    PROBLEM "$1"
    exit 1
}

SelectTerminal() {
    local xx
    for xx in "${terminals_supported[@]}" ; do
        if which "$xx" &> /dev/null ; then
            echo "$xx"
            return 0
        fi
    done
    return 1
}

ARM_installer_in_terminal() {
    local urls=(
        "$ARM_IMAGES_GIT_PAGE"
    )
    local url ix count=${#urls[@]}
    local errcode
    local msg
    local targetdir=/tmp/arm-install

    rm -rf $targetdir

    for ((ix=0; ix < $count; ix++)) ; do
        url="${urls[$ix]}"
        git clone $url $targetdir 2>/dev/null
        errcode=$?
        if [ $errcode -eq 0 ] ; then
            #if [ "$Manuels_internal_testing" = "yes" ] ; then
            #    PROBLEM "$FUNCNAME called for testing only."
            #    return
            #fi
            cd $targetdir
            chmod +x image-install-calamares.sh                                 # sudo not needed here?
            $terminal -e "sudo ./image-install-calamares.sh"
            return
        else
            msg="ARM installer fetch failed (git code $errcode) from\n<tt>$url</tt>\n\n"
            if [ $ix -lt $((count - 1)) ] ; then
                PROBLEM "${msg}Next URL to try:\n<tt>${urls[$((ix+1))]}</tt>\n"
            else
                PROBLEM "${msg}Bailing out.\n"
                return
            fi
        fi
    done
}
export -f ARM_installer_in_terminal

InfoPage() {
    local browser="firefox"
    [ -x /usr/bin/$browser ] || browser="kde-open"
    [ -x /usr/bin/$browser ] || browser="xdg-open"
    $browser "$ARM_INFO_PAGE"
}
export -f InfoPage

Main() {
    local progname=${0##*/}

    # common configs
    local file=/usr/share/endeavouros/scripts/eos-script-lib-yad
    source $file || DIE "file $file not found"
    AssignIconVariables_in_eos_bash_shared

    # ARM configs
    file=/etc/welcome-installer.conf
    source $file               || DIE "file $file not found"
    [ "$ARM_INFO_PAGE" ]       || DIE "variable ARM_INFO_PAGE not set in $file"
    [ "$ARM_IMAGES_GIT_PAGE" ] || DIE "variable ARM_IMAGES_GIT_PAGE not properly set in $file"

    local terminals_supported=(konsole xfce4-terminal)
    local terminal="$(SelectTerminal)"
    [ -n "$terminal" ] || DIE "no terminal found"
    case "$terminal" in
        konsole)          terminal+=" --fullscreen" ;;
        xfce4-terminal)   terminal+=" --maximize" ;;
    esac

    local icon=$ICO_CALAMARES
    local title="EndeavourOS ARM image installer"
    local text=""
    text+="Starting to install the EndeavourOS ARM image\n"
    text+="to your external storage device.\n"
    text+="Please follow the instructions in the terminal window.\n"
    text+="\n"
    text+="Before starting the ARM Installer, ensure you have a storage device\n"
    text+="plugged into a USB port to receive the image.\n"
    text+="Note that the download size of the image is about 1.5 GB.\n"
    text+="Click <b>More Info</b> for further details.\n"

    local cmd=(
        eos_yad
        --form --align-buttons --use-interp --image=$icon
        --title="$title"
        --text="$text"
        --button="Start ARM Installer!$icon!Install EndeavourOS ARM image":5
        --button='More Info!user-info!More details on the wiki page':"InfoPage"
        --button=yad-cancel:1
    )
    "${cmd[@]}"
    case "$?" in
        5) ARM_installer_in_terminal ;;
    esac
    return 0
}

Main "$@"
