#!/bin/bash

# =======================
# Define Colors
RED='\e[41m'
NC='\e[0m' # No color
BG='\e[7m' # Highlighting Background color
TC='\e[1m' # Highlighting Text color

# =======================
# Service messages section
# Wrong Option
ERRORMSG="$RED Wrong option $NC"
TRYAGAINMSG=" Please try again..."

# =======================
# Pacman installation
INSTALLPKGS="sudo pacman -Syu"

# =======================
# mhwd-ui functions
cd || exit
trap 'rm $HOME/{.running_kernel,.installed_kernels,.available_kernels}' EXIT
in_array() {
    local haystack=${1}[@]
    local needle=${2}
    for i in ${!haystack}; do
        if [[ ${i} == ${needle} ]]; then
            return 0
        fi
    done
    return 1
}

function install_kernel() {
    PS3="which kernel do you wish to install? (press ctrl+d to preceed, ctrl+c to cancel): "
    # Gather the results in an array.
    kernels=($(grep -v -f .installed_kernels .available_kernels) "proceed" "cancel")

    select pick in "${kernels[@]}"; do
        case $pick in
        proceed)
            break
            ;;
        cancel)
            exit
            ;;
        *)
            if in_array picks $pick; then
                picks=($(printf -- '%s\n' "${picks[@]}" | grep -v $pick))
            else
                picks+=("$pick")
            fi
            printf 'Selected: %s\n' "${picks[*]}"
            ;;
        esac
    done

    if [[ $picks ]]; then
        printf 'Installing %s\n' "${picks[*]}"
        sudo mhwd-kernel -i "${picks[@]}"
    fi
}

function remove_kernel() {
    PS3="which kernel do you wish to remove? (press ctrl+d to preceed, ctrl+c to cancel): "
    # Gather the results in an array.
    kernels=($(grep -v -f .running_kernel .installed_kernels) "proceed" "cancel")

    select pick in "${kernels[@]}"; do
        case $pick in
        proceed)
            break
            ;;
        cancel)
            exit
            ;;
        *)
            if in_array picks $pick; then
                picks=($(printf -- '%s\n' "${picks[@]}" | grep -v $pick))
            else
                picks+=("$pick")
            fi
            printf 'Selected: %s\n' "${picks[*]}"
            ;;
        esac
    done

    if [[ $picks ]]; then
        printf 'Removing %s\n' "${picks[*]}"
        sudo mhwd-kernel -r "${picks[@]}"
    fi
}

mhwd-kernel -li | awk 'NR==1 {print $4}' | tr -d '()' >.running_kernel &
mhwd-kernel -li | awk 'NR > 2 { print $2}' >.installed_kernels &
mhwd-kernel -l | awk 'NR > 1 { print $2}' >.available_kernels &

# Run infinite loop for UI / menu, till the user quits using the "quit" option or CTRL+C.
while true; do

    # Define Menu entries which will be run with the echo command
    E0="\e[1mQ\e[0muit"
    E1="\e[1mI\e[0mnstall a Kernel"
    E2="\e[1mR\e[0memove a Kernel"
    E3="Use \e[1mF\e[0mree Graphics"
    E4="Use \e[1mN\e[0monfree Graphics"
    E5="\e[1mL\e[0mist Installed Drivers"
    E6="\e[1mC\e[0monfigure Touchpad"
    E7="\e[1mS\e[0mystem Temperatures"
    E8="\e[1mD\e[0misk Usage"
    E9="\e[1mH\e[0mome Folder Usage"
    E10="\e[1mB\e[0mios Information"
    E11="P\e[1mo\e[0mwer Consumption"
    E12="\e[1mP\e[0mrinters"

    printf '\33[H\33[2J' # Use this instead of the clear command

    echo
    echo -e "                       $BG$TC Hardware and Drivers $NC"
    echo -e " $TC+----------------------------------------------------------------+$NC"
    echo -e " $TC|$NC    $BG 1 $NC $E1        $TC|$NC    $BG 2 $NC $E2        $TC|$NC"
    echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
    echo -e " $TC|$NC    $BG 3 $NC $E3       $TC|$NC    $BG 4 $NC $E4   $TC|$NC"
    echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
    echo -e " $TC|$NC    $BG 5 $NC $E5  $TC|$NC    $BG 6 $NC $E6     $TC|$NC"
    echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
    echo -e " $TC|$NC    $BG 7 $NC $E7 $NC    $TC|$NC    $BG 8 $NC $E8             $TC|$NC"
    echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
    echo -e " $TC|$NC    $BG 9 $NC $E9       $TC|$NC    $BG B $NC $E10       $TC|$NC"
    echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
    echo -e " $TC|$NC    $BG O $NC $E11       $TC|$NC    $BG P $NC $E12               $TC|$NC"
    echo -e " $TC+----------------------------------------------------------------+$NC"
    echo ""
    echo -e "  Enter marked number or letter   -    $BG 0 $NC $E0 "

    # save entered number/letter in variable "choice"
    read -r -n 1 -e choice

    # test, whether "choice" fits any of the following numbers or letters
    case "$choice" in
    1 | I | i)
        printf '\33[H\33[2J'
        install_kernel
        echo ""
        ;;
    2 | R | r)
        printf '\33[H\33[2J'
        remove_kernel
        echo ""
        ;;
    3 | F | f)
        printf '\33[H\33[2J'
        sudo mhwd -a pci free 0300
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    4 | N | n)
        printf '\33[H\33[2J'
        sudo mhwd -a pci nonfree 0300
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    5 | L | l)
        printf '\33[H\33[2J'
        mhwd -li -d
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    6 | C | c)
        printf '\33[H\33[2J'
        xinput_tui
        ;;
    7 | S | s)
        printf '\33[H\33[2J'
        sensors
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    8 | D | d)
        printf '\33[H\33[2J'
        ncdu -x /
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    9 | H | h)
        printf '\33[H\33[2J'
        ncdu -x $HOME
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    B | b)
        printf '\33[H\33[2J'
        sudo dmidecode -t bios
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    O | o)
        printf '\33[H\33[2J'
        if [[ -f /usr/bin/powertop ]]; then
            sudo powertop
        else
            echo "Installing Powertop"
            if [[ -f /var/lib/pacman/db.lck ]]; then
                echo "powertop is not installed and Pacman seems to be in use"
            else
                $INSTALLPKGS powertop && sudo powertop
            fi
        fi
        echo
        echo -e " $RED To return to Hardware and Drivers press ENTER $NC"
        # wait for input, e.g. by pressing ENTER:
        read -r
        ;;
    P | p)
        printf '\33[H\33[2J'
        bcups
        ;;
    0 | Q | q)
        printf '\33[H\33[2J' && exit
        ;;
    *) # do this, if $choice variable contains anything else not offered above
        echo -e "$ERRORMSG"
        echo -e "$TRYAGAINMSG"
        sleep 2
        ;;
    esac
done
