#!/bin/bash

# =======================
# Define Colors
RED='\e[41m'
BLUE='\e[44m'
ORANGE='\e[46m'
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.$NC"
TRYAGAINMSG="$RED Press any key and try again$NC"
#
INSTALLINGTIMESETMSG="Installing timeset to manage time"
TIMESETNOTINSTMSG="Timeset is not installed and pacman seems to be in use"

if [ $(cat /proc/1/comm) = "systemd" ]; then
	systemctl status NetworkManager.service 2>/dev/null | grep -q " active " && netcommand="nmtui"
	systemctl status Netctl.service 2>/dev/null | grep -q " active " && netcommand="sudo wifi-menu"
	systemctl status systemd-networkd 2>/dev/null | grep -q " active " &&  netcommand="wpa_tui"
else
	rc-status | grep -q "NetworkManager" && netcommand="nmtui"
	rc-status | grep -q "Netctl" && netcommand="sudo wifi-menu"
fi

function manage_time {
	if [ -e /usr/bin/timeset ]; then
		sudo timeset
	else
		echo "$INSTALLINGTIMESETMSG"
		if [ -e /var/lib/pacman/db.lck ]; then
			echo "$TIMESETNOTINSTMSG"
		else
			sudo pacman -Sy timeset && sudo timeset
		fi
	fi
}

function init_settings {
if [ $(cat /proc/1/comm) = "systemd" ]; then
	initmenu
else
	initmenu-openrc
fi
}

E0="\e[1mQ\e[0muit"
E1="\e[1mA\e[0mppearance"
E2="\e[1mN\e[0metwork"
E3="\e[1mS\e[0mound"
E4="System \e[1mC\e[0monfiguration"
E5="\e[1mT\e[0mime Settings"
E6="\e[1mI\e[0mnit-System"

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

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

			echo
			echo -e "                         $BG$TC System & Settings $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"
			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|A|a)
            echo
            appearance-menu
            echo ""
            ;;
        2|N|n)
            echo
            $netcommand
            echo ""
            ;;
        3|S|s)
            echo
            alsamixer
            echo ""
            ;;
        4|C|c)
            echo
            system-settings
            echo ""
            ;;
        5|T|t)
            echo
            manage_time
            echo ""
            ;;
        6|I|i)
            echo
            init_settings
            echo ""
            ;;
		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
}

main
