#!/bin/sh

############################################################################################
# THIS MODULE INCLUDES ALL THE ACTIONS INTENDED FOR THE MANAGEMENT OF LISTS AND SINGLE PAGES
############################################################################################

################################################################################
#				ABOUT
################################################################################
_about_show_markdown() {
	if curl -o /dev/null -sIf "$markdown_url"; then
		markdown_page=$(curl -Ls "$markdown_url" \
		| sed '1,${ /^\s*#/ d; /^\s*!/ d; /\[Applications]/d; /\ --- /d; /\ | - | /d; /\!\[/d; }' \
		| sed '$!N;s/^\s*\n\s*$//;P;D')
		echo "$markdown_page" | fold -sw 72 | sed 's/^ //g; s/^/ /g'
	elif curl -Ls "$toolpack_readme" | grep -q "^| $arg " || curl -Ls "$toolpack_readme" | grep -q "^| $(echo "$arg" | sed 's/\.toolpack//g') "; then
		toolpack_appname=$(echo "$arg" | sed 's/\.toolpack//g')
		toolpack_md=$(curl -Ls "$toolpack_readme")
		toolpack_arg=$(echo "$toolpack_md" | grep -i "^| $toolpack_appname " | tr '|' '\n' | cut -c 2- | grep .)
		toolpack_about_description=$(echo "$toolpack_arg" | awk -F: "NR==$toolpack_description")
		toolpack_about_site=$(echo "$toolpack_arg" | awk -F: "NR==$toolpack_site")
		printf "\n%b\n\nSOURCE: %b\n" "$toolpack_about_description" "$toolpack_about_site" | fold -sw 72 | sed 's/^ //g; s/^/ /g'
	else
		printf ' "%s" IS NOT A VALID ARGUMENT\n' "$package_name"
	fi
}

_about_page() {
	_determine_args
	package_name="${Green}$1\033[0m"
	markdown_url="$AMCATALOGUEMARKDOWNS/${1}.md"
	printf " PACKAGE: %b\n" "$package_name"
	app_name=$(echo "$1" | sed 's/-appimage//g; s/debian-testing-//g; s/\.toolpack//g')
	argpath=$(echo "$ARGPATHS" | grep "/$app_name$")
	if [ -f "$argpath/remove" ]; then
		app_status="installed\033[0m"
		if test -f "$argpath/.am-installer/$app_name"; then
			app_status="installed\033[0m"
		elif [ "$app_name" != "$1" ]; then
			if test -f "$argpath/.am-installer/$1"; then
				app_status="installed\033[0m, as ${Green}$app_name\033[0m"
			fi
		elif test -d "$argpath/.am-installer"; then
			scriptname=$(basename -- "$(find "$argpath"/.am-installer/* | head -1)")
			if [ "$scriptname" != "$1" ]; then
				app_status="installed\033[0m, but replaced by ${Green}$scriptname\033[0m\n\n NOTE: run ${RED}$AMCLI -a $scriptname\033[0m instead"
			fi
		fi
	elif [ -f "$argpath-appimage/remove" ]; then
		app_status="installed\033[0m"
		app_name="$app_name-appimage"
	else
		app_status="not installed"
	fi
	METAPACKAGES="kdegames kdeutils node platform-tools"
	for m in $METAPACKAGES; do
		metaargpath=$(echo "$ARGPATHS" | grep "/$m$")
		if grep -q "◆ $1 : .*\"$m\"" "$AMDATADIR/$ARCH-apps"; then
			if test -f "$metaargpath/remove"; then
				app_status="installed\033[0m, as part of ${Green}$m\033[0m\n\n NOTE: run ${RED}$AMCLI -a $m\033[0m instead"
				app_name="$m"
			fi
		elif [ "$1" = "$m" ]; then
			app_status="installed\033[0m"
		fi
	done
	if echo "$app_status" | grep -q "^installed"; then
		printf " STATUS: %b\n" "${LightBlue}$app_status"
		disk_usage=$(du -sm "$argpath" | cut -f1)
		[ -z "$disk_usage" ] && disk_usage=$(du -sm "$metaargpath" | cut -f1)
		app_version=$(grep -w " ◆ $app_name	|" "$AMCACHEDIR"/version-args 2>/dev/null | sed 's:.*|	::')
		echo ""
		echo " Disk usage: $disk_usage MB"
		echo " Installed version: $app_version"
	else
		echo " STATUS: $app_status"
	fi
	_about_show_markdown
}

################################################################################
#				FILES
################################################################################
_files_header() {
	echo ""
	APPSNUMB=$(find "$APPSPATH" -type f -name 'remove' -exec grep -L 'usr/local/lib' {} \; 2>/dev/null | sed -r 's|/[^/]+$||' | sort | uniq | wc -l)
	if [ "$APPSNUMB" = 1 ]; then
		APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAM"
	else
		APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAMS"
	fi
	if grep -q 'usr/local/lib' "$APPSPATH"/*/remove 2> /dev/null; then
		LIBNUMB=$(grep -l "usr/local/lib" "$APPSPATH"/*/remove | wc -l)
		if [ "$LIBNUMB" = 1 ]; then
			LIBSMESSAGE="AND $LIBNUMB LIBRARY"
		else
			LIBSMESSAGE="AND $LIBNUMB LIBRARIES"
		fi
		[ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE $LIBSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE $LIBSMESSAGE MANAGED BY \"$AMCLIUPPER\""
	else
		[ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE MANAGED BY \"$AMCLIUPPER\""
	fi
	echo ""
}

# Functions to determine file's type

_files_if_binary() {
	if file ./"$arg"/* | grep "static" >/dev/null 2>&1; then
		echo " ◆ $arg	|	static-binary" >> "$AMCACHEDIR"/files-type
	else
		echo " ◆ $arg	|	dynamic-binary" >> "$AMCACHEDIR"/files-type
	fi
}

_files_if_library() {
	echo " ◆ $arg	|	library" >> "$AMCACHEDIR"/files-type
}

_files_if_script() {
	if grep "/ffwa" "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	launcher" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*bash' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	bash-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*dash' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	dash-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*bin.*sh' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	posix-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*awk' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	awk-script" >> "$AMCACHEDIR"/files-type
	else
		echo " ◆ $arg	|	script" >> "$AMCACHEDIR"/files-type
	fi
}

_files_if_appimage() {
	if ! echo "$string" | grep 'AppImages require FUSE to run' >/dev/null 2>&1; then
		if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
			echo " ◆ $arg	|	appimage🔒" >> "$AMCACHEDIR"/files-type
		else
			echo " ◆ $arg	|	appimage" >> "$AMCACHEDIR"/files-type
		fi
	else
		if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
			echo " ◆ $arg	|	appimage🔒*" >> "$AMCACHEDIR"/files-type
		else
			echo " ◆ $arg	|	appimage*" >> "$AMCACHEDIR"/files-type
		fi
	fi
}

_files_type() {
	APPVERSION=$(<"$AMCACHEDIR"/version-args grep -w " ◆ $arg	|" | sed 's:.*|	::')
	string=$(strings -d ./"$arg/$arg" 2>/dev/null)
	FILE=$(command -v "$arg" 2>/dev/null)
	LINK=$(readlink "$FILE" 2>/dev/null)
	if echo "$string" | grep -- '--appimage-extract option' >/dev/null 2>&1; then
		_files_if_appimage
	elif file ./"$arg"/* | grep -E 'LSB|/bin' >/dev/null 2>&1; then
		_files_if_binary
	elif grep "#!" "${LINK:-$FILE}" >/dev/null 2>&1; then
		_files_if_script
	elif grep  "usr/local/lib" ./"$arg"/remove >/dev/null 2>&1; then
		_files_if_library
	else
		echo " ◆ $arg	|	other" >> "$AMCACHEDIR"/files-type
	fi
}

# Function to determine file's size
_files_sizes() {
	if grep -q "usr/local/lib" ./"$arg"/remove; then
		LIBNAME=$(<"$APPSPATH"/"$arg"/remove tr ' ' '\n' | grep "usr/local/lib" | head -1 | sed 's/*//g')
		SIZE=$(du -sh "$LIBNAME"* | cut -f1 | sort -rh | head -1)
	else
		SIZE=$(du -sh -- "$arg" | cut -f1 -d"	")
	fi
	SIZE=$(echo "$SIZE" | sed 's/.$/ &/; s/$/iB/')
	echo " ◆ $arg	|	$SIZE" >> "$AMCACHEDIR"/files-sizes
}

# Function to determine file's database
_files_db() {
	if test -f "$APPSPATH"/"$arg"/.am-installer/*toolpack; then
		DB="toolpacks"
	elif [ "$arg" = am ]; then
		DB="am"
	elif test -f "$CACHEDIR"/extra/"$arg" || test -f "$APPSPATH"/"$arg"/.am-installer/"$arg" && ! grep -q "^APP=" "$APPSPATH"/"$arg"/.am-installer/"$arg"; then
		DB="none"
	elif test -f "$AMDATADIR/$ARCH-apps" && ! grep -q "^◆ $arg :" "$AMDATADIR/$ARCH-apps"; then
		DB="unknown"
	else
		DB="am"
	fi
	echo " ◆ $arg	|	$DB" >> "$AMCACHEDIR"/files-db
}

# Functions to create file's table
_files_files() {
	cd "$APPSPATH" || exit 1
	INSTALLED_APPS=$(find . -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2> /dev/null | sort -rh | sed 's@.*	@@')
	if ! test -f "$AMCACHEDIR"/version-args; then
		_check_version
	fi
	if ! test -f "$AMCACHEDIR"/files-type; then
		for arg in $INSTALLED_APPS; do
			if test -f ./"$arg"/remove 2>/dev/null; then
		 		_files_type
			fi
		done
	fi
	rm -f "$AMCACHEDIR"/files-sizes "$AMCACHEDIR"/files-db
	for arg in $INSTALLED_APPS; do
		if test -f ./"$arg"/remove 2>/dev/null; then
			_files_sizes
			_files_db
		fi
	done
}

_files() {
	_files_files
	rm -f "$AMCACHEDIR"/files-args
	INSTALLED_APPS=$(find . -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2>/dev/null | sort -rh | sed 's@.*	@@')
	for arg in $INSTALLED_APPS; do
		if test -f ./"$arg"/remove 2>/dev/null; then
			APPVERSION=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/version-args | tr '	' '\n' | tail -1)
			APPTYPE=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/files-type | tr '	' '\n' | tail -1)
			APPSYZE=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/files-sizes | tr '	' '\n' | tail -1)
			APPDB=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/files-db | tr '	' '\n' | tail -1)
			echo " ◆ $arg	|	$APPDB	|	$APPVERSION	|	$APPTYPE	|	$APPSYZE" >> "$AMCACHEDIR"/files-args
		fi
	done
}

_files_number() {
	FILES_NUMBER=$(find "$APPSPATH" -type f -name 'remove' 2>/dev/null | uniq | wc -l)
}

_files_appimage_type_notes() {
	if grep -qe "appimage\*\|appimage🔒\*" "$AMCACHEDIR"/files*; then
		printf " *has dependency to EOL libfuse2 library, please ask the creator to update\n"
		printf "\n"
	fi
}

_files_total_size() {
	printf "\n"
	command -v aisap >/dev/null 2>&1 && printf '%s\n\n' " AppImages with 🔒 are sandboxed with aisap"
	echo " TOTAL SIZE: $(find . -type f -name 'remove' -printf "%h\n" | du -ch -- */ 2>/dev/null | awk 'END {print $1"iB"}' | sed 's/...$/ &/') of disk space in use"
	printf "\n"
}

_files_sort_by_name() {
	_files_header
	rm -f "$AMCACHEDIR"/files-args-byname
	_files
	echo "- APPNAME | DB | VERSION | TYPE | SIZE " > "$AMCACHEDIR"/files-args-byname
	echo "- ------- | -- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-byname
	sort "$AMCACHEDIR"/files-args 2>/dev/null >> "$AMCACHEDIR"/files-args-byname
	column -t "$AMCACHEDIR"/files-args-byname
	_files_total_size
	_files_appimage_type_notes
}

_files_sort_by_size() {
	_files_header
	rm -f "$AMCACHEDIR"/files-args-bysize
	_files
	echo "- APPNAME | DB | VERSION | TYPE | SIZE " > "$AMCACHEDIR"/files-args-bysize
	echo "- ------- | -- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-bysize
	cat "$AMCACHEDIR"/files-args >> "$AMCACHEDIR"/files-args-bysize 2>/dev/null
	column -t "$AMCACHEDIR"/files-args-bysize
	_files_total_size
	_files_appimage_type_notes
}

################################################################################
#				LIST/QUERY
################################################################################
_pretty_list() {
	# Remove references to URLs, "-a" elements in "-l" and "-q"
	sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g; s/^/\n/g' | _colors | fold -sw 75 | sed 's/^/   /g; s/  ◆ /◆ /g; s/  :/ :/g'
}

_pretty_list_compat() {
	# Remove references to URLs, "-a" elements in "-l" and "-q"
	sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g' | _colors | fold -sw 75 | sed 's/^/   /g; s/  ◆ /◆ /g; s/  :/ :/g'
}

_colors() {
	awk '{
		printf "%s \033[32m%s\033[0m", $1, $2
		{$1 = ""; $2 = ""; print $0;}
	}'
}

_list() {
	# Check if github.com is online, if not, the function will read the offline list
	wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
	# Check the number of installed apps and libraries
	LIBNUMBER=$(grep "usr/local/lib" "$APPSPATH"/*/remove 2> /dev/null | wc -l)
	ITEMSNUMBER=$(find "$APPSPATH" -name 'remove' -printf "%h\n" 2>/dev/null | sort -u | wc -l)
	_detect_appman_apps
	if [ "$AMCLI" = am ]; then
		if [ -d "$APPMAN_APPSPATH" ]; then
			APPMAN_ITEMS=$(find "$APPMAN_APPSPATH" -name 'remove' -printf "%h\n" 2>/dev/null | sort -u | wc -l)
			ITEMSNUMBER=$(("$ITEMSNUMBER"+"$APPMAN_ITEMS"))
		fi
		if test -f /opt/am/remove; then
			ITEMSNUMBER=$(("$ITEMSNUMBER"-1))
		fi
	fi
	APPSNUMBER=$(("$ITEMSNUMBER" - "$LIBNUMBER"))
	# Determine the number of available apps from the list
	AVAILABLE_APPS_NUMBER=$(grep -v "ffwa-\|\"kdegames\"\|\"kdeutils\"\|\"node\"\|\"platform-tools\"" "$AMDATADIR/$ARCH-apps" | grep -e "$" -c)
	# Generate a list of the installed apps with version
	[ ! -f "$AMCACHEDIR"/version-args ] && _check_version
	if [ -n "$APPMAN_ITEMS" ]; then
		_check_version
	else
		grep " ◆ $ARGS	" "$AMCACHEDIR"/version-args > "$AMCACHEDIR"/version-args-real
		sort "$AMCACHEDIR"/version-args-real > "$AMCACHEDIR"/version-args
	fi
	INSTALLED=$(sort "$AMCACHEDIR"/version-args 2>/dev/null | sed 's/	|	/ /g' | grep -v "◆ am " | uniq)
	MESSAGE2="$INSTALLED"
	# Check if among the installed apps are available libraries
	if [ "$LIBNUMBER" != 0 ]; then
		# Check if the installed libraries are more than one
		if [ "$LIBNUMBER" = 1 ]; then
			MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARY"
		else
			MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARIES"
		fi
	else
		MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE"
	fi
}

_list_appimages() {
	# Determine the number of available apps
	APPIMAGE_NAMES=$(curl -Ls https://portable-linux-apps.github.io/appimages.md | tr '/)' '\n' | grep -i ".md$" | uniq | sed 's/.md$//g' | grep -v "\[")
	if ! test -f "$AMCACHEDIR/$ARCH-appimages"; then
		_online_check
		for appimage in $APPIMAGE_NAMES; do
			grep "◆ $appimage :" "$AMDATADIR/$ARCH-apps" >> "$AMCACHEDIR/$ARCH-appimages" &
		done
		wait
	fi
	AVAILABLE_APPIMAGES_NUMBER=$(grep -e "$" -c "$AMCACHEDIR/$ARCH-appimages")
}

_list_toolpacks() {
	# Determine the number of available apps
	if ! test -f "$AMCACHEDIR/$ARCH-toolpack"; then
		toolpack_md=$(curl -Ls "$toolpack_readme")
		TOOLPACK_NAMES=$(echo "$toolpack_md" | cut -d' ' -f2- | awk '{print $1}')
		for toolpack in $TOOLPACK_NAMES; do
			toolpack_arg=$(echo "$toolpack_md" | grep -i "^| $toolpack " | tr '|' '\n' | cut -c 2- | grep .) \
			&& toolpack_about_description=$(echo "$toolpack_arg" | awk -F: "NR==$toolpack_description" | sed 's/ $/./g') \
			&& echo "◆ $toolpack : $toolpack_about_description" >> "$AMCACHEDIR/$ARCH-toolpack" &
		done
		wait
		sort "$AMCACHEDIR/$ARCH-toolpack" | uniq > "$AMCACHEDIR/$ARCH-toolpack-new" && mv "$AMCACHEDIR/$ARCH-toolpack-new" "$AMCACHEDIR/$ARCH-toolpack" || exit 1
	fi
	AVAILABLE_TOOLPACKS_NUMBER=$(grep -e "$" -c "$AMCACHEDIR/$ARCH-toolpack")
}

_list_msg_head() {
	echo "" && echo "$MESSAGE" && echo "" && echo "$MESSAGE2" && echo ""
}

_list_msg_footer() {
	echo "$LIST" && echo ""
}

_list_toolpacks_msg() {
	_list_msg_head
	echo " LIST OF THE $AVAILABLE_TOOLPACKS_NUMBER TOOLPACK PROGRAMS AVAILABLE:"
	_list_msg_footer
}

_list_appimages_msg() {
	_list_msg_head
	echo " LIST OF THE $AVAILABLE_APPIMAGES_NUMBER APPIMAGES AVAILABLE IN THE 'AM' REPOSITORY:"
	_list_msg_footer
}

_list_apps_msg() {
	_list_msg_head
	echo " LIST OF THE $AVAILABLE_APPS_NUMBER APPLICATIONS AVAILABLE IN THE 'AM' REPOSITORY:"
	_list_msg_footer
}

_list_apps_all_msg() {
	_list_msg_head
	AVAILABLE_APPS_NUMBER=$(grep -v "ffwa-\|\"kdegames\"\|\"kdeutils\"\|\"node\"\|\"platform-tools\"" "$AMDATADIR/$ARCH-apps" | grep -e "$" -c)
	AVAILABLE_TOOLPACKS_NUMBER=$(grep -e "$" -c "$AMCACHEDIR/$ARCH-toolpack")
	AVAILABLE_TOTAL_PROGRAMS=$(("$AVAILABLE_APPS_NUMBER" + "$AVAILABLE_TOOLPACKS_NUMBER"))
	echo " LIST OF THE $AVAILABLE_TOTAL_PROGRAMS PROGRAMS AVAILABLE IN ALL OUR SUPPORTED DATABASES:"
	_list_msg_footer
}

################################################################################
#				USAGE
################################################################################

case "$1" in
	'-a'|'about')
		[ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
		_online_check
		printf "%s\n" "-----------------------------------------------------------------------"
		[ ! -f "$AMCACHEDIR"/version-args ] && _check_version
		entries="$(echo "$@" | cut -f2- -d ' ')" # Removes first argument
		for arg in $entries; do
			if curl -o /dev/null -sIf "$AMCATALOGUEMARKDOWNS/${arg}.md" 1>/dev/null; then
				_about_page "$arg"
			elif curl -Ls "$toolpack_readme" | grep -q "^| $arg "; then
				arg="$arg.toolpack"
				_about_page "$arg"
			elif echo "$arg" | grep -q ".toolpack$"; then
				_about_page "$arg"
			else
				printf " ERROR: %b not found\033[0m\n" "${RED}\"$arg\""
			fi
			printf "%s\n" "-----------------------------------------------------------------------"
		done
		;;

	'-f'|'files')
		if [ "$2" = "--less" ]; then
			_files_number && echo "$FILES_NUMBER"
			[ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && echo "$DIVIDING_LINE" && _files_number && echo "$FILES_NUMBER"
		elif [ "$2" = "--byname" ]; then
			_files_sort_by_name
			[ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && APPMAN_ON=1 && _files_number && [ "$FILES_NUMBER" != 0 ] && echo "$DIVIDING_LINE" && _files_sort_by_name
			_betatester_message_on
		else
			_files_sort_by_size
			[ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && APPMAN_ON=1 && _files_number && [ "$FILES_NUMBER" != 0 ] && echo "$DIVIDING_LINE" && _files_sort_by_size
			_betatester_message_on
		fi
		;;

	'-l'|'list')
		_list
		if [ "$2" = --all ]; then
			_list_toolpacks
			LIST=$(sort "$AMDATADIR/$ARCH-apps" "$AMCACHEDIR/$ARCH-toolpack" | uniq | _pretty_list)
			_list_apps_all_msg | less -Ir
			echo "$DIVIDING_LINE"
			echo " $(("$AVAILABLE_APPS_NUMBER" + "$AVAILABLE_TOOLPACKS_NUMBER")) programs in all supported databases"
			echo "$DIVIDING_LINE"
		elif [ "$2" = --appimages ]; then
			_list_appimages
			LIST=$(sort "$AMCACHEDIR/$ARCH-appimages" | _pretty_list)
			_list_appimages_msg | less -Ir
			echo "$DIVIDING_LINE"
			echo " $AVAILABLE_APPIMAGES_NUMBER AppImages in the \"AM\" database"
			echo "$DIVIDING_LINE"
		elif [ "$2" = --toolpack ]; then
			_list_toolpacks
			LIST=$(sort "$AMCACHEDIR/$ARCH-toolpack" | _pretty_list)
			_list_toolpacks_msg | less -Ir
			echo "$DIVIDING_LINE"
			echo " $AVAILABLE_TOOLPACKS_NUMBER programs in the \"Toolpacks\" database"
			echo "$DIVIDING_LINE"
		else
			LIST=$(sort "$AMDATADIR/$ARCH-apps" | _pretty_list)
			_list_apps_msg | less -Ir
			echo "$DIVIDING_LINE"
			echo " $AVAILABLE_APPS_NUMBER programs in the \"AM\" database"
			echo "$DIVIDING_LINE"
		fi
		;;

	'-q'|'query')
		if [ -z "$2" ]; then
			echo " USAGE: $AMCLI $1 [ARGUMENT]"
			echo "        $AMCLI $1 --all [ARGUMENT]"
			echo "        $AMCLI $1 --appimages [ARGUMENT]"
			echo "        $AMCLI $1 --pkg [ARGUMENT]"
			echo "        $AMCLI $1 --toolpack [ARGUMENT]"; exit 1
		fi
		wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
		if [ "$2" = --pkg ]; then
			entries="$(echo "$@" | cut -f3- -d ' ' | tr -s ' ' '|')"
			printf "\n Search results for packages: %s\n\n"  "$entries" | tr '[:lower:]' '[:upper:]'
			grep -iE "$entries" "$AMDATADIR/$ARCH-apps" --color=always | _pretty_list_compat
		elif [ "$2" = --appimages ]; then
			entries="$(echo "$@" | cut -f3- -d ' ')"
			printf "\n Search results for \"%s\":\n\n"  "$entries" | tr '[:lower:]' '[:upper:]'
			pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
			_list_appimages
			grep -Pi "$pattern" "$AMCACHEDIR/$ARCH-appimages" | _pretty_list_compat
		elif [ "$2" = --toolpack ]; then
			entries="$(echo "$@" | cut -f3- -d ' ')"
			printf "\n Search results for \"%s\":\n\n"  "$entries" | tr '[:lower:]' '[:upper:]'
			pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
			_list_toolpacks
			grep -Pi "$pattern" "$AMCACHEDIR/$ARCH-toolpack" | _pretty_list_compat
		elif [ "$2" = --all ]; then
			entries="$(echo "$@" | cut -f3- -d ' ')"
			printf "\n Search results for \"%s\":\n\n"  "$entries" | tr '[:lower:]' '[:upper:]'
			pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
			_list_toolpacks
			sort "$AMCACHEDIR/$ARCH-toolpack" "$AMDATADIR/$ARCH-apps" | grep -Pi "$pattern" | _pretty_list_compat
		else
			entries="$(echo "$@" | cut -f2- -d ' ')"
			printf "\n Search results for \"%s\":\n\n"  "$entries" | tr '[:lower:]' '[:upper:]'
			pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
			grep -Pi "$pattern" "$AMDATADIR/$ARCH-apps" | _pretty_list_compat
		fi
		printf '\n'
		;;
esac
