#!/bin/bash

RED='\e[41m'
BLUE='\e[44m'
ORANGE='\e[46m'
NC='\e[0m'
trap 'rm $HOME/{.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 &
inxi -c 5 -b > .inxi_info &

    while true; do
    clear
    echo ""
    echo -e "                             $NC ::Hardware::  "
    echo -e " ┌────────────────────────────────────────────────────────────────────────────┐"
    echo -e " │    1   Install a kernel                      2   Remove a kernel           │"
    echo -e " │    3   Use free graphics                     4   Use nonfree graphics      │"
    echo -e " │    5   List installed drivers                6   Configure touchpad        │"
    echo -e " │    7   Configure printers                    8   Configure monitors        │"
    echo -e " └────────────────────────────────────────────────────────────────────────────┘"
    echo -e "                           $NC ::Information::  "
    echo -e " ┌────────────────────────────────────────────────────────────────────────────┐"
    echo -e " │    I   Basic system information              T   System temperatures       │"
    echo -e " │    D   Disk usage                            U   Home folder usage         │"
    echo -e " │    H   Detailed hardware information         B   Bios information          │"
    echo -e " │    P   Prosessor information                 M   Mountpoints and partitions│"
    echo -e " └────────────────────────────────────────────────────────────────────────────┘"
    echo -e "          Select a number                       0 Exit mhwd-tui"
    echo ""
    read -s -n1 choix
    case $choix in
        1)
            echo
            install_kernel
            echo ""
            ;;
        2)
            echo
            remove_kernel
            echo ""
            ;;
        3)
            echo
            sudo mhwd -a pci free 0300
            echo ""
            echo "Done. Press any key to continue"
            read
            echo ""

            ;;
        4)
            echo
            sudo mhwd -a pci nonfree 0300
            echo ""
            echo "Done. Press any key to continue"
            read
            echo ""
            ;;
        5)
            echo
            mhwd -li -d
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        6)
            echo
            xinput_tui
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        7)
            echo
            bcups
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        8)
            echo
            brandr
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        I|i)
            echo
            inxi -c 5 -b
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        T|t)
            echo
            sensors
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        D|d)
            echo
            ncdu -x /
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        B|b)
            echo
            sudo dmidecode -t bios
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        P|p)
            echo
            sudo dmidecode -t processor
            lscpu
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        H|h)
            echo
            sudo lshw > .lshw && nano .lshw            
            rm .lshw
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        U|u)
            echo
            ncdu -x $HOME
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;
        M|m)
            echo
            lsblk -o NAME,SIZE,FSTYPE,LABEL,MOUNTPOINT,UUID
            echo ""
            echo "Press any key to continue"
            read
            echo ""
            ;;

        0)
            clear && exit
            read
            ;;
        *)
            echo -e "$RED Wrong option $NC"
            echo "Wait and try again later..."
            echo ""
            sleep 3
            clear
            ;;
    esac
    done
fi
