#!/bin/sh
################################################################################################################################################################
#
#				"AM" INSTALL, version 20260509
#
################################################################################################################################################################

# Colors
RED='\033[0;31m'; LightBlue='\033[1;34m'; Green='\033[0;32m'

# For developers
[ -z "$AM_BRANCH" ] && AM_BRANCH="main"

# Function to check online connections (uses github.com by default, as the database and CLI itself are stored/hosted there)
_online_check_tool() {
	if command -v wget >/dev/null 2>&1; then
		wget -q --tries=10 --timeout=20 --spider https://github.com
	elif command -v curl >/dev/null 2>&1; then
		curl --output /dev/null --silent --head --fail https://github.com 1> /dev/null
	else
		printf "\n %b💀 ERROR! MISSING ESSENTIAL COMMANDS\033[0m: %s\n\n Install the above and try again! \n\n" "${RED}" "curl, wget"
		exit 0
	fi
}

_online_check() {
	if ! _online_check_tool; then
		printf "\n Installer wouldn't work offline\n\n Please check your internet connection and try again\n\n"
		exit 0
	fi
}

_online_check

# Check dependencies for this script
_check_dependency() {
	AMDEPENDENCES="chmod chown grep"
	for dependency in $AMDEPENDENCES; do
		if ! command -v "$dependency" >/dev/null 2>&1; then
			printf "\n %b💀 ERROR! MISSING ESSENTIAL COMMAND \033[0m: %b\n\n Install the above and try again! \n\n" "${RED}" "$dependency"
			exit 1
		fi
	done
}

_check_dependency

_script_downloader() {
	if command -v wget >/dev/null 2>&1; then
		wget -q "$URL" -O "$SCRIPT"
	elif command -v curl >/dev/null 2>&1; then
		curl -Lo "$SCRIPT" "$URL" 2>/dev/null
	fi
}

if [ -z "${SUDO_USER:-$DOAS_USER}" ]; then
	echo 'Please use sudo or doas'
	exit 1
fi

# INSTALL "AM"
_prepare_am_directory() {
	# CREATE AND ENTER THE MAIN DIRECTORY FOR "AM"
	mkdir -p /opt/am/modules /usr/local/bin && cd /opt/am || exit 1

	# CREATE THE SCRIPT NEEDED TO UNINSTALL "AM"
	printf '#!/bin/sh\n\nset -e\n' > /opt/am/remove
	printf '\n%s\n' 'if [ "$(id -u)" -ne 0 ]; then echo "Permission denied"; exit 1; fi' >> /opt/am/remove
	printf '%s\n' 'rm -f /usr/local/bin/am' >> /opt/am/remove
	printf '%s\n' 'rm -R -f /opt/am' >> /opt/am/remove
	chmod a+x /opt/am/remove || exit 1

	# DOWNLOAD THE MAIN SCRIPT
	URL="https://raw.githubusercontent.com/ivan-hc/AM/$AM_BRANCH/APP-MANAGER"
	SCRIPT="APP-MANAGER"
	if ! _script_downloader; then
		echo "💀 ERROR: Cannot download am"
		exit 1
	fi
	chmod a+x /opt/am/APP-MANAGER
	MODULES=$(sort /opt/am/APP-MANAGER | tr '"' '\n' | grep "[a-z]\.am$")
	# LINK THE MAIN SCRIPT IN $PATH
	if uname -a | grep -qi "Linux deck \|Linux steamdeck "; then
		[ -d "/home/${SUDO_USER:-$DOAS_USER}" ] && BINDIR="/home/${SUDO_USER:-$DOAS_USER}/.local/bin" || BINDIR=""
		[ -n "$BINDIR" ] && mkdir -p "$BINDIR" && ln -fs /opt/am/APP-MANAGER "$BINDIR"/am 2>/dev/null && sed -i "s#/usr/local/bin#$BINDIR#g" /opt/am/remove
	else
		ln -fs /opt/am/APP-MANAGER /usr/local/bin/am 2>/dev/null || printf "%bWARNING: Couldn't link am to \"/usr/local/bin/am\"! \033[0m\n" "${RED}"
	fi
}

_download_am_modules() {
	# DOWNLOAD MODULES
	cd /opt/am/modules || exit
	if [ -z "$MODULES" ]; then
		echo "💀 ERROR: Cannot download am modules"
		exit 1
	fi
	for module_name in $MODULES; do
		if [ ! -f ./"$module_name" ]; then
			echo " ◆ Downloading $module_name..."
			URL="https://raw.githubusercontent.com/ivan-hc/AM/$AM_BRANCH/modules/$module_name"
			SCRIPT="$module_name"
			_script_downloader
		fi
	done
	cd ..

	# ENABLE NON-ROOT PERMISSIONS TO THE MAIN DIRECTORY FOR THE CURRENT USER
	chown -R "${SUDO_USER:-$DOAS_USER}" /opt/am
}

echo '--------------------------------------------------------------------------'
printf " %bInstalling \"AM\" in /opt/am\033[0m\n" "${Green}"
_prepare_am_directory
echo '--------------------------------------------------------------------------'
printf " %bInstalling modules\033[0m\n" "${Green}"
_download_am_modules
echo '--------------------------------------------------------------------------'
printf " %b\"AM\" has been successfully installed! \033[0m\n\n" "${Green}"
printf " Please, run \"%bam -h\033[0m\" to see the list of the options.\n" "${LightBlue}"
echo '--------------------------------------------------------------------------'
