#!/bin/env bash
# This file is part of Origami Kernel Manager.
#
# Origami Kernel Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Origami Kernel Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Origami Kernel Manager.  If not, see <https://www.gnu.org/licenses/>.
#
# Copyright (C) 2023-present Rem01Gaming

[ ! -d /usr ] && ANDROID=1
[ -z $PREFIX ] && PREFIX="/usr"

# Check dependencies
if ! hash fzf fzy jq curl sqlite3; then
	echo "[-] Environment has missing dependencies"
	exit 127
fi

[ ! -d $HOME/.okm ] && mkdir -p $HOME/.okm

if [ ! -f $PREFIX/share/origami-kernel/init_run.sh ]; then
	echo "[-] Faulty installation detected !"
	echo "[*] Please reinstall OKM"
	exit 1
fi

source $PREFIX/share/origami-kernel/menu_helper.sh
tput smcup
tput civis
clear
start_throbber "Gathering information about your hardware..."

# Sync to data in the rare case a device crashes
sync

source $PREFIX/share/origami-kernel/init_run.sh
source $PREFIX/share/origami-kernel/database_util.sh
source $PREFIX/share/origami-kernel/settings.sh
source $PREFIX/share/origami-kernel/utils/cpu/cpu_util.sh
source $PREFIX/share/origami-kernel/utils/gpu/gpu_util.sh
source $PREFIX/share/origami-kernel/utils/dram/dram_util.sh
source $PREFIX/share/origami-kernel/utils/memory/memory_util.sh
source $PREFIX/share/origami-kernel/utils/net/net_util.sh
source $PREFIX/share/origami-kernel/utils/disp/disp_util.sh
source $PREFIX/share/origami-kernel/utils/misc/misc_util.sh
source $PREFIX/share/origami-kernel/utils/battery/battery_util.sh
source $PREFIX/share/origami-kernel/utils/sched/sched_util.sh

LINE=$(stty size | awk '{print $2}')
VERSION="v$(cat $PREFIX/share/origami-kernel/version)"
ENTROPY="$(cat /proc/sys/kernel/random/entropy_avail)/$(cat /proc/sys/kernel/random/poolsize) ($(($(cat /proc/sys/kernel/random/entropy_avail) * 100 / $(cat /proc/sys/kernel/random/poolsize)))% available)"

if [ $ANDROID ]; then
	ANDROID_VER=$(getprop ro.build.version.release)
	VENDOR=$(getprop "ro.product.manufacturer")
fi

exit_handler() {
	if [ "$?" -eq 0 ]; then
		stop_throbber
		tput cnorm
		tput rmcup
	fi
}

trap "exit_handler" EXIT

if [ ! -f $database_path ]; then
	create_database
fi

if [[ $(get_db_version) != "$VERSION" ]]; then
	update_db_version
fi

if [ $(risk_acceptence) -eq 1 ] && [ ! -f /dev/okm-execstoredcmd ] && [ $(sql_query "SELECT execstoredcmd FROM tb_info;") -eq 1 ]; then
	stop_throbber
	init_execstoredcmd
	touch /dev/okm-execstoredcmd
fi

# User agreement
if [ $(risk_acceptence) -eq 0 ]; then
	stop_throbber
	clear
	fold -s -w $LINE <<EOF
OKM - Origami Kernel Manager $VERSION
Copyright (c) 2023-present Rem01Gaming

DISCLAIMER AND LICENSE AGREEMENT

Origami Kernel Manager offers advanced root functions that could potentially harm your device if used incorrectly or in combination with other software, tweaks, or modules. Please be cautious as Origami Kernel Manager does not take responsibility for any damages resulting from the misuse of this software.

If you are not well-versed in how root access functions, it's advisable to refrain from utilizing the root options until you have a complete understanding of the associated risks.

This software is licensed under the GNU General Public License v3.0. It comes with NO WARRANTY AND LIABILITY. By using this software, you acknowledge and accept full responsibility for its use. You agree that any actions taken with this software that negatively impact your device are not the responsibility of the developer.


EOF
	read -p "Type 'I AGREE' to continue: " a1
	case $a1 in
	"I AGREE") accept_risk ;;
	*)
		clear
		echo -e "\033[38;5;196m[-] Not agreed, Aborted.\033[0m"
		exit 1
		;;
	esac
fi

show_license() {
	clear
	fold -s -w $LINE <<EOF
Origami Kernel Manager ${VERSION}
Copyright (c) 2023-present Rem01Gaming

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, Kindly write a message to me Rem01Gaming.

Report problems and questions to https://t.me/rem01shideout or https://github.com/Rem01Gaming/origami_kernel_manager

Rem01Gaming <Rem01_Gaming@proton.me>


[*] Press enter to back into main menu
EOF
	read -r -s
}

donate() {
	# Use the ip-api.com JSON API to get geolocation data
	GEO_DATA=$(curl -s "http://ip-api.com/json")
	code=$?

	if [ $code -gt 0 ]; then
		echo -e "\n[-] No internet nor http://ip-api.com access"
		echo "[*] Hit enter to back to main menu"
		read -r -s
		return $code
	fi

	# Extract the country value from the JSON response
	COUNTRY=$(echo "$GEO_DATA" | jq -r '.countryCode')

	# Check if the country is Indonesia or Philippines
	if [ "$COUNTRY" == "ID" ] || [ "$COUNTRY" == "PH" ]; then
		open_link https://saweria.co/Rem01Gaming
	else
		open_link https://www.buymeacoffee.com/Rem01Gaming
	fi
}

main_menu() {
	while true; do
		unset_headvar
		header_info=()
		[ $ANDROID ] && header_info+=(
			"[] DEVICE: $(getprop ro.product.model)"
			"[] MANUFACTURER: $VENDOR"
			"[] SELINUX: $(getenforce)"
			"[] ANDROID VERSION: $ANDROID_VER"
		)
		header_info+=(
			"[] CPU: $chipset"
			"[] GPU: $gpu"
			"[] KERNEL VERSION: $(uname -r)"
			"[] UPTIME: $(uptime -p)"
			"[] ENTROPY: $ENTROPY"
		)
		options="CPU control\nGPU control\nDRAM control\nMemory settings\nScheduler settings\nNetworking settings\nMiscellaneous settings\nCharging controller\nDisplay color calibration"
		header "Main Menu"
		selected="$(fzy_select "$options\nOKM Settings\nShow License\nDonate me\nJoin my telegram channel\nExit" "")"

		case "$selected" in
		"CPU control") cpu_menu ;;
		"GPU control") gpu_menu ;;
		"DRAM control") dram_menu ;;
		"Memory settings") memory_menu ;;
		"Scheduler settings") sched_menu ;;
		"Networking settings") net_menu ;;
		"Miscellaneous settings") misc_menu ;;
		"Charging controller") batt_menu ;;
		"Display color calibration") disp_menu ;;
		"OKM Settings") settings_menu ;;
		"Show License") show_license ;;
		"Join my telegram channel") open_link https://t.me/rem01schannel ;;
		"Donate me") donate ;;
		"Exit") clear && exit 0 ;;
		esac
	done
}

stop_throbber
main_menu
