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

NO_SET_CLIPBOARD=0

show_usage () {
	echo 'usage: termux-info [--no-set-clipboard]'
	echo 'Provides information about Termux, and the current system. Helpful for debugging.'
	exit 0

}

while [ $# -ge 1 ]; do
	case "$1" in
		--no-set-clipboard) NO_SET_CLIPBOARD="1"; shift;;
		-h | --help) show_usage;;
		*) break;;
	esac
done

if [ "$#" != "0" ]; then
	show_usage
fi

updates() {
	local updatable

	if [ "$(id -u)" = "0" ]; then
		echo "Running as root. Cannot check package updates."
	else
		apt update >/dev/null 2>&1
		updatable=$(apt list --upgradable 2>/dev/null | tail -n +2)

		if [ -z "$updatable" ];then
			echo "All packages up to date"
		else
			echo "$updatable"
		fi
	fi
}

repo_subscriptions() {
	local main_sources
	main_sources=$(grep -P '^\s*deb\s' "/data/data/com.termux/files/usr/etc/apt/sources.list")

	if [ -n "$main_sources" ]; then
		echo "# sources.list"
		echo "$main_sources"
	fi

	local filename repo_package supl_sources
	while read -r filename; do
		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
		supl_sources=$(grep -P '^\s*deb\s' "$filename")

		if [ -n "$supl_sources" ]; then
			if [ -n "$repo_package" ]; then
				echo "# $repo_package (sources.list.d/$(basename "$filename"))"
			else
				echo "# sources.list.d/$(basename "$filename")"
			fi
			echo "$supl_sources"
		fi
	done < <(find "/data/data/com.termux/files/usr/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
}

case "${TERMUX__USER_ID:-}" in ''|*[!0-9]*|0[0-9]*) TERMUX__USER_ID=0;; esac
export TERMUX__USER_ID

output=""

if [ -n "${TERMUX_VERSION:-}" ]; then
# Application version is exported in Termux v0.107 or higher only.
output+="Termux Variables:
$(compgen -e TERMUX_ | while read v; do echo "${v}=${!v}"; done)
"
else
output+="Termux Variables:
unsupported
"
fi

output+="Packages CPU architecture:
$(dpkg --print-architecture)
Subscribed repositories:
$(repo_subscriptions)
Updatable packages:
$(updates)
Android version:
$(getprop ro.build.version.release)
Kernel build information:
$(uname -a)
Device manufacturer:
$(getprop ro.product.manufacturer)
Device model:
$(getprop ro.product.model)
Supported ABIs:
SUPPORTED_ABIS: $(getprop ro.product.cpu.abilist)
SUPPORTED_32_BIT_ABIS: $(getprop ro.product.cpu.abilist32)
SUPPORTED_64_BIT_ABIS: $(getprop ro.product.cpu.abilist64)
LD Variables:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH
LD_PRELOAD=$LD_PRELOAD"
# Escape '\$[](){}|^.?+*' with backslashes for regex
escaped_package_name="$(echo -n "com.termux" | sed -zE -e 's/[][\.|$(){}?+*^]/\\&/g')"
TERMUX_PLUGINS="$(pm list packages --user "$TERMUX__USER_ID" 2>&1 </dev/null | grep -E "^package:$escaped_package_name\.[a-zA-Z]" | cut -d ":" -f 2- | grep -vE "^$escaped_package_name\.tapm\.[a-zA-Z]")"
if [ -n "${TERMUX_PLUGINS:-}" ]; then
	        output+="$(printf "\nInstalled termux plugins:\n${TERMUX_PLUGINS}\n")"
fi
echo "$output"

if [ "$NO_SET_CLIPBOARD" != "1" ] && [ -n "$(command -v termux-clipboard-set)" ]; then
	# Copy to clipboard (requires termux-api)
	# use timeout in case termux-api is installed but the termux:api app is missing
	echo "$output" | timeout 3 termux-clipboard-set 2>/dev/null
	timeout 3 termux-toast "Information has been copied to the clipboard" 2>/dev/null
fi

exit 0
