#!/data/data/com.termux/files/usr/bin/env bash

# By YZBruh | ShawkTeam

# Variables
LOCAL_VERSION="3.0.2"
LOCAL_RELDATE="20241214"
LOCAL_OWNER="ShawkTeam"
LOCAL_REPO="pmt"
LOCAL_RELEASE_TAG="${LOCAL_VERSION}"
LOCAL_PREFIX="${PREFIX}"
[ -d "${LOCAL_PREFIX}" ] \
|| LOCAL_PREFIX="/data/data/com.termux/files/usr"
LOCAL_TMPDIR="${LOCAL_PREFIX}/tmp/pmt-termux-helper"
SILENT=false

# Colors
RESET="\033[0m"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"

# Printer functions
function printc() { ${SILENT} || echo -e "$*" >&2; }
function print()
{
	if echo "$*" | grep "Success" &>/dev/null; then
		${SILENT} || echo -e " - ${GREEN}${1:0:7}${RESET}." >&2
	else
		${SILENT} || echo -e " - $*" >&2
	fi
}

# Print error and exit
function abort()
{
	print "${RED}Error:${RESET} ${@}"
	cleanup
	exit 1
}

# Print warnings
function warning() { print "${YELLOW}Warning:${RESET} ${@}"; }

# Print head
function script_head() { printc " --- Partition Manager Termux Helper Script --- \n"; }

# For display help message
function view_help()
{
	${SILENT} || echo -n "Usage: "
	if echo "${0}" | grep "./" >&/dev/null; then
		printc "${0} [OPTIONS]..."
	else
		printc "(bash) ${0} [OPTIONS]"
	fi

	printc "Install, uninstall, install status checker for Partition Manager binary.\n"
	printc "Options:"
	printc "	install,   -i [OPTS]		Download and install Partition Manager."
	printc "	uninstall, -u			   Uninstall Partition Manager."
	printc "	status,	-s			   Display install/uninstall status."
	printc "	--quiet,   -q			   Silent mode. No output."
	printc "	--setup,   -S			   Setup required packages."
	printc "	--package <FILE>			If you already have a pmt package, make\n								  setup by specifying this way."
	printc "Report bugs to <t.me/ShawkTeam | Topics | pmt>"
}

# Script really operated termux proclamation?
function really_termux()
{
	basename -a ${LOCAL_PREFIX}/bin/* | grep "termux" &>/dev/null \
	|| abort "Are you sure you're in termux?"
}

# Get architecture info
function get_architecture()
{
	LOCAL_ARCH=$(getprop ro.product.cpu.abi)

	print "Device architecture: ${arch}."
}

# Generate template dir
function gen_tempdir() { mkdir -p "${LOCAL_TMPDIR}"; }

# Clean template dir
function cleanup() { rm -rf "${LOCAL_TMPDIR}"; }

# Check install/uninstall status
function check_status()
{
	local installed="${RED}false${RESET}"
	/system/bin/which pmt &>/dev/null \
	&& installed="${GREEN}true${RESET}"

	print "Installed: ${installed}"
	exit 0
}

# check xz-utils install status
function check_xz()
{
	# The outflow of the APT is not controlled in use
	[ -f ${LOCAL_PREFIX}/bin/xz ] \
	|| abort "xz-utils not installed! Run setup command."
}

# Check internet connection
function net_control()
{
	curl "https://github.com" &>/dev/null \
	|| abort "You need internet connection to this process."
}

# Download Partition Manager
function download()
{
	local URL="https://github.com/${LOCAL_OWNER}/${LOCAL_REPO}/releases/tag/${LOCAL_RELEASE_TAG}/download/pmt-${LOCAL_ARCH}-${LOCAL_RELDATE}.xz"
	local URL_MANDOC="https://github.com/${LOCAL_OWNER}/${LOCAL_REPO}/releases/tag/${LOCAL_RELEASE_TAG}/download/mandoc.gz"

	cd "${LOCAL_TMPDIR}"
	print "Downloading: 'pmt-${LOCAL_ARCH}-${LOCAL_RELDATE}.xz'..."
	wget "${URL}" &>/dev/null \
	|| abort "Download failed!"
	cd - &>/dev/null

	HAVE_MANDOC=true
	print "Downloading mandoc..."
	curl -L "${URL_MANDOC}" -o "${LOCAL_TMPDIR}/pmt.8.gz" &>/dev/null \
	|| warning "Download failed! (mandoc)"

	chmod -R 755 "${LOCAL_TMPDIR}" &>/dev/null \
	|| warning "Cannot set mode '777' on downloaded files."
}

# For installing required packages
function setup_packages()
{
	net_control

	print "Updating mirrors..."
	pkg update &>/dev/null || abort "Updating failed!"

	print "Installing xz-utils, wget..."
	pkg install -y xz-utils wget &>/dev/null \
	|| abort "Installing failed!"

	print "Success."
	exit 0
}

# Install deb package
function install_fn()
{
	local mydir="${PWD}"
	[ "${LOCAL_PACKAGE}" = "" ] || local bname=$(basename ${LOCAL_PACKAGE})

	${PACKAGE} && \
	if [ ! -f "${LOCAL_PACKAGE}" ]; then
		[ -d "${LOCAL_PACKAGE}" ] \
		&& abort "\`${LOCAL_PACKAGE}': is directory."

		abort "\`${LOCAL_PACKAGE}': no such file."
	else
		cp "${LOCAL_PACKAGE}" "${LOCAL_TMPDIR}/pmt-${LOCAL_ARCH}.xz"
	fi

	cd "${LOCAL_TMPDIR}"

	print "Extracting package..."
	if unxz "$(basename *.xz)" &>/dev/null; then
		rm -f "pmt*.xz"
	else
		abort "Failed! Cannot extract pmt package."
	fi

	[ -z $(basename "pmt-*") ] \
	&& abort "Extracted binary file was not found!"
	mv "$(basename pmt-*)" pmt

	print "Installing..."
	cp pmt "${LOCAL_PREFIX}/bin/pmt" &>/dev/null \
	|| abort "Installing failed!"

	${HAVE_MANDOC} && {
		print "Installing mandoc (force)..."
		cp -f  pmt.8.gz ${LOCAL_PREFIX}/share/man/man8 &>/dev/null
		chmod 755 ${LOCAL_PREFIX}/share/man/man8/pmt.8.gz &>/dev/null
	}

	print "Setting up permissions for binary..."
	chmod 755 "${LOCAL_PREFIX}/bin/pmt" &>/dev/null \
	|| abort "Failed to set permissions!"

	print "Success."
	cd "${mydir}"
}

# Uninstall deb package
function uninstall_fn()
{
	print "Uninstalling Partition Manager..."

	if rm "${LOCAL_PREFIX}/bin/pmt" &>/dev/null; then
		print "Success."
	else
		abort "Failed!"
	fi

	rm -f ${LOCAL_PREFIX}/share/man/man8/pmt.8.gz
}

trap "abort Interrupt." SIGINT
trap "cleanup" SIGQUIT
PACKAGE=false
ALREADY_SHIFT=false
HAVE_MANDOC=false

# Process arguments
while (($# >= 1)); do
	SOME_SPEC=1

	case "${1}" in
		install|-i)
			PROCESS=1
			;;
		uninstall|-u)
			PROCESS=2
			;;
		status|-s)
			script_head
			check_status y
			;;
		--quiet|-q)
			SILENT=true
			;;
		--setup|-S)
			script_head
			print "Starting package setupper..."
			setup_packages
			;;
		--package)
			PACKAGE=true
			[ -z ${2} ] && {
				printc "Option '--package' requires an argument (file)."
				exit 1
			}
			LOCAL_PACKAGE="${2}"
			ALREADY_SHIFT=true && shift 1
			;;
		--help)
			view_help
			exit 0
			;;
		*)
			if echo ${1} | grep "-" &>/dev/null; then
				if ! echo ${1} | grep ".xz" &>/dev/null; then
					printc "Unknown option: ${1}"
					exit 1
				else
					break;
				fi
			else
				break
			fi
			;;
	esac

	${ALREADY_SHIFT} || shift 1
done

### Main ###
[ -z "${1}" -a "${SOME_SPEC}" != 1 ] && { view_help; exit 1; }

[ "${PROCESS}" = 1 -o "${PROCESS}" = 2 ] && {
	script_head
	really_termux
	gen_tempdir
}

if [ "${PROCESS}" = 1 ]; then
	/system/bin/which pmt &>/dev/null \
	&& abort "Partition Manager already installed."
	${PACKAGE} || net_control
	check_xz
	get_architecture
	print "Starting install process..."
	${PACKAGE} || download
	install_fn
elif [ "${PROCESS}" = 2 ]; then
	/system/bin/which pmt &>/dev/null \
	&& abort "Partition Manager already uninstalled."
	print "Starting uninstall process..."
	uninstall_fn
fi

cleanup

exit 0
