#!/bin/bash
#
# Entry point for configuring an oVirt node when running in standalone mode.


. /usr/libexec/ovirt-functions

# symlinked scripts for menu options, link name is menu label
CONFIG_DIR=/etc/ovirt-config-setup.d

# special options, all others execute the symlinked script in CONFIG_DIR
DEBUG_SHELL="Shell"

declare -a OPTIONS

can_include_item() {
    local item=$(readlink -f "$1")
    if is_booted_from_local_disk; then
        if [ $item = "/usr/libexec/ovirt-config-storage" \
          -o $item = "/usr/libexec/ovirt-config-uninstall" \
          -o $item = "/usr/libexec/ovirt-config-boot-wrapper" ]; then
            return 1
        fi
    fi
    return 0
}

is_during_startup() {
    local result=1
    for tty in $(ps -ef | awk '{ printf $2"::"$6" " }'); do
        if [[ "$tty" =~ "$$::" ]]; then
            len=${#$}
            the_tty=${tty:len+2}
            if [ "$the_tty" == "?" ]; then
                # If we're in rescue mode then do not return success
                if [[ -n "$(grep rescue /proc/cmdline 2>/dev/null)" ]]; then
                    result=1
                else
                    result=0
                fi
                break
            fi
        fi
    done

    return $result
}

support_menu() {
    local shell="Shell"
    local continue="Continue boot"
    local uninstall="Uninstall an existing Hypervisor"
    local exit="Exit support menu"

    declare -a OPTIONS
    OPTIONS[${#OPTIONS[*]}]="${shell}"
    if is_during_startup; then
        OPTIONS[${#OPTIONS[*]}]="${continue}"
    fi
    if ! is_booted_from_local_disk; then
        OPTIONS[${#OPTIONS[*]}]="${uninstall}"
    fi
    OPTIONS[${#OPTIONS[*]}]="${exit}"

    PS3="Choose an option: "
    printf "\n\n Support Menu\n\n" >&2
    select OPTION in "${OPTIONS[@]}"
      do
      log_only "Selected: $OPTION"
      case "$OPTION" in
          "${uninstall}") clear; /usr/libexec/ovirt-config-uninstall; return 1 ;;
        "${continue}") exit 0 ;;
    "${shell}") clear; bash; return 1 ;;
            "${exit}") return 0 ;;
        esac
    done
}

for cfg in $CONFIG_DIR/*; do
    label=$(basename "$cfg")
    if can_include_item "$cfg" ; then
        # Assume label is actually XX_Some Text. So strip of the first 3 characters
        label=${label:3}
        OPTIONS[${#OPTIONS[*]}]="$label"
    fi
done

EXIT_SETUP="Exit Hypervisor Configuration Menu"
SUPPORT="Support Menu"

if is_during_startup; then
    OPTIONS[${#OPTIONS[*]}]="$SUPPORT"
else
    OPTIONS[${#OPTIONS[*]}]="$EXIT_SETUP"
fi

# serial console sets echoprt by default which looks ugly
stty -echoprt
# reset tty, otherwise serial console is broken
reset > /dev/null
clear

PS3="Choose an option to configure: "
while true ; do
        grep -v ^Kernel /etc/issue
        printf "\n\n Hypervisor Configuration Menu\n\n" >&2

        select OPTION in "${OPTIONS[@]}"
        do
            log_only "Selected: $OPTION"
            case "$OPTION" in
                "$SUPPORT")
                    if ask_yes_or_no "This menu is for troubleshooting with support representatives. Do not use these options without guidance from support. Enter the support menu ([Y]es or [N]o)?"; then
                        while true; do
                            if support_menu; then
                                break
                            fi
                        done
                    fi
                    continue 2
                    ;;
                "") break ;;
                "$EXIT_SETUP") exit;;
                *)
                    {
                        printf "\n\n"
                        $CONFIG_DIR/*"$OPTION"
                        rc=$?
                        case $rc in
                            0) printf "\n";;
                            99) printf "\n$OPTION ABORTED.\n\n";;
                            *) printf "\nERROR: $OPTION FAILED. "
                                printf "See $OVIRT_LOGFILE\n\n"
                                ;;
                        esac
                    } 2>&1 | tee -a $OVIRT_TMP_LOGFILE;
                    if [ -f $OVIRT_TMP_LOGFILE ]; then
                        cat $OVIRT_TMP_LOGFILE >> $OVIRT_LOGFILE
                        rm -f $OVIRT_TMP_LOGFILE
                    fi
                    break ;;
            esac
        done
done
