#!/usr/bin/env bash

set -u
selected=""
version="2.0"

function process_done() {
 echo -e "\n"
	echo "Done!! Press any key..."
	read -n1 selected
}

function pkg_update() {
	clear
	sudo pacman -Syu
	process_done
	pkgmanage
}

function pkg_search() {
	clear
	SEARCHWORD=$(whiptail --inputbox "Search word?" 0 0 3>&1 1>&2 2>&3)
		exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		pkgmanage
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		pkgmanage
	else
	       	:
	fi
	pacman -Ss ${SEARCHWORD}
	SEARCHWORD=""
	process_done
	pkgmanage
}

function pkg_details() {
	clear
	PKGNAME=$(whiptail --inputbox "Package name?" 0 0 3>&1 1>&2 2>&3)
		exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		pkgmanage
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		pkgmanage
	else
	       	:
	fi
	pacman -Si ${PKGNAME}
	PKGNAME=""
	process_done
	pkgmanage
}

function pkg_install() {
	clear
	PKGNAME=$(whiptail --inputbox "Package name?" 0 0 3>&1 1>&2 2>&3)
	exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		pkgmanage
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		pkgmanage
	else
	       	:
	fi
	sudo pacman -S ${PKGNAME}
	PKGNAME=""
	process_done
	pkgmanage
}

function pkg_remove() {
	clear
	PKGNAME=$(whiptail --inputbox "Package name?" 0 0 3>&1 1>&2 2>&3)
	exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		pkgmanage
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		pkgmanage
	else
	       	:
	fi
	sudo pacman -Rs ${PKGNAME}
	PKGNAME=""
	process_done
	pkgmanage
}

function clean_cache () {
	clear
	sudo pacman -Scc
	process_done
	pkgmanage
}

function noneed_pkgs () {
	clear
	sudo pacman -Rns $(pacman -Qtdq)
	process_done
	pkgmanage
}

function pkgmanage () {
	clear
	pkg_run=$(whiptail --title "Package Management" --menu "" 0 0 0 --notags 'pkg_update' "Update all packages" 'pkg_search' "Search packages" 'pkg_details' "Show package details" 'pkg_install' "Install Package" 'pkg_remove' "Remove Packages" 'noneed_pkgs' "Remove no-needed packages" 'clean_cache' "Clean caches" 3>&1 1>&2 2>&3)
	exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		main
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		main
	else
	       	:
	fi
	${pkg_run}
}

function setntpdaemon () { 
	clear
	FILE="/etc/systemd/system/dbus-org.freedesktop.timesync1.service"
	if [ -e ${FILE} ]; then
		sudo timedatectl set-ntp false
	else
		sudo timedatectl set-ntp true
	fi
	process_done
	time_settings
}

function select_timezone() {
	clear
	sudo ln -sf /usr/share/zoneinfo/$(tzselect) /etc/localtime
	sudo hwclock --systohc
	echo -e "\nPlease reboot for changes to take effect."
	process_done
	time_settings
}

function ntp_sync() {
	clear
	sudo ntpdate pool.ntp.org
	process_done
	time_settings
}

function systohc() {
	clear
	sudo hwclock --systohc
	process_done
	time_settings
}

function time_settings () {
	clear
	FILE="/etc/systemd/system/dbus-org.freedesktop.timesync1.service"
	if [ -e ${FILE} ]; then
		NTPD="Disable"
	else
		NTPD="Enable"
	fi
	time_run=$(whiptail --title "Time Settings" --menu "" 0 0 0 --notags 'select_timezone' "Select time zone" 'ntp_sync' "Sync time with NTP" 'systohc' "Write to hardware clock" 'setntpdaemon' "${NTPD} NTP autosync" 3>&1 1>&2 2>&3)
		exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		main
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		main
	else
	       	:
	fi
	${time_run}
}

about_univa() {
	clear
 echo -e "About This System\n"
 echo -e "Univalent Tools\nVersion 2.0\n"
 echo -e "Univalent GNU/Linux\nVersion $(lsb_release -rs)\n"
 echo -e "Copyright (C) 2022-2023\nUnivalent Project / Radio New Japan"
 echo -e "\n"
 echo "Press any key..."
	read -n1 selected
}

function main () {
	clear
	main_run=$(whiptail --title "Univalent Hello" --menu "" 0 0 0 --notags 'pkgmanage' "Package Management" 'nmtui' "Network" 'alsamixer' "Sound" 'time_settings' "Time settings" 'about_univa' "About This System" 3>&1 1>&2 2>&3)
		exitmenu=$?
	if [ $exitmenu == 1 ]; then
		clear
		exit 0
	elif [ $exitmenu == 134 ]; then
		$(basename $0) --missing-popt
		exit 0
	else
	       	:
	fi
	${main_run}
}

while true; do
	main
done

