#!/bin/bash

UpdateDatabase() { pkexec pacman -Fy >/dev/null ; }

CheckDatabaseUpdate() {

    if ! /usr/bin/pacman -Fl eos-bash-shared >&/dev/null ; then
        # need to run pacman -Fy

        local txt=""
        local cmd

        txt+="\nPacman database needs updating using command:\n"
        txt+="<tt>  pacman -Fy</tt>\n"
        cmd=(
            eos_yad --form --title="Database update needed" --text="$txt" --image=dialog-information
            --button=yad-quit:$exit_quit
            --button="Update database now!system-run!":$exit_updatedb
        )
        "${cmd[@]}"
        case "$?" in
            $exit_updatedb)   UpdateDatabase ;;
            $exit_quit | *)   exit ;;
        esac
    fi
}

AdjustButtonName() {
    # may modify '$app_button_name' because of yad quirks

    case "$app_button_name" in
        su-c_wrapper) app_button_name="su-c__wrapper" ;;   # handle underscore character for yad !!
    esac
}

HelpExists() {
    local -r app="$1"
    [ -d "/etc/eos-apps-info/pageinfo/$app" ]
}

IsBonusApp() {
    local -r app="$1"
    printf "%s\n" "${bonus_apps[@]}" | grep "$app" >/dev/null
}

AddAppButton() {
    local -r app="$1"              # e.g. su-c_wrapper  (= real app name)
    local app_button_name="$app"   # e.g. su-c__wrapper (= app name changed for yad in RenameApp)
    local is_bonus=no

    if [ -x "/usr/bin/$app" ] || HelpExists "$app" || IsBonusApp "$app" ; then
        AdjustButtonName
        cmd+=(--field="$app_button_name!!$app":fbtn "eos-apps-info-helper '$app'")   # Add app buttons to the command.
    else
        echo "$progname: help page for '$app' not found." >> /tmp/$progname.log
    fi
}

EosApps2cmd() {
    local app

    for app in "${apps[@]}" ; do
        AddAppButton "$app"
    done
}

BonusApps2cmd() {
    local -r blabel="Bonus items"
    local app

    cmd+=(--field="<b><i>$blabel:</i></b>":LBL "")                              # visually separates bonus apps

    for app in "${bonus_apps[@]}" ; do
        AddAppButton "$app"
    done
}

Main()
{
    local progname=${0##*/}
    # local -r bonus_apps=(downgrade grub inxi linux linux-lts nvidia paru rate-mirrors virtualbox worm yad yay)
    local -r apps=(
        akm ChangeDisplayResolution
        # checkupdatesext
        create-ml
        curl-exit-code-to-string
        eos-apps-info eos-connection-checker
        eos-downgrade eos-download-wallpapers
        eos-log-tool
        eos-manual-intervention
        eos-pacdiff eos-packagelist eos-quickstart eos-rankmirrors eos-sendlog eos-shifttime
        eos-update
        # eos-update-notifier eos-update-notifier-configure
        eos-wallpaper-set eos-welcome grub-colour-chooser
        # mirrorlist-rank-info
        # nvidia-inst
        # nvidia-driver-supported-branches
        paccache-service-manager
        pahis rami reflector-simple RunInTerminal show-location-info su-c_wrapper
    )
    local arg
    for arg in "$@" ; do
        case "$arg" in
            --show-apps) echo "${apps[*]}"; exit ;;
        esac
    done

    source /usr/share/endeavouros/scripts/eos-script-lib-yad || return 1
    eos_assert_deps $progname yad || return 1
    local -r bonus_apps=($(eos-apps-info-helper --show-bonus))

    # yad command exit codes (don't use even numbers)
    local -r exit_quit=11
    local -r exit_updatedb=13
    local -r update_label="Update database"

    CheckDatabaseUpdate

    local txt=""
    txt+="Click the buttons below to see information about the apps in the EndeavourOS repository.\n"
    txt+="If needed, scroll to see all buttons.\n"
    txt+="Note: you may need to click the <b>$update_label</b> button below to update your package database.\n\n"

    local cmd=(
        eos_yad --form --title="EndeavourOS apps info" --text="$txt"
        --columns=4 --align-buttons --image=dialog-information --scroll --geometry=800x600
        --button="$update_label!!Updates package database and restarts this app":$exit_updatedb
        --buttons-layout=center
    )

    EosApps2cmd
    BonusApps2cmd

    while true ; do
        "${cmd[@]}"
        case "$?" in
            $exit_updatedb) UpdateDatabase ;;
            *) break ;;
        esac
    done
}

Main "$@"
