#!/usr/bin/env bash

DIR="$( cd "$( dirname "$0" )" && pwd )"
CLI=$(echo $(basename "$0"))

function _am(){
	AMCLI="am"
	AMCLIPATH=$AMCLI
	SUDOCOMMAND="sudo"
	COMPLETIONPATH="/etc/bash_completion.d"
	COMPLETIONFILE="am-completion.sh"
	APPSPATH="/opt"
	AMPATH="$APPSPATH/$AMCLI"
}

function _appman(){
	if test -f ~/.config/appman/appman-config; then
		break 2>/dev/null
	else
		echo -e "-----------------------------------------------------------------------\n>>> Thank you for choosing AppMan!\n-----------------------------------------------------------------------\nBefore proceeding with any task, write the name of the directory in which\nyou will install the programs, for example 'Programs', 'My-apps'... you\ncan also choose a subfolder, for example '.local/AppMan-apps', or whatever\nname you want, as long as it doesn't contain spaces.\n\nThe destination folder will be created in $HOME\n-----------------------------------------------------------------------\n"
		read -r -ep "Please, write the name or the path of your custom application's folder$(echo -e '\n> ')" location
		case $location in
		'.local/bin') echo "Choose different path!" && read -r -ep "Please, write the name or the path of your custom application's folder$(echo -e '\n : ')" location;;
		*) mkdir -p ~/.config/appman; echo $location >> ~/.config/appman/appman-config
		echo -e '-----------------------------------------------------------------------\nOK, you have chosen to install apps in '$HOME'/'$location'\nTo change this edit the configuration file ~/.config/appman/appman-config\n\nIn case of problems, use the option "-h".\n-----------------------------------------------------------------------\nYou are ready! Start installing your favorite apps locally!!\n-----------------------------------------------------------------------'
		exit;;
		esac
	fi
	APPSDIR=$(cat ~/.config/appman/appman-config)
	mkdir -p ~/$APPSDIR/appman/.cache
	mkdir -p ~/.local/bin ~/.local/share/applications ~/.local/share/icons
	AMCLI="appman"
	AMCLIPATH="$DIR/$AMCLI"
	SUDOCOMMAND=""
	COMPLETIONPATH="$HOME"
	COMPLETIONFILE=".bash_completion"
	APPSPATH="$HOME/$APPSDIR"
	AMPATH="$HOME/$APPSDIR/$AMCLI"
}

if [ "$DIR/$CLI" == "/usr/local/bin/am" ] 2>/dev/null; then
	_am
else
	_appman
fi

function _online_check(){
	wget -q --tries=10 --timeout=20 --spider https://github.com
	if [[ ! $? -eq 0 ]]; then
		echo -e '\n '$AMCLI' is offline, please check your internet connection and try again\n'
		exit
	fi
}

AMREPO="https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main"
arch=$(printf '%s\n' "$HOSTTYPE")
currentuser=$(printf '%s\n' "$USER")

for name in "cat" "chmod" "chown" "curl" "grep" "sed" "wget"; do
  test -x "$(command -v $name 2>/dev/null)" \
  || { printf "\n%s\n" 'ERROR: command "'$name'" not found, strongly needed by "'$AMCLI'"!'; deps=1; };
done; [ "$deps" != "1" ] || { printf "\nInstall the above and try again\n\n"; exit 1; }

rm -rf "$AMPATH/options"
cat <<-HEREDOC >> "$AMPATH/options"
	about
	apikey
	backup
	clean
	convert
	dev
	devmode
	download
	files
	install
	list
	lock
	overwrite
	query
	remove
	sync
	template
	test
	unlock
	update
	web
	--disable-completion
	--enable-completion
	--firejail
	--home
	--launcher
	--rollback
	--sandbox
	--user
HEREDOC

function _no_sudo(){
	if [ -n "$SUDO_COMMAND" ]; then 
		printf "\n WARNING: You don't need to use SUDO for this.\n\n"; 
		exit 1; 
	fi
}

function _patch_bashrc_and_profile(){
	if $(! grep -q 'export PATH=$PATH:$(xdg-user-dir USER)/.local/bin' $HOME/.bashrc); then
		echo 'export PATH=$(echo $PATH | tr ":" "\n" | grep -v "/.local/bin" | tr "\n" ":" | sed ''s/.$//'')' >> $HOME/.bashrc
		echo -e 'export PATH=$PATH:$(xdg-user-dir USER)/.local/bin\n' >> $HOME/.bashrc
	fi
	if $( grep -q 'PATH="$HOME/.local/bin:$PATH"' $HOME/.profile); then
		sed -i 's#PATH="$HOME/.local/bin:$PATH"#PATH="$PATH:$HOME/.local/bin"#g' $HOME/.profile
	fi
}

if [ $AMCLI == appman ] 2>/dev/null; then
	_no_sudo
	_patch_bashrc_and_profile
fi

if test -f $AMPATH/ghapikey.txt; then
		HeaderAuthWithGITPAT=' --header "Authorization: token '$(cat $AMPATH/ghapikey.txt)'" '
fi

case "$1" in

  '') printf "\n USAGE: '$AMCLI [option]'\n        '$AMCLI [option] [argument]'\n\n Run the '$AMCLI -h' command to find out more\n\n";;

  '-h'|'help') printf '
               _____                    _____ 
              /\    \                  /\    \		   A	   A
             /::\    \                /::\____\		    P	    M
            /::::\    \              /::::|   |		     P
           /::::::\    \            /:::::|   |		      M	      &
          /:::/\:::\    \          /::::::|   |		       A
         /:::/__\:::\    \        /:::/|::|   |		        N
        /::::\   \:::\    \      /:::/ |::|   |
       /::::::\   \:::\    \    /:::/  |::|___|______
      /:::/\:::\   \:::\    \  /:::/   |::::::::\    \
     /:::/  \:::\   \:::\____\/:::/    |:::::::::\____\
     \::/    \:::\  /:::/    /\::/    / ~~~~~/:::/    /
      \/____/ \:::\/:::/    /  \/____/      /:::/    /
               \::::::/    /               /:::/    /
                \::::/    /               /:::/    /
                /:::/    /               /:::/    /╔═╗╔╗╔┌─┐┌─┐┌─┐┬─┐
               /:::/    /               /:::/    / ╠═╣║║║├─┤│ ┬├┤ ├┬┘
              /:::/    /               /:::/    /  ╩ ╩╝╚╝┴ ┴└─┘└─┘┴└─
             /:::/    /╔═╗╔═╗┬  ┬┌─┐┌─┐┌┬┐┬┌─┐┌┐┌
             \::/    / ╠═╝╠═╝│  ││  ├─┤ │ ││ ││││
              \/____/  ╩  ╩  ┴─┘┴└─┘┴ ┴ ┴ ┴└─┘┘└┘	by Ivan Alex HC

  >> 𝘋𝘢𝘵𝘢𝘣𝘢𝘴𝘦 & 𝘴𝘰𝘭𝘶𝘵𝘪𝘰𝘯𝘴 𝘧𝘰𝘳 𝘢𝘭𝘭 𝘈𝘱𝘱𝘐𝘮𝘢𝘨𝘦𝘴 𝘢𝘯𝘥 𝘱𝘰𝘳𝘵𝘢𝘣𝘭𝘦 𝘢𝘱𝘱𝘴 𝘧𝘰𝘳 𝘎𝘕𝘜/𝘓𝘪𝘯𝘶𝘹 << 

 ##########################################################################
 
 𝙋𝙇𝙀𝘼𝙎𝙀 𝙎𝙃𝘼𝙍𝙀 𝙏𝙃𝙄𝙎 𝙋𝙍𝙊𝙅𝙀𝘾𝙏 & 𝙎𝙋𝙍𝙀𝘼𝘿 𝙄𝙏 𝙏𝙊 𝙏𝙃𝙀 𝙇𝙄𝙉𝙐𝙓 𝙒𝙊𝙍𝙇𝘿❗❗❗
 
 𝙃𝙀𝙇𝙋 𝙈𝙀 𝙆𝙀𝙀𝙋 𝙏𝙃𝙄𝙎 𝙋𝙍𝙊𝙅𝙀𝘾𝙏 𝘼𝙇𝙄𝙑𝙀❗❗❗
 
 This AppImage package management method can help developers spread their
 creations to the masses, improve them via bug report, and build new ones.
 
 My goal here is to provide a centralized system for finding otherwise
 unobtainable AppImage packages, thus giving them a home to stay.
 
 Share the repository https://github.com/ivan-hc/AM-Application-Manager

 Or the catalog on the website https://portable-linux-apps.github.io

 It does not cost you anything, for us its a great help that can help us
 save the AppImage packaging format, inviting new developers to join the
 development, build and improve more and more packages!
 
 ████████╗██╗░░██╗░█████╗░███╗░░██╗██╗░░██╗  ██╗░░░██╗░█████╗░██╗░░░██╗██╗
 ╚══██╔══╝██║░░██║██╔══██╗████╗░██║██║░██╔╝  ╚██╗░██╔╝██╔══██╗██║░░░██║██║
 ░░░██║░░░███████║███████║██╔██╗██║█████═╝░  ░╚████╔╝░██║░░██║██║░░░██║██║
 ░░░██║░░░██╔══██║██╔══██║██║╚████║██╔═██╗░  ░░╚██╔╝░░██║░░██║██║░░░██║╚═╝
 ░░░██║░░░██║░░██║██║░░██║██║░╚███║██║░╚██╗  ░░░██║░░░╚█████╔╝╚██████╔╝██╗
 ░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝╚═╝░░╚═╝  ░░░╚═╝░░░░╚════╝░░╚═════╝░╚═╝
 
 ##########################################################################
 __________________________________________________________________________
 
 				USAGE:
 
 	'$AMCLI' {OPTION}
 
 	'$AMCLI' {OPTION} {PROGRAM}
 __________________________________________________________________________
 
 ##########################################################################
 __________________________________________________________________________
 
 				OPTIONS 
 
 		(standard, for both "AM" AND "APPMAN")
 __________________________________________________________________________
 __________________________________________________________________________
 
 -a, about
 
 SYNOPSIS:	-a {PROGRAM}
 
 DESCRIPTION: 	Shows more info about one or more apps, all the pages are 
 
 downloaded from https://portable-linux-apps.github.io
  __________________________________________________________________________
 
 -b, backup
 
 SYNOPSIS:	-b {PROGRAM}
 
 DESCRIPTION:	Save the current version of one or more apps, each snapshot 
 
 is stored into a dedicated directory, under $HOME/.am-snapshots/$PROGRAM
 
 To restore the snapshots see the "-o" option.
 __________________________________________________________________________
 
 -c, clean
 
 SYNOPSIS:	-c
 
 DESCRIPTION:	Removes all the unnecessary files and folders.
 __________________________________________________________________________
 
 -d, download
 
 SYNOPSIS:	-d {PROGRAM}
 
 DESCRIPTION:	Download one or more installation scripts to your desktop.
 __________________________________________________________________________
 
 -f, files
 
 SYNOPSIS:	-f
 
 DESCRIPTION:	Shows the list of all installed programs, with sizes.
 __________________________________________________________________________
 
 -h, help
 
 SYNOPSIS:	-h
 
 DESCRIPTION:	Prints this message.
 __________________________________________________________________________
 
 -H, --home
 
 SYNOPSIS:	-H {PROGRAM}
 
 DESCRIPTION:	Set a dedicated $HOME directory for one or more AppImages.
 __________________________________________________________________________
 
 -i, install
 
 SYNOPSIS:	-i {PROGRAM}
 
 DESCRIPTION:	Install one or more programs.
 __________________________________________________________________________
 
 -l, list
 
 SYNOPSIS:	-l
 
 DESCRIPTION:	Shows the list of all the apps available in the repository.
 __________________________________________________________________________
 
 -o, overwrite
 
 SYNOPSIS:	-o {PROGRAM}
 
 DESCRIPTION:	Overwrite the existing version of the app with a snapshot
 
 saved previously (see the option "-b", above).
 __________________________________________________________________________
 
 -q, query
 
 SYNOPSIS:	-q {KEYWORD}
 
 DESCRIPTION:	Can be used to search for keywords and terms in the list 
 
 of available applications to display matches. This can be useful if you 
 
 are looking for applications having a specific feature.
 __________________________________________________________________________
 
 -r, remove
 
 SYNOPSIS:	-r {PROGRAM}
 
 DESCRIPTION:	Removes one or more apps, requires confirmation.
 __________________________________________________________________________
 
 -R
 
 SYNOPSIS:	-R {PROGRAM}
 
 DESCRIPTION:	Removes one or more apps without asking.
 __________________________________________________________________________
 
 -s, sync
 
 SYNOPSIS:	-s
 
 DESCRIPTION:	Updates this script to the latest version hosted.
 __________________________________________________________________________
 
 -t, template
 
 SYNOPSIS:	-t {PROGRAM}
 
 DESCRIPTION:	This option allows you to generate a custom script from a 
 
 list of different templates that may be vary according to the kind of app 
 
 you want to upload to the "AM" repo, and the source where it is available.
 
 You can install it using the "'$AMCLI' test /path/to/your-script" command.
 __________________________________________________________________________
 
 -u, -U, update
 
 SYNOPSIS:	-u
 		-u {PROGRAM}
 
 DESCRIPTION: Update all the apps or just one.
 __________________________________________________________________________
 
 -v, version
 
 SYNOPSIS:	-v
 
 DESCRIPTION:	Shows the version.
 __________________________________________________________________________
 
 -w, web
 
 SYNOPSIS:	-w
 
 DESCRIPTION:	Shows the URLs of the sites/sources of then app.
 __________________________________________________________________________
 
 --disable-completion
 
 SYNOPSIS:	--disable-completion
 
 DESCRIPTION:	Disable bash-completion.
 __________________________________________________________________________
 
 --enable-completion
 
 SYNOPSIS:	--enable-completion
 
 DESCRIPTION:	Enable bash-completion to complete a keyword with the "TAB"
 
 key, using the names of all installable applications available.
 __________________________________________________________________________
 
 --firejail, --sandbox
 
 SYNOPSIS:	--firejail {PROGRAM}
 
 DESCRIPTION:	Run an AppImage in a sandbox using Firejail.
 __________________________________________________________________________

 --launcher
 
 SYNOPSIS:	--launcher /path/to/${APPIMAGE}

 DESCRIPTION:	Embed one or more local AppImages in the applications menu.
 
 I suggest dragging the files into the terminal to get the desired effect.
 
 Launchers are located in ~/.local/share/applications/AppImages by default.
 __________________________________________________________________________

 --rollback
 
 SYNOPSIS:	--rollback {PROGRAM}

 DESCRIPTION:	Download an older or specific version of the software you 
 
 are interested in (only works with Github).
 __________________________________________________________________________
 
 apikey
 
 SYNOPSIS:	apikey {Github Token}
 		apikey delete
 
 DESCRIPTION:	Get unlimited access to https://api.github.com using your 
 
 personal access tokens. The configuration file named "ghapikey.txt" will 
 
 be saved in '$AMPATH' .
 
 Use the command "'$AMCLI' apikey delete/del/remove" to remove the file.
 __________________________________________________________________________
 
 dev, devmode
 
 SYNOPSIS:	dev off
 		dev on
 
 DESCRIPTION:	View the installer output during installation, use "on".
 
 It can be disabled with "off" or with the "-s" and "-u" options.
 __________________________________________________________________________
 
 
 lock
 
 SYNOPSIS:	lock {PROGRAM}
 
 DESCRIPTION:	Lock the selected app to the current version installed, 
 
 only works if exists a dedicated "AM-updater" installed with the app.
 __________________________________________________________________________
 
 test
 
 SYNOPSIS:	test {FILE}
 
 DESCRIPTION:	Install a local installation script, or test your own.
 __________________________________________________________________________
 
 unlock
 
 SYNOPSIS:	unlock {PROGRAM}
 
 DESCRIPTION:	Unlock updates for the selected program. This option nulls 
 
 the option "lock" (see above).
 __________________________________________________________________________
 __________________________________________________________________________

 			       EXTRA OPTIONS 
 __________________________________________________________________________

 conv, convert				( only works with "AppMan" )
 
 SYNOPSIS:	conv {PROGRAM}
 
 DESCRIPTION:	Downloads the installation scripts for "AM" and converts 
 
 them to rootless installation scripts that can be installed locally. 
 __________________________________________________________________________
 
 --user, appman				( only works with "AM" )
 
 SYNOPSIS:	--user

 DESCRIPTION:	Run "AM" as an unprivileged user making it act as "AppMan".
 __________________________________________________________________________

 ##########################################################################
 __________________________________________________________________________
 
 		     ABOUT "AM" APPLICATION MANAGER
 __________________________________________________________________________
 
 "AM" is a software manager that deals with the management of all portable 
 
 apps and AppImages out there. It is distributed in two versions:
 
 ◆ "AM", the main version that integrates applications at the system level, 
 
 installing them in /opt, for all the users;
 
 ◆ "AppMan", the portable and "rootless" version that installs all the apps 
 
 in the $HOME directory, integrating additional options to convert all the 
 
 installation scripts from "AM".
 
 __________________________________________________________________________
 
 				  SITES
 __________________________________________________________________________
 
 HTTPS://PORTABLE-LINUX-APPS.GITHUB.IO 	(main website)
 
 HTTPS://GITHUB.COM/IVAN-HC/APPMAN
 
 HTTPS://GITHUB.COM/IVAN-HC/AM-APPLICATION-MANAGER
 __________________________________________________________________________

 ##########################################################################' | less;;

  '-a'|'about')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f "$AMPATH/.cache/about-args";
		printf "%s\n" "$@" | tr ' ' '\n' >> "$AMPATH/.cache/about-args" && printf "STOP\n" >> "$AMPATH/.cache/about-args";
		ARGS="$(tail -n +2 $AMPATH/.cache/about-args)"

		for arg in $ARGS; do
			for var in "$arg"; do

				function _about(){
					markdown="https://raw.githubusercontent.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/main/apps/$arg.md"
					if curl -o /dev/null -sIf "$markdown" 1>/dev/null; then 
					printf "%s\n" "-----------------------------------------------------------------------"

					case "$arg" in
					*) 
						rm -rf "$AMPATH/.cache/about"; 
						mkdir -p "$AMPATH/.cache/about";
						cd "$AMPATH/.cache/about" && wget -q "$markdown" &&
						sed -i '1,${ /^\s*#/ d; /^\s*!/ d; /\[Applications]/d; /\ --- /d; }' "$arg.md";
						printf "%s\n" " PACKAGE: $(echo $arg | tr a-z A-Z)";
							if test -f "$APPSPATH/$arg/remove"; then 
							printf "%s\n" " STATUS: INSTALLED, ABOUT $(du -sm $APPSPATH/$arg | cut -f1) MB OF DISK SPACE IN USE";
						else 
							printf "%s\n" " STATUS: NOT INSTALLED";
						fi
							cat ./$arg.md && printf "%s\n" "-----------------------------------------------------------------------";
						;;
					esac
				else
					printf "%s\n" " \"$arg\" is not a valid argument" | tr 'a-z' 'A-Z';
				fi
				}
				if [ "$arg" == "STOP" ]; then
					exit 1
				else
					_about
				fi
			done
		done
	done

	shift
	;;

  '-b'|'backup')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/backup-args;
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/backup-args && echo STOP >> $AMPATH/.cache/backup-args;
		ARGS=$(tail -n +2 $AMPATH/.cache/backup-args)

		for arg in $ARGS; do
			for var in $arg; do

				function _backup(){
					case $arg in
						'') 
							echo " Specify the name of a program to back up"; echo ""; exit;;
						*) 
							if test -f $APPSPATH/$arg/remove; then
								echo ""; read -p " Do you wish to backup the current version of $arg (y,N)?" yn
								case $yn in
								[Yy]* ) 
									mkdir -p "$(xdg-user-dir USER)/.am-snapshots/$arg"; cp -r $APPSPATH/$arg "$(xdg-user-dir USER)/.am-snapshots/$arg/$(date +%F-%X | sed 's/://g' | sed 's/-//g')"; echo ""; echo " SAVED in $(xdg-user-dir USER)/.am-snapshots/$arg"; echo "";;
								[Nn]*|* ) 
									echo ""; echo " OPERATION ABORTED!"; echo "";;
								esac
							else
								echo ""; echo " '$arg' is not a valid argument or is not installed."; echo ""; break;
							fi
						esac
				}

				if [ $arg == STOP ]; then
					exit
				else
					_backup
				fi
			done
		done
	done

	shift
	;;

  '-c'|'clean')

  	_no_sudo

	function _clean_launchers(){
		for var in $HOME/.local/share/applications/AppImages/*.desktop; do
			for arg in "$var"; do
				APPIMAGENAME=$(grep "Exec=" 0<"$var" 2>/dev/null | head -1 | cut -c 6- | sed 's/\s.*$//')
				MOUNTPOINTS=$(echo "$APPIMAGENAME" | cut -d'/' -f1-4)
				if $( ! test -f "$APPIMAGENAME" 2> /dev/null); then
					if $(! test -d "$(echo $MOUNTPOINTS)" 2> /dev/null); then
						if $(echo $MOUNTPOINTS | grep -q "/media/"); then
							echo -e "\n 💀ERROR: cannot remove $(basename $var)\n   related AppImage is located in an unmounted path of /media\n"
						elif $(echo $MOUNTPOINTS | grep -q "/mnt/"); then
							echo -e "\n 💀ERROR: cannot remove $(basename $var)\n   related AppImage is located in an unmounted path of /mnt\n"
						fi
					else
						rm -f "$var"
					fi
				fi
			done
		done
	}

  	function _clean_all(){
  		echo -e "\n Cleaning of temporary files and folders in progress...\n" && sleep 0.1 &&
  		for i in {1000..0000}; do 
			echo -ne " $i\r" && sleep 0.0001
		done
  		rm -R -f $AMPATH/.cache/* | echo " ◆ Cleaning up the content of $AMPATH/.cache"
  		rm -R -f $APPSPATH/*/tmp | echo ' ◆ Removed all '$APPSPATH'/*/tmp directories'
  		rm -R -f $AMPATH/appdb | echo " ◆ Removed old $AMPATH/appdb directory, if exists"
  		_clean_launchers && echo ' ◆ Removed orphaned launchers produced with the "--launcher" option'
  		echo -e "\n DONE!\n"
  	}

  	_clean_all
	;;

  '-conv'|'convert')

  	_no_sudo

  	while [ -n "$1" ]; do

  		if [ $AMCLI == am ] 2>/dev/null; then
  			echo " This function only works for AppMan"
  			exit
  		fi

		rm -f $AMPATH/.cache/convert-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/convert-args && echo STOP >> $AMPATH/.cache/convert-args
		ARGS=$(tail -n +2 $AMPATH/.cache/convert-args)

		case $2 in
		'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
		esac

		for arg in $ARGS; do
			for var in $arg; do

				function _convert(){
					case $arg in
					'') 	echo " Specify the name of one or more programs to convert"; echo ""; break;;
					*) 
						function _convert_to_appman_compatible_script(){
							echo " Converting $arg to an AppMan-compatible script"
  							sed -i "s# /usr/local/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
							sed -i "s# /usr/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
							sed -i "s# /usr/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
							sed -i "s# /usr/local/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
							sed -i "s# /usr/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
							sed -i "s# /usr/local/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
							sed -i "s# /usr/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
							sed -i "s# /usr/local/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
							sed -i "s# /opt# $APPSPATH#g" ./$arg
							sed -i "s#/opt/#$APPSPATH/#g" ./$arg
							sed -i "s# https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" ./$arg
							echo -e '\n# CHANGE THE PATH OF ICONS AND EXECUTABLES IN LOCALLY INSTALLED AM*.DESKTOP FILES\nAPPSDIR=$(cat ~/.config/appman/appman-config)\nsed -i "s#Icon=/opt#Icon=$(xdg-user-dir USER)/$APPSDIR#g" ~/.local/share/applications/AM*.desktop\nsed -i "s#Exec=$APP#Exec=$(xdg-user-dir USER)/.local/bin/$APP#g" ~/.local/share/applications/AM*.desktop\nsed -i "s#Exec=/usr/bin/#Exec=$(xdg-user-dir USER)/.local/bin/#g" ~/.local/share/applications/AM*.desktop\nsed -i "s#Exec=/opt/firefox#Exec=$(xdg-user-dir USER)/$APPSDIR/firefox#g" ~/.local/share/applications/AM*.desktop\nsed -i "s#Exec=/opt/kdegames#Exec=$(xdg-user-dir USER)/$APPSDIR/kdegames#g" ~/.local/share/applications/AM*.desktop\nsed -i "s#Exec=/opt/kdeutils#Exec=$(xdg-user-dir USER)/$APPSDIR/kdeutils#g" ~/.local/share/applications/AM*.desktop' >> ./$arg
						}

						if curl --output /dev/null --silent --head --fail $AMREPO/programs/$arch/$arg  1>/dev/null; then
  							_online_check
  							cd $(xdg-user-dir DESKTOP); wget -q $AMREPO/programs/$arch/$arg
  							_convert_to_appman_compatible_script
  						elif test -f $(xdg-user-dir DESKTOP)/am-scripts/$arch/$arg; then
  							cd $(xdg-user-dir DESKTOP); cp ./am-scripts/$arch/$arg ./$arg
  							_convert_to_appman_compatible_script
						else
							echo ""; echo ' "'$arg'" is NOT a valid argument'; echo ""
						fi
					esac
				}

				if [ $arg == STOP ]; then
					exit
				else
					_convert
				fi
			done
		done
	done

	shift
	;;

  '-d'|'download')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/download-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/download-args && echo STOP >> $AMPATH/.cache/download-args
		ARGS=$(tail -n +2 $AMPATH/.cache/download-args)

		for arg in $ARGS; do
			for var in $arg; do

			function _download(){
				case $arg in
				*) 
					if curl --output /dev/null --silent --head --fail $AMREPO/programs/$arch/$arg  1>/dev/null; then
						echo ""; echo ' "'$arg'" installation script downloaded! ' | tr a-z A-Z
						cd $(xdg-user-dir DESKTOP); wget -q $AMREPO/programs/$arch/$arg; echo ""
					elif curl --output /dev/null --silent --head --fail $AMREPO/testing/$arch/$arg  1>/dev/null; then
						echo ""; echo '  "'$arg'" downloaded from "testing", the unstable branch ' | tr a-z A-Z; echo ""; echo ' WARNING! PROGRAMS COMING FROM "TESTING" ARE BROKEN, USE AT YOUR OWN RISK!'
						cd $(xdg-user-dir DESKTOP); wget -q $AMREPO/testing/$arch/$arg; echo ""
					else
						echo ""; echo ' "'$arg'" is NOT a valid argument, check the list (command "'$AMCLI' -l")'; echo ""
					fi
				esac
			}

				if [ $arg == STOP ]; then
					exit
				else
					_download
				fi
			done
		done
	done

	shift
	;;

  '-f'|'files')

  	_no_sudo

  	function _files(){
		rm -f $AMPATH/.cache/files-args
		echo ""; echo $(echo "YOU HAVE INSTALLED "; cd $APPSPATH && find . -type f -name 'remove' 2>/dev/null | sed -r 's|/[^/]+$||' | sort | uniq | wc -l
		echo " STANDALONE PROGRAMS MANAGED BY '$(echo $AMCLI | tr a-z A-Z)':"); echo ""
		cd $APPSPATH &&
		APP=$(find -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * | sort -rh | sed 's@.*	@@')
		SIZE=$(find -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * | sort -rh)
		echo "- APPNAME | TYPE | SIZE " >> $AMPATH/.cache/files-args
		echo "- ------- | ---- | ----" >> $AMPATH/.cache/files-args
		for arg in $APP; do
			 for var in $arg; do
			 	if ! test -f ./$var/remove 2>/dev/null; then
			 		break
			 	fi
			 	if [ -z $(strings -d "./$var/$var" 2>/dev/null | grep -m 1 "AppImage") ] 2>/dev/null; then
    					echo " ◆ $var	|	Other	|	$(echo "$SIZE" | grep $var | cut -f1 -d"	" | sed 's/.$/ &/' | sed 's/$/iB/')" >> $AMPATH/.cache/files-args
    				else
  					echo " ◆ $var	|	AppImage	|	$(echo "$SIZE" | grep $var | cut -f1 -d"	" | sed 's/.$/ &/' | sed 's/$/iB/')" >> $AMPATH/.cache/files-args
    				fi
			 	
			 done
		done
		cat $AMPATH/.cache/files-args | column -t
		echo ""
	}

	_files

	;;

  '-H'|'--home')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

    	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/home-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/home-args && echo STOP >> $AMPATH/.cache/home-args
		ARGS=$(tail -n +2 $AMPATH/.cache/home-args)

		for arg in $ARGS; do
			for var in $arg; do

				function _home(){
					cd $APPSPATH
					if ! test -d ./$arg; then
						echo ' ERROR: "'$arg'" is not installed'
					else
						case $arg in
    						*)
    							if [ -z $(strings -d "./$arg/$arg" 2>/dev/null | grep -m 1 "AppImage") ] 2>/dev/null; then
    								echo ' ERROR: "'$arg'" is NOT an AppImage'
    							else
  								cd ./$arg
    								mkdir -p ./$arg.home
    								echo ' Setting $HOME to '$APPSPATH'/'$arg'/'$arg'.home for this AppImage'
    							fi
    						esac
    					fi
				}

				if [ $arg == STOP ]; then
					exit
				else
					_home
    				fi
    			done
    		done
    	done
	;;

  '-i'|'install')

	_online_check

	while [ -n "$1" ]; do

		case $2 in
		'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
		esac

		if [ $AMCLI == am ] 2>/dev/null; then
  			$SUDOCOMMAND echo "" 2>/dev/null
  			if ! $SUDOCOMMAND -n true 2>/dev/null; then
				exit
  			fi
  		fi

		echo -e "-----------------------------------------------------------------------\n >> START OF ALL INSTALLATION PROCESSES <<\n-----------------------------------------------------------------------"
		rm -f $AMPATH/.cache/install-args
		rm -R -f $AMPATH/.cache/installed
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/install-args 
		echo STOP >> $AMPATH/.cache/install-args
		ARGS=$(tail -n +2 $AMPATH/.cache/install-args)

		for arg in $ARGS; do
			for var in $arg; do

				function _install(){
					echo ""
					case $arg in

					*) 
						if test -f $APPSPATH/$arg/remove; then
							echo ' ERROR: "'$arg'" is already installed, please try to remove it first' && echo ""; break;
						else
							if curl --output /dev/null --silent --head --fail $AMREPO/programs/$arch/$arg  1>/dev/null; then
								case $arg in
								*) 
									for var in $arg; do
										cd $AMPATH/.cache; mkdir -p tmp; cd tmp; wget -q $AMREPO/programs/$arch/$arg; cd ..; mv ./tmp/$arg ./$arg; rmdir ./tmp
										chmod a+x ./$arg

										if [ $arg == appman ]; then
											if [ $AMCLI == appman ] 2>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL APPMAN USING APPMAN'
												break
											fi
										fi

										if grep -q '.deb' ./$arg; then
											if ! command -v ar 1>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL "'$(echo '"'$arg'"' | tr a-z A-Z)'" WITHOUT INSTALLING "BINUTILS"!'; break
											fi
										fi

										if grep -q 'ffwa-' ./$arg; then
											if ! command -v firefox 1>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL "'$(echo '"'$arg'"' | tr a-z A-Z)'" WITHOUT INSTALLING "FIREFOX"!'; break
											fi
										fi

										if grep -q '.tar' ./$arg; then
											if ! command -v tar 1>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL "'$(echo '"'$arg'"' | tr a-z A-Z)'" WITHOUT INSTALLING "TAR"!'; break
											fi
										fi

										if grep -q '.zip' ./$arg; then
											if ! command -v unzip 1>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL "'$(echo '"'$arg'"' | tr a-z A-Z)'" WITHOUT INSTALLING "UNZIP"!'; break
											fi
										fi

										if grep -q 'zsync /opt' ./$arg; then
											if ! command -v zsync 1>/dev/null; then
												echo ' 💀 ERROR: YOU CANNOT INSTALL "'$(echo '"'$arg'"' | tr a-z A-Z)'" WITHOUT INSTALLING "ZSYNC"!'; break
											fi
										fi

										function _install_common_patch(){
											sed -i "s#wget #wget -q --show-progress #g" ./$arg; sed -i 's#$(wget -q --show-progress #$(wget -q #g' ./$arg; sed -i "s# -q -q # -q #g" ./$arg
											sed -i 's/currentuser=/#currentuser=/g' ./$arg
											sed -i 's/chown -R $currentuser/#chown -R $currentuser/g' ./$arg
											sed -i '/# MESSAGE/,$d' ./$arg
											sed -i "s# https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" ./$arg
										}

										function _install_appman_local_patch(){
											sed -i "s# /opt# $APPSPATH#g" ./$arg
											sed -i "s#/opt/#$APPSPATH/#g" ./$arg
											sed -i "s# /usr/local/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
											sed -i "s# /usr/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
											sed -i "s# /usr/local/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
											sed -i "s# /usr/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
											sed -i "s# /usr/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
											sed -i "s# /usr/local/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
											sed -i "s# /usr/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
											sed -i "s# /usr/local/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
										}

										if [ $AMCLI == appman ] 2>/dev/null; then
											_install_common_patch
											_install_appman_local_patch
										else
											if [[ $(grep "[[:space:]]ro[[:space:],]" /proc/mounts) ]]; then
												$SUDOCOMMAND mkdir -p /usr/local/share/applications
												sed -i 's#/usr/share/applications#/usr/local/share/applications#g' ./$arg
											fi
											_install_common_patch
										fi

										echo -ne " ◆ RUNNING THE INSTALLATION SCRIPT FOR $(echo '"'$arg'"' | tr a-z A-Z)... \r" &&
										$SUDOCOMMAND ./$arg | less -E && echo -ne "                                                                          \r" &&
										echo -ne " ◆ CONFIGURING $(echo '"'$arg'"' | tr a-z A-Z)...\r" && echo -ne "                                                                          \r" &&
										$SUDOCOMMAND chown -R $currentuser $APPSPATH/$(cd $APPSPATH && ls -td * | head -1) 2> /dev/null
										if test -f $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/AM-updater; then
											mkdir $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/.am-installer 2> /dev/null &&
											wget -q $AMREPO/programs/$arch/$arg -O $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/.am-installer/$arg
										fi &&

										if test -d $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons; then
											find $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons/* -xtype l -delete 2> /dev/null
											if [ -z "$(ls -A $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons)" ]; then
   												wget -q https://raw.githubusercontent.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/main/icons/$arg.png -O $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons/$arg
											fi 
										fi &&

										if [ $AMCLI == appman ] 2>/dev/null; then
											sed -i "s#Icon=/opt#Icon=$(xdg-user-dir USER)/$APPSDIR#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
											sed -i "s#Exec=$arg#Exec=$(xdg-user-dir USER)/.local/bin/$arg#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
											sed -i 's#Exec=$APP#Exec='$(xdg-user-dir USER)'/.local/bin/$APP#g' ~/.local/share/applications/AM*.desktop 2> /dev/null
											sed -i "s#Exec=/usr/bin/#Exec=$(xdg-user-dir USER)/.local/bin/#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
											sed -i "s#Exec=/opt/#Exec=$(xdg-user-dir USER)/$APPSDIR/#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
										fi

										echo -ne " ◆ $(echo '"'$arg'"' | tr a-z A-Z) INSTALLED ($(du -sm $APPSPATH/$(cd $APPSPATH && ls -td * | head -1) | awk '{print $1}' ) MB OF DISK SPACE)\n"
										$SUDOCOMMAND rm $AMPATH/.cache/$arg
										unset $LATESTDIR
										LATESTDIR=$(cd $APPSPATH && ls -td * | head -1)

										if test -f $APPSPATH/$LATESTDIR/remove; then
											if test -d $APPSPATH/$LATESTDIR/tmp; then
												echo -e " 💀 ERROR DURING INSTALLATION, REMOVED $(echo '"'$arg'"' | tr a-z A-Z)!"
												$AMCLIPATH -R $LATESTDIR 1> /dev/null
											else
												echo " ◆ $LATESTDIR" >> $AMPATH/.cache/installed &&
												chown -R $currentuser $AMPATH/.cache/about 2> /dev/null
											fi
										else
											echo " INSTALLATION ABORTED!"
										fi
										break; done
								esac
							else
								echo ' 💀 "'$arg'" is NOT a valid argument, please try again!'
							fi
						fi
					esac
				}

				if [ $arg == STOP ]; then
					if test -f $AMPATH/.cache/installed; then
						echo -e "\n-----------------------------------------------------------------------\n >> END OF ALL INSTALLATION PROCESSES <<\n-----------------------------------------------------------------------\n\n THE FOLLOWING NEW PROGRAMS HAVE BEEN INSTALLED:\n"
						echo -e "\n-----------------------------------------------------------------------" >> $AMPATH/.cache/installed
						grep -w -v "◆ am" 0<$AMPATH/.cache/installed; 
						exit
					else
						exit
					fi
				else
					_install
				fi
			done
		done
	done

	shift
	;;

  '-l'|'list')

  	_no_sudo

	function _list(){
		wget -q --tries=10 --timeout=20 --spider https://github.com
		if [[ $? -eq 0 ]]; then
			rm -R -f $AMPATH/$arch-apps $AMPATH/list
			wget -q $AMREPO/programs/$arch-apps -P $AMPATH
		fi
		echo -e "$(awk -v FS="(◆ | : )" '{print $2}' 0<$AMPATH/$arch-apps)" >> $AMPATH/list
		cd $AMPATH && cat options >> list 2>/dev/null
		MESSAGE=$(echo " YOU HAVE INSTALLED $(cd $APPSPATH && find -name 'remove' -printf "%h\n" 2>/dev/null | sort -u | wc -l) APPLICATIONS OUT OF $(grep -e "$" -c $AMPATH/$arch-apps) AVAILABLE")
		echo -e "\n$MESSAGE\n\n LIST OF THE $(grep -e "$" -c $AMPATH/$arch-apps) APPLICATIONS AVAILABLE IN THE 'AM' REPOSITORY:\n\n$(cat $AMPATH/$arch-apps)" | less -I
		echo -e "\n $MESSAGE\n"
	}

	_list
	;;

  '-o'|'overwrite')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/overwrite-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/overwrite-args && echo STOP >> $AMPATH/.cache/overwrite-args
		ARGS=$(tail -n +2 $AMPATH/.cache/overwrite-args)

		for arg in $ARGS; do
			for var in $arg; do

				function _overwrite(){
					case $arg in
					$AMCLI) 
						$AMCLIPATH -s; break;;
					'') 
						echo " Specify the name of a program to overwrite"; echo "";;
					*) 
						if test -f $APPSPATH/$arg/remove; then
							echo ""; read -p " Do you wish to overwrite this version of $arg with an old one (y,N)?" yn
							case $yn in
							[Yy]* ) 
								echo ""; printf " Please, select a snapshot or press CTRL+C to abort:\n"; sleep 1;
								select d in $(xdg-user-dir USER)/.am-snapshots/$arg/*; do test -n "$d" && break; echo ">>> Invalid Selection"; done
								cp -r --backup=t $d/* $APPSPATH/$arg/
								rm -R -f ./tmp $APPSPATH/$arg/*~
	  							echo " RESTORE COMPLETED SUCCESSFULLY!"; echo "";;
							[Nn]*|* ) 
								echo ""; echo " OPERATION ABORTED!"; echo "";;
							esac
						else
							echo ""; echo " '$arg' is not a valid argument or is not installed."; echo ""
						fi
					esac
				}

				if [ $arg == STOP ]; then
					exit
				else
					_overwrite
				fi
			done
		done
	done

	shift
	;;

  '-q'|'query')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

  	wget -q --tries=10 --timeout=20 --spider https://github.com
	if [[ $? -eq 0 ]]; then
		rm -R -f $AMPATH/$arch-apps $AMPATH/list
		wget -q $AMREPO/programs/$arch-apps -P $AMPATH
	fi
  	echo -e "$(awk -v FS="(◆ | : )" '{print $2}' 0<$AMPATH/$arch-apps)" >> $AMPATH/list
	cd $AMPATH && cat options >> list
	ARGS=$(echo "$@" | sed 's/-q //')
	echo ""
	echo ' Search results for "'$ARGS'" in the list of available programs:' | tr a-z A-Z
	echo ""
	grep -i -E "$2" $AMPATH/$arch-apps | grep -i -E "$3" | grep -i -E "$4" | grep -i -E "$5" | grep -i -E "$6" | grep -i -E "$7" | grep -i -E "$8" | grep -i -E "$9"
	echo ""
	exit
	;;

  '-r'|'remove')

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/remove-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/remove-args && echo STOP >> $AMPATH/.cache/remove-args
		ARGS=$(tail -n +2 $AMPATH/.cache/remove-args)

		case $2 in
		'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
		esac

		for arg in $ARGS; do
			for var in $arg; do

				function _remove(){
					if ls $APPSPATH/$arg > /dev/null 2>&1; then
						case $arg in
						*) 
							for var in $arg; do
								$SUDOCOMMAND echo "" > /dev/null; read -p ' ◆ DO YOU WISH TO REMOVE "'$(echo $arg | tr a-z A-Z)'" (Y,n)?' yn

								case $yn in
								[Nn]* ) 
									echo ' - "'$(echo $arg | tr a-z A-Z)'" HAS NOT BEEN REMOVED!'; echo "";;
								[Yy]*|* ) 
									$SUDOCOMMAND $APPSPATH/$arg/remove && echo ' ◆ "'$(echo $arg | tr a-z A-Z)'" HAS BEEN SUCCESSFULLY REMOVED!' && break;;
								esac
							done;;
						esac
					else
						echo '"'$arg'",' >> $AMPATH/.cache/unavailable-args
					fi
				}

				if [ $arg == STOP ]; then
					if test -f $AMPATH/.cache/unavailable-args; then
						echo -e "-----------------------------------------------------------------------"
						echo -e " ◆ ERROR: $(echo $(cat $AMPATH/.cache/unavailable-args) | rev | cut -c 2- | rev) not available in $APPSPATH \n INSTALLED PROGRAMS (valid arguments):"
						cd $APPSPATH && find -name 'remove' -printf " %h\n" 2>/dev/null | sort -u | xargs -n 1 basename 2> /dev/null; rm $AMPATH/.cache/unavailable-args
						echo -e "-----------------------------------------------------------------------"
						exit
					else
						exit
					fi
				else
					_remove
				fi
			done
		done
	done

	shift
	;;

  '-R')

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/remove-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/remove-args && echo STOP >> $AMPATH/.cache/remove-args
		ARGS=$(tail -n +2 $AMPATH/.cache/remove-args)

		case $2 in
		'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
		esac

		for arg in $ARGS; do
			for var in $arg; do

				function _REMOVE(){
					if ls $APPSPATH/$arg > /dev/null 2>&1; then
						case $arg in
						*) 
							for var in $arg; do
								$SUDOCOMMAND $APPSPATH/$arg/remove && echo ' ◆ "'$(echo $arg | tr a-z A-Z)'" HAS BEEN SUCCESSFULLY REMOVED!' && break; break
							done;;
						esac
					else
						echo '"'$arg'",' >> $AMPATH/.cache/unavailable-args
					fi
				}

				if [ $arg == STOP ]; then
					if test -f $AMPATH/.cache/unavailable-args; then
						echo -e "-----------------------------------------------------------------------"
						echo -e " ◆ ERROR: $(echo $(cat $AMPATH/.cache/unavailable-args) | rev | cut -c 2- | rev) not available in $APPSPATH \n INSTALLED PROGRAMS (valid arguments):"
						cd $APPSPATH && find -name 'remove' -printf " %h\n" 2>/dev/null | sort -u | xargs -n 1 basename 2> /dev/null; rm $AMPATH/.cache/unavailable-args
						echo -e "-----------------------------------------------------------------------"
						exit
					else
						exit
					fi
				else
					_REMOVE
				fi
			done
		done
	done

	shift
	;;

  '-s'|'sync')

	_no_sudo
	_online_check

	echo "-----------------------------------------------------------------------"
	echo -e " Check for any changes between the installation scripts\n in the repository and those used previously..."
	rm -R -f $AMPATH/.cache/sync-args && echo "" >> $AMPATH/.cache/sync-args
	cd $APPSPATH && find -name 'AM-updater' -printf "%h\n" 2>/dev/null | sort -u | cut -c 3- > $AMPATH/.cache/sync-args
	ARGS=$(cat $AMPATH/.cache/sync-args)

	for arg in $ARGS; do
		for var in $arg; do

			function _sync_all(){
			if test -d $APPSPATH/$arg/.am-installer; then
					INSTALLER=$(ls -A1 $APPSPATH/$arg/.am-installer)
					CURRENT=$(cat $APPSPATH/$arg/.am-installer/$INSTALLER)
					rm -R -f $AMPATH/.cache/$arg
					wget -q $AMREPO/programs/$arch/$INSTALLER -O $AMPATH/.cache/$INSTALLER
					SOURCE=$(cat $AMPATH/.cache/$INSTALLER)
					if [ "$CURRENT" == "$SOURCE" ]; then
						echo -ne "\r" 2> /dev/null
					else
						echo -e "\n The installer of $INSTALLER has been changed! \n See https://github.com/ivan-hc/AM-Application-Manager/blob/main/programs/$arch/$INSTALLER"
					fi
					rm -R -f $AMPATH/.cache/$INSTALLER
				else
					echo -e "\n ◆ No installation script for $arg...\n downloading one to $APPSPATH/$arg/.am-installer for this entry"
					mkdir -p $APPSPATH/$arg/.am-installer
					wget -q $AMREPO/programs/$arch/$arg -O /$APPSPATH/$arg/.am-installer/$arg
				fi
			}

			_sync_all
		done
	done

	function _sync_amcli(){
		echo "-----------------------------------------------------------------------"
	  	rm -R -f $AMPATH/$arch-apps $AMPATH/list && wget -q $AMREPO/programs/$arch-apps -P $AMPATH && echo -e "$(awk -v FS="(◆ | : )" '{print $2}' 0<$AMPATH/$arch-apps)" >> $AMPATH/list
		cd $AMPATH && cat options >> list
		echo ""; echo ' SYNCHRONIZING "'$(echo $AMCLI | tr a-z A-Z)'" VERSION '$($AMCLIPATH -v)'...'; sleep 0.5; echo "";
	  	rm -R -f $AMPATH/.cache/* 1>/dev/null; cd $AMPATH/.cache
	  	if [ $AMCLI == am ] 2>/dev/null; then
	  		wget -q $AMREPO/APP-MANAGER && chmod a+x ./APP-MANAGER
	  		cd ..
	  		mv $AMPATH/.cache/APP-MANAGER $AMPATH; chmod 777 $AMPATH
	  	else
	  		wget -q $AMREPO/APP-MANAGER -O appman && chmod a+x ./$AMCLI
	  		cd ..
	  		mv $AMPATH/.cache/$AMCLI $AMCLIPATH
	  	fi
	 	echo ' THE "'$(echo $AMCLI | tr a-z A-Z)'" SCRIPT IS NOW UPDATED TO THE '$($AMCLIPATH -v)' VERSION! '
	 	echo -e "\n See https://github.com/ivan-hc/AM-Application-Manager/commits/main/APP-MANAGER\n"
	}

	_sync_amcli
	;;

  '-t'|'template')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/template-args
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/template-args && echo STOP >> $AMPATH/.cache/template-args
		ARGS=$(tail -n +2 $AMPATH/.cache/template-args)

		for arg in $ARGS; do
			for var in $arg; do
				if [ $arg == STOP ]; then
					exit
				else
					case $arg in
					*) 
						for var in $arg; do
							cd $(xdg-user-dir DESKTOP)
							echo "##############################################################"; echo ""
							echo ' Create templates for "'$arg'"
							' | tr a-z A-Z
							echo "--------------------------------------------------------------"; echo "" 
							echo " EACH MODEL IS BASED ON A DIFFERENT METHOD OF BUILDING/UPDATING THE PROGRAM.
							"
							echo ' PLEASE, SELECT A TEMPLATE FOR "'$(echo $arg | tr a-z A-Z)'": 
							'
							echo '  0) APPIMAGE updatable with "appimageupdatetool"' | tr a-z A-Z
							echo '  1) APPIMAGE updatable using a separated zsync file' | tr a-z A-Z
							echo "  2) ANY ARCHIVE from github.com (always updatable)" | tr a-z A-Z
							echo "  3) ANY ARCHIVE from AUR (always updatable)" | tr a-z A-Z
							echo "  4) APPIMAGE, build your own with pkg2appimage and appimagetool" | tr a-z A-Z
							echo "  5) APPIMAGE, build your own with pkg2appimage using a custom AppRun" | tr a-z A-Z
							echo '  6) WEBAPP based on a Firefox profile (requires "firefox" in $PATH)' | tr a-z A-Z
							echo "  7) APPIMAGE from github.com (always updatable)" | tr a-z A-Z
							echo "  8) APPIMAGE from any website out there (for advanced users)" | tr a-z A-Z
							echo "  9) APPIMAGE from the AUR (always updatable)" | tr a-z A-Z
							echo "  10) ANY PROGRAM from any website (for advanced users)" | tr a-z A-Z
							echo " "
							read -r -p " WHAT KIND OF PROGRAM DO YOU WANT TO WRITE A SCRIPT FOR? : " response

							case "$response" in
    							# APPIMAGE UPDATABLE USING APPIMAGEUPDATETOOL
    							0) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-for-appimageupdate -O ./am-scripts/$arch/$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
    								# IF YOU CAN, USE A ONE-LINE COMMAND TO DOWNLOAD THE APPIMAGE
    								read -r -ep " USE A ONE-LINE COMMAND TO DOWNLOAD THE LATEST APPIMAGE $(echo -e '\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,17p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo ''$response'' >> ./am-scripts/$arch/$arg
									sed -n '19,105p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								read -r -ep " ◆ ADD THE URL TO THE MAIN PAGE OF THE WEBSITE $(echo -e '\n : ')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#REPLACETHIS#$RESPONSE#g ./am-scripts/$arch/$arg
								esac

								echo "-----------------------------------------------------------------------"
								# ADD A DESCRIPTION FOR THIS APPLICATION
								read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT

								case "$COMMENT" in
								*) 
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# APPIMAGE UPDATABLE USING ZSYNC
    							1) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-for-zsync -O ./am-scripts/$arch/$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
    								# IF YOU CAN, USE A ONE-LINE COMMAND TO DOWNLOAD THE APPIMAGE
    								read -r -ep " USE A ONE-LINE COMMAND TO CHECK ONLY THE URL TO THE APPIMAGE $(echo -e '\n THE DEFAULT PROGRAM ASSIGNED TO DOWNLOAD THIS LINK WILL BE "WGET"\n USE YOUR BEST SKILLS TO GET THE URL OF THE LATEST VERSION AVAILABLE\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,3p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'URL=$('$response')' >> ./am-scripts/$arch/$arg
									sed -n '5,36p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'URL=$('$response')' >> ./am-scripts/$arch/$arg
									sed -n '38,118p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

    								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								read -r -ep " ◆ ADD THE URL TO THE MAIN PAGE OF THE WEBSITE $(echo -e '\n : ')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#REPLACETHIS#$RESPONSE#g ./am-scripts/$arch/$arg
								esac

								echo "-----------------------------------------------------------------------"
								# ADD A DESCRIPTION FOR THIS APPLICATION
								read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT

								case "$COMMENT" in
								*) 
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

    							# DOWNLOAD ANY ARCHIVE FROM GITHUB
    							2) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-GitHub-generic -O ./am-scripts/$arch/$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								# ADD THE NAME OF THE GITHUB REPOSITORY, ALSO ADD SOME BASIC INFO IF AVAILABLE
								read -r -ep " ◆ ADD THE NAME OF THE REPOSITORY (EXAMPLE 'NAME/REPOSITORY') $(echo -e '\n https://github.com/')" GITHUBREPO

								case "$GITHUBREPO" in
								*) 
									sed -i s#REPLACETHIS#$GITHUBREPO#g ./am-scripts/$arch/$arg
									# GET THE DESCRIPTION OF THIS REPOSITORY TO ADD IN ALL CREATED FILES
									COMMENT=$(curl $HeaderAuthWithGITPAT https://api.github.com/repos/$GITHUBREPO 2>/dev/null | grep description | sed 's/"description": "//' | sed 's/",//' | cut -c 3-)
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: https://github.com/$GITHUBREPO\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md && 
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								# ADD AN EXTENSION OR AN UNIQUE PART OF THE NAME OF THE ARCHIVE
								read -r -ep " ◆ ADD THE EXTENSION OR AN UNIQUE PART OF THE ARCHIVE'S NAME $(echo -e '\n EXAMPLE, "amd64.zip" OR "x64.tar" ): ')" EXTENSION

								case "$EXTENSION" in
								*) 
									sed -i s#FILENAMEEXTENSION#$EXTENSION#g ./am-scripts/$arch/$arg
								esac

								# CHECK THE DOWNLOAD LINK, THE ONE THAT POINTS TO THE LATEST RELEASE, NOTE THAT IT MAY BE A DEVELOPMENT VERSION, ADD "/latest" AFTER "/releases" TO POINT TO THE LAST STABLE VERSION
								read -p ' Do you wish to check the link to the latest version (Y,n)?' yn

								case $yn in
								'N'|'n');;
								'Y'|'y'|*) 
									echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$GITHUBREPO/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -i "$EXTENSION" | cut -d '"' -f 4 | head -1
									echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
								esac

								read -p ' If correct, press "ENTER", otherwise press "N": ' yn

								case $yn in
								'N'|'n') 
									read -p ' Press 1 to add something or 2 to remove something: ' response
									case "$response" in
									1) 
										read -r -ep ' URL must contain ("x64", "x86_64"... or leave blank): ' response

										case "$response" in
										'') ;;
										*) 
											sed -i 's#browser_download_url#browser_download_url | grep -i '$response' #g' ./am-scripts/$arch/$arg
											read -p ' Do you wish to check the link for the last time (Y,n)?' yn

											case $yn in
											'N'|'n') ;;
											'Y'|'y'|*) 
												echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$GITHUBREPO/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -i "$response" | grep -i $EXTENSION | cut -d '"' -f 4 | head -1
												echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
											esac;;
										esac;;
									2) 
										read -r -ep ' URL must NOT contain ("txt", "ARM"... or leave blank): ' response

										case "$response" in
										'') ;;
										*) 
											sed -i 's#browser_download_url#browser_download_url | grep -w -v '$response' #g' ./am-scripts/$arch/$arg
											read -p ' Do you wish to check the link for the last time (Y,n)?' yn

											case $yn in
											'N'|'n') ;;
											'Y'|'y'|*) 
												echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$GITHUBREPO/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -w -v "$response" | grep -i $EXTENSION | cut -d '"' -f 4 | head -1
												echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
											esac;;
										esac;;
									esac;;
								'Y'|'y'|*) ;;
								esac

								echo "-----------------------------------------------------------------------"
								# IF YOU CAN, USE A ONE-LINE COMMAND TO EXTRACT THE ARCHIVE, CREATE ICONS AN MORE
    								echo -e "\n NOW, SUPPOSE YOU DOWNLOADED THE ARCHIVE TO './tmp', THEN RUN \n THE COMMAND 'cd ..'. WE WILL STILL NEED TO:\n\n  - extract the archive;\n  - transfer content into the directory we are in;\n  - include an icon and .desktop file in the same place (if needed).\n\n WHAT ARE THE COMMANDS TO RUN BEFORE './tmp' IS REMOVED?\n" 
    								read -r -ep " IF YOU CAN, RESUME EVERYTHING IN ONE LINE COMMAND $(echo -e '\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,22p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "$response" >> ./am-scripts/$arch/$arg
									sed -n '24,43p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "  $response" >> ./am-scripts/$arch/$arg
									sed -n '45,80p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# DOWNLOAD ANY ARCHIVE FROM THE ARCH USER REPOSITORY
							3) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND, ALSO ADD SOME BASIC INFO IF AVAILABLE
								wget -c -q $AMREPO/templates/AM-SAMPLE-AUR-generic -O ./am-scripts/$arch/$arg &&
								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								read -r -ep " ◆ ADD THE REAL NAME OF THE PACKAGE BASE, ON AUR$(echo -e '\n : ')" AUR

								case "$AUR" in
								*) 
									sed -i s#AURREPO#$AUR#g ./am-scripts/$arch/$arg
								esac

								# ADD AN EXTENSION OR AN UNIQUE PART OF THE NAME OF THE ARCHIVE
								read -r -ep " ◆ ADD THE EXTENSION OR AN UNIQUE PART OF THE ARCHIVE'S NAME $(echo -e '\n EXAMPLE, "amd64", "x86_64", "tar.gz"... ): ')" EXTENSION

								case "$EXTENSION" in
								*) 
									sed -i s#FILENAMEEXTENSION#$EXTENSION#g ./am-scripts/$arch/$arg
								esac

								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								UPSTREAMURL=$(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -A3 "Upstream URL" | tail -1 | cut -c 30- | cut -f1 -d'"')
								sed -i s#REPLACETHIS#$UPSTREAMURL#g ./am-scripts/$arch/$arg
								# ADD A DESCRIPTION FOR THIS APPLICATION
								COMMENT=$(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -A1 Description | tail -1 | cut -c 30- | rev | cut -c 6- | rev) 
								# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
								echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
								echo -e "\n $COMMENT\n\n SITE: $UPSTREAMURL\n\n SOURCE: https://aur.archlinux.org/packages/$AUR\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
								# CREATE A NEW LINE FOR THE APPLICATION'S LIST
								echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								echo "-----------------------------------------------------------------------"
								echo " THIS IS A PREVIEW OF THE DOWNLOAD LINK:"
								echo "$(echo $(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | grep -i "$EXTENSION" | grep -w -v ".asc" | grep -w -v ".zsync" | grep -w -v "archlinux.org" | head -1))"
								echo "-----------------------------------------------------------------------"
								# IF YOU CAN, USE A ONE-LINE COMMAND TO EXTRACT THE ARCHIVE, CREATE ICONS AN MORE
    								echo -e "\n NOW, SUPPOSE YOU DOWNLOADED THE ARCHIVE TO './tmp', THEN RUN \n THE COMMAND 'cd ..'. WE WILL STILL NEED TO:\n\n  - extract the archive;\n  - transfer content into the directory we are in;\n  - include an icon and .desktop file in the same place (if needed).\n\n WHAT ARE THE COMMANDS TO RUN BEFORE './tmp' IS REMOVED?\n" 
    								read -r -ep " IF YOU CAN, RESUME EVERYTHING IN ONE LINE COMMAND $(echo -e '\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,22p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "$response" >> ./am-scripts/$arch/$arg
									sed -n '24,42p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "  $response" >> ./am-scripts/$arch/$arg
									sed -n '44,79p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

    							# CREATE AN APPIMAGE ON-THE-FLY USING DEFAULT TOOLS
    							4) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-pkg2appimage -O ./am-scripts/$arch/$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
								echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
								echo "\n $COMMENT\n\n SITE: \n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&  
								# CREATE A NEW LINE FOR THE APPLICATION'S LIST
								echo '◆ '$arg' : '$COMMENT'' >> ./am-scripts/list
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# CREATE AN APPIMAGE ON-THE-FLY USING CUSTOM OPTIONS
    							5) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-pkg2appimage-custom -O ./am-scripts/$arch/$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								# LIBUNIONPRELOAD
								read -p ' Do you wish to add libunionpreload (y,N)?' yn

								case $yn in
								'Y'|'y') 
									sed -i 's/#export LD_PRELOAD/export LD_PRELOAD/g' ./am-scripts/$arch/$arg
									sed -i 's/#chmod/chmod/g' ./am-scripts/$arch/$arg
									sed -i 's/#mv ./mv ./g' ./am-scripts/$arch/$arg
									sed -i 's/#wget/wget/g' ./am-scripts/$arch/$arg;;
								'N'|'n'|*) ;;
								esac

								# SYSTEM LIBRARIES
								read -p ' Do you wish to include system libraries (Y,n)?' yn

								case $yn in
								'N'|'n') 
									sed -i 's/##export LD_LIBRARY_PATH/export LD_LIBRARY_PATH/g' ./am-scripts/$arch/$arg;;
								'Y'|'y'|*) 
									sed -i 's/#export LD_LIBRARY_PATH/export LD_LIBRARY_PATH/g' ./am-scripts/$arch/$arg;;
								esac

								# BINARY PATH
								read -p ' Do you wish to specify the binary`s path manually (y,N)?' yn

								case $yn in 
								'Y'|'y') 
									read -r -ep " ◆ ADD THE PATH (EXAMPLE '/usr/lib.../$arg') $(echo -e '\n: ')" RESPONSE

									case "$RESPONSE" in
									*) 
										sed -i s/#exec/exec/g ./am-scripts/$arch/$arg
										sed -i s#CUSTOMPATH#$RESPONSE#g ./am-scripts/$arch/$arg
									esac;;
								'N'|'n'|*) 
									sed -i s/##exec/exec/g ./am-scripts/$arch/$arg;;
								esac

								# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
								echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
								echo -e "\n $COMMENT\n\n SITE: \n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&  
								# CREATE A NEW LINE FOR THE APPLICATION'S LIST
								echo '◆ '$arg' : '$COMMENT'' >> ./am-scripts/list; echo ""
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# CREATE A CUSTOM FIREFOX PROFILE ("firefox" MUST BE IN "$PATH" TO MADE IT WORK)
    							6) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
    								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
    								wget -c -q $AMREPO/templates/AM-SAMPLE-Firefox-webapp -O ./am-scripts/$arch/ffwa-$arg &&
    								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/ffwa-$arg &&
								echo "-----------------------------------------------------------------------"
								read -r -ep " ◆ NAME OF THE APP: " RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#GIVEMEANAME#$RESPONSE#g ./am-scripts/$arch/ffwa-$arg
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo ffwa-$arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/ffwa-$arg.md
									echo -e "\n WebApp & Firefox Profile for $RESPONSE.\n\n SITE: MYWEBSITE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/ffwa-$arg.md &&  
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ ffwa-$arg : WebApp & Firefox Profile for $RESPONSE." >> ./am-scripts/list
								esac

								# PUT THE APP INTO A CATEGORY, THIS IS NEEDED FOR OUR CUSTOM DESKTOP FILE
								echo -e " LIST OF CATEGORIES:\n 0) AudioVideo\n 1) Audio\n 2) Video\n 3) Development\n 4) Education\n 5) Game\n 6) Graphics\n 7) Network\n 8) Office\n 9) Science\n 10) Settings\n 11) System\n 12) Utility (default)\n "
								read -r -p " CHOOSE A CATEGORY : " response

								case "$response" in
								0) 
									sed -i s/YOURCATEGORY/AudioVideo/g ./am-scripts/$arch/$arg;;
								1) 
									sed -i s/YOURCATEGORY/Audio/g ./am-scripts/$arch/$arg;; 
								2) 
									sed -i s/YOURCATEGORY/Video/g ./am-scripts/$arch/$arg;; 
								3) 
									sed -i s/YOURCATEGORY/Development/g ./am-scripts/$arch/$arg;;
								4) 
									sed -i s/YOURCATEGORY/Education/g ./am-scripts/$arch/$arg;; 
								5) 
									sed -i s/YOURCATEGORY/Game/g ./am-scripts/$arch/$arg;; 
								6) 
									sed -i s/YOURCATEGORY/Graphics/g ./am-scripts/$arch/$arg;; 
								7) 
									sed -i s/YOURCATEGORY/Network/g ./am-scripts/$arch/$arg;;
								8) 
									sed -i s/YOURCATEGORY/Office/g ./am-scripts/$arch/$arg;; 
								9) 
									sed -i s/YOURCATEGORY/Science/g ./am-scripts/$arch/$arg;; 
								10) 
									sed -i s/YOURCATEGORY/Settings/g ./am-scripts/$arch/$arg;; 
								11) 
									sed -i s/YOURCATEGORY/System/g ./am-scripts/$arch/$arg;;
								12|*) 
									sed -i s/YOURCATEGORY/Utility/g ./am-scripts/$arch/$arg;;
								esac

								echo "-----------------------------------------------------------------------"
								# ENTER THE URL OF THE SITE YOU WANT TO CREATE A CUSTOM FIREFOX PROFILE FOR
								read -r -ep " ◆ URL OF THE WEBAPP: " RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#MYWEBSITE#$RESPONSE#g ./am-scripts/$arch/ffwa-$arg; sed -i s#MYWEBSITE#$RESPONSE#g ./am-scripts/portable-linux-apps.github.io/apps/ffwa-$arg.md;;
								esac 

								echo "-----------------------------------------------------------------------"
								# ENTER THE URL OF THE ICON THAT WILL APPEAR IN THE MENU
								read -r -ep " ◆ COPY/PASTE THE URL OF THE ICON HERE $(echo -e '\n URL: ')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#YOURICONURL#$RESPONSE#g ./am-scripts/$arch/ffwa-$arg
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# DOWNLOAD THE APPIMAGE FROM GITHUB AND KEEP IT ALWAYS UPDATED (BEST WAY)
							7) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND, ALSO ADD SOME BASIC INFO IF AVAILABLE
								wget -c -q $AMREPO/templates/AM-SAMPLE-AppImage-from-GitHub -O ./am-scripts/$arch/$arg &&
								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								# ADD THE NAME OF THE GITHUB REPOSITORY
								read -r -ep " ◆ ADD THE NAME OF THE REPOSITORY (EXAMPLE 'NAME/REPOSITORY') $(echo -e '\n https://github.com/')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#REPLACETHIS#$RESPONSE#g ./am-scripts/$arch/$arg
									# GET THE DESCRIPTION OF THIS REPOSITORY TO ADD IN ALL CREATED FILES
									COMMENT=$(curl $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE 2>/dev/null | grep description | sed 's/"description": "//' | sed 's/",//' | cut -c 3-)
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: https://github.com/$RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								echo "-----------------------------------------------------------------------"
								# CHECK THE DOWNLOAD LINK, THE ONE THAT POINTS TO THE LATEST RELEASE, NOTE THAT IT MAY BE A DEVELOPMENT VERSION, ADD "/latest" AFTER "/releases" TO POINT TO THE LAST STABLE VERSION
								read -p ' Do you wish to check the link to the latest version (Y,n)?' yn

								case $yn in
								'N'|'n') ;;
								'Y'|'y'|*) 
									echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -i appimage | cut -d '"' -f 4 | head -1
									echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
								esac

								read -p ' If correct, press "ENTER", otherwise press "N": ' yn

								case $yn in
								'N'|'n') 
									read -p ' Press 1 to add something or 2 to remove something: ' response

									case "$response" in
									1) 
										read -r -ep ' URL must contain ("x64", "x86_64"... or leave blank): ' response

										case "$response" in
										'') ;;
										*) 
											sed -i 's#browser_download_url#browser_download_url | grep -i '$response' #g' ./am-scripts/$arch/$arg
											read -p ' Do you wish to check the link for the last time (Y,n)?' yn

											case $yn in
											'N'|'n') ;;
											'Y'|'y'|*) 
												echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -i "$response" | grep -i appimage | cut -d '"' -f 4 | head -1
												echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
											esac;;
										esac;;
									2) 
										read -r -ep ' URL must NOT contain ("txt", "ARM"... or leave blank): ' response

										case "$response" in
										'') ;;
										*) 
											sed -i 's#browser_download_url#browser_download_url | grep -w -v '$response' #g' ./am-scripts/$arch/$arg
											read -p ' Do you wish to check the link for the last time (Y,n)?' yn

											case $yn in
											'N'|'n') ;;
											'Y'|'y'|*) 
												echo ""; wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases -O - | grep -w -v i386 | grep -w -v i686 | grep -w -v aarch64 | grep -w -v arm64 | grep -w -v armv7l | grep browser_download_url | grep -w -v "$response" | grep -i appimage | cut -d '"' -f 4 | head -1
												echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n";;
											esac;;
										esac;;
									esac;;
								'Y'|'y'|*) ;;
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# DOWNLOAD THE APPIMAGE FROM ANY WEBSITE
							8) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND, ALSO ADD SOME BASIC INFO IF AVAILABLE
								wget -c -q $AMREPO/templates/AM-SAMPLE-AppImage-from-Anywhere -O ./am-scripts/$arch/$arg &&
								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								read -r -ep " ◆ ADD THE URL TO THE MAIN PAGE OF THE WEBSITE $(echo -e '\n : ')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#REPLACETHIS#$RESPONSE#g ./am-scripts/$arch/$arg
								esac

								echo "-----------------------------------------------------------------------"
								# ADD A DESCRIPTION FOR THIS APPLICATION
								read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT

								case "$COMMENT" in
								*) 
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								echo "-----------------------------------------------------------------------"
								# IF YOU CAN, USE A ONE-LINE COMMAND TO DOWNLOAD THE APPIMAGE
    								read -r -ep " USE A ONE-LINE COMMAND TO CHECK ONLY THE URL TO THE APPIMAGE $(echo -e '\n THE DEFAULT PROGRAM ASSIGNED TO DOWNLOAD THIS LINK WILL BE "WGET"\n USE YOUR BEST SKILLS TO GET THE URL OF THE LATEST VERSION AVAILABLE\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,18p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'version=$('$response')' >> ./am-scripts/$arch/$arg
									sed -n '20,34p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'version=$('$response')' >> ./am-scripts/$arch/$arg
									sed -n '36,118p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# DOWNLOAD THE APPIMAGE FROM THE ARCH USER REPOSITORY
							9) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND, ALSO ADD SOME BASIC INFO IF AVAILABLE
								wget -c -q $AMREPO/templates/AM-SAMPLE-AppImage-from-AUR -O ./am-scripts/$arch/$arg &&
								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								read -r -ep " ◆ ADD THE REAL NAME OF THE PACKAGE BASE, ON AUR$(echo -e '\n : ')" AUR

								case "$AUR" in
								*) 
									sed -i s#AURREPO#$AUR#g ./am-scripts/$arch/$arg
								esac

								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								UPSTREAMURL=$(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -A3 "Upstream URL" | tail -1 | cut -c 30- | cut -f1 -d'"')
								sed -i s#REPLACETHIS#$UPSTREAMURL#g ./am-scripts/$arch/$arg
								# ADD A DESCRIPTION FOR THIS APPLICATION
								COMMENT=$(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -A1 Description | tail -1 | cut -c 30- | rev | cut -c 6- | rev) 
								# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
								echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
								echo -e "\n $COMMENT\n\n SITE: $UPSTREAMURL\n\n SOURCE: https://aur.archlinux.org/packages/$AUR\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
								# CREATE A NEW LINE FOR THE APPLICATION'S LIST
								echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								echo "-----------------------------------------------------------------------"
								echo " THIS IS A PREVIEW OF THE DOWNLOAD LINK:"
								echo "$(echo $(wget -q https://aur.archlinux.org/packages/$AUR -O - | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | grep -i ".appimage" | grep -w -v ".asc" | grep -w -v ".zsync" | grep -w -v "archlinux.org" | head -1))"
								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							# DOWNLOAD ANY ARCHIVE FROM ANY WEBSITE
							10) 
								mkdir -p ./am-scripts ./am-scripts/$arch ./am-scripts/portable-linux-apps.github.io/apps &&
								# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND, ALSO ADD SOME BASIC INFO IF AVAILABLE
								wget -c -q $AMREPO/templates/AM-SAMPLE-Anywhere-generic -O ./am-scripts/$arch/$arg &&
								sed -i s/SAMPLE/$arg/g ./am-scripts/$arch/$arg &&
								echo "-----------------------------------------------------------------------"
								# ADD THE URL TO THE MAIN PAGE OF THE WEBSITE
								read -r -ep " ◆ ADD THE URL TO THE MAIN PAGE OF THE WEBSITE $(echo -e '\n : ')" RESPONSE

								case "$RESPONSE" in
								*) 
									sed -i s#REPLACETHIS#$RESPONSE#g ./am-scripts/$arch/$arg
								esac

								echo "-----------------------------------------------------------------------"
								# ADD A DESCRIPTION FOR THIS APPLICATION
								read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT

								case "$COMMENT" in
								*) 
									# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
									echo "# $(echo $arg | tr a-z A-Z)" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md
									echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/$arg.md &&
									# CREATE A NEW LINE FOR THE APPLICATION'S LIST
									echo "◆ $arg : $COMMENT" >> ./am-scripts/list
								esac

								echo "-----------------------------------------------------------------------"
								# IF YOU CAN, USE A ONE-LINE COMMAND TO CHECK THE DOWNLOAD LINK TO THE LAST RELEASE OR FROM THE DOWNLOAD PAGE OF THE WEBSITE
    								read -r -ep " USE A ONE-LINE COMMAND ONLY TO SHOW THE DOWNLOAD URL, NOTHING MORE. $(echo -e '\n THE DEFAULT PROGRAM ASSIGNED TO DOWNLOAD THIS LINK WILL BE "WGET"\n USE YOUR BEST SKILLS TO GET THE URL OF THE LATEST VERSION AVAILABLE\n :') " DOWNLOADURL

								case "$DOWNLOADURL" in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,18p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'version=$('$DOWNLOADURL')' >> ./am-scripts/$arch/$arg
									sed -n '20,33p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo 'version=$('$DOWNLOADURL')' >> ./am-scripts/$arch/$arg
									sed -n '35,80p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac 	 

								echo "-----------------------------------------------------------------------"
								# IF YOU CAN, USE A ONE-LINE COMMAND TO EXTRACT THE ARCHIVE, CREATE ICONS AN MORE
    								echo -e "\n NOW, SUPPOSE YOU DOWNLOADED THE ARCHIVE TO './tmp', THEN RUN \n THE COMMAND 'cd ..'. WE WILL STILL NEED TO:\n\n  - extract the archive;\n  - transfer content into the directory we are in;\n  - include an icon and .desktop file in the same place (if needed).\n\n WHAT ARE THE COMMANDS TO RUN BEFORE './tmp' IS REMOVED?\n" 
    								read -r -ep " IF YOU CAN, RESUME EVERYTHING IN ONE LINE COMMAND $(echo -e '\n :') " response

								case $response in
								*) 
									mv ./am-scripts/$arch/$arg ./am-scripts/$arch/$arg.old
									sed -n '1,22p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "$response" >> ./am-scripts/$arch/$arg
									sed -n '24,42p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									echo "  $response" >> ./am-scripts/$arch/$arg
									sed -n '44,80p' ./am-scripts/$arch/$arg.old >> ./am-scripts/$arch/$arg
									rm -f ./am-scripts/$arch/$arg.old;;
								esac

								echo "-----------------------------------------------------------------------"
								# END OF THIS FUNCTION
								echo -e "\n All files are saved in $(xdg-user-dir DESKTOP)/am-scripts\n";;

							*) 
								echo -e "\n No valid argument was chosen: process aborted! \n" | tr a-z A-Z; break;;
							esac
						done
					esac
				fi
			done
		done
	done

	shift
	;;

  '-u'|'-U'|'update') 

	_no_sudo
	_online_check

	while [ -n "$1" ]; do
		case $2 in
		'') 
			echo ""; echo -e ' "'$(echo $AMCLI | tr a-z A-Z)'" CAN MANAGE UPDATES FOR THE FOLLOWING PROGRAMS: \n'
  			cd $APPSPATH && find -name 'AM-updater' -printf " %h\n" 2>/dev/null | sort -u | xargs -n 1 basename 2>/dev/null
			echo -e '\n ALL OTHERS ARE FIXED VERSIONS OR CAN MANAGE UPDATES BY THEMSELF'; sleep 0.1
			echo -e "\n THE UPDATE VERIFICATION PROCESS IS IN PROGRESS, PLEASE WAIT..."; sleep 0.2; echo -e "\n-----------------------------------------------------------------------"
			echo -e " >> START OF ALL PROCESSES <<\n-----------------------------------------------------------------------"

			for f in $APPSPATH/*/; do 
				cd $f 2>/dev/null && 
				if test -f ./AM-updater; then
					start=$(date +%s); $(sh -x ./AM-updater > /dev/null 2>&1) | echo -ne ' Updating "'$(printf '%s\n' "${PWD##*/}")'"...\r'; end=$(date +%s)
					echo -e " ◆ $(echo $(printf '%s\n' "${PWD##*/}") | tr a-z A-Z) is updated, $(($end-$start)) seconds elapsed! \n"
				else
					echo ""  > /dev/null 2>&1
				fi
			done

			rm -R -f $AMPATH/.cache/* $APPSPATH/*/tmp; echo "-----------------------------------------------------------------------"
			$AMCLIPATH -s 
			echo -e "-----------------------------------------------------------------------\n >> END OF ALL PROCESSES << \n-----------------------------------------------------------------------\n"; sleep 0.2
			echo -e ' ALL THE PROGRAMS MANAGED BY "'$(echo $AMCLI | tr a-z A-Z)'" ARE UPDATED! \n'
			exit;;
		*) 
			if test -f $APPSPATH/$2/AM-updater; then
				start=$(date +%s); $APPSPATH/$2/AM-updater > /dev/null 2>&1 | echo -ne "\n UPDATING $(echo $2 | tr a-z A-Z)\r"; end=$(date +%s)
				echo -e " ◆ $(echo $2 | tr a-z A-Z) is updated, $(($end-$start)) seconds elapsed! \n" && break
			else
				echo -e '\n "'$(echo $AMCLI | tr a-z A-Z)'" CANNOT MANAGE UPDATES FOR "'$(echo $2 | tr a-z A-Z)'"'
				UPDATERS=$(cd $APPSPATH/$2 2>/dev/null && find . -name "*update*" -print 2>/dev/null)

				if [ -n "$UPDATERS" ]; then
					echo ' This program probably includes its own update system!'
				else
					break
				fi

				exit
			fi
		esac
	done

	shift
	;;

  '-w'|'web')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		if curl --output /dev/null --silent --head --fail https://raw.githubusercontent.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/main/apps/$2.md  1>/dev/null; then
			case $2 in
				*) 
					for var in $2; do 
						rm -R -f $AMPATH/.cache/about; mkdir $AMPATH/.cache/about;
						cd $AMPATH/.cache/about && wget -q https://raw.githubusercontent.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/main/apps/$2.md && echo "" && echo " SITE/SOURCES:" && echo "" &&
						sed -i '/^\s*#/ d' $2.md; sed -i '/^\s*!/ d' $2.md; sed -i '/\[Go back]/d' $2.md
						grep -o " http[^ ]*" ./$2.md && echo "" && exit; 
					done
			esac
		else
			echo ""; 
			echo ' "'$2'" IS NOT A VALID ARGUMENT, PLEASE RUN THE "'$AMCLI' -l" COMMAND'; echo ""; 
			echo ' TO SEE THE LIST OF ALL THE APPLICATIONS AVAILABLE FOR '$arch''; echo "";
			echo ' OR START A SEARCH (WITH KEYWORDS) BY USING THE "'$AMCLI' -q" COMMAND'; echo "";
			exit;
		fi
	done

	shift
	;;

  '--disable-completion')

  	if [ $(grep -o $AMCLI 0<$COMPLETIONPATH/$COMPLETIONFILE | head -1) == $AMCLI 2>/dev/null ]; then
  		grep -v 'complete -W "$(cat '$AMPATH'/list 2>/dev/null)" '$AMCLI'' $COMPLETIONPATH/$COMPLETIONFILE > $AMPATH/$COMPLETIONFILE.backup &&
		$SUDOCOMMAND mv $AMPATH/$COMPLETIONFILE.backup $COMPLETIONPATH/$COMPLETIONFILE
		echo " Completion disabled! "
	else
		echo " Completion script is NOT enabled! "
	fi
	;;

  '--enable-completion')

	if [ $(grep -o $AMCLI 0<$COMPLETIONPATH/$COMPLETIONFILE | head -1) == $AMCLI 2>/dev/null ]; then
		echo " Completion script is already enabled!"
	else
		$SUDOCOMMAND mkdir -p $COMPLETIONPATH
		echo '#!/usr/bin/env bash' >> $COMPLETIONFILE
		echo 'complete -W "$(cat '$AMPATH'/list 2>/dev/null)" '$AMCLI'' >> $COMPLETIONFILE
		chmod a+x $COMPLETIONFILE
		$SUDOCOMMAND mv $COMPLETIONFILE $COMPLETIONPATH/
		echo " Completion has been enabled!"
	fi
	;;

  '--firejail'|'--sandbox')

	_no_sudo

	if ! command -v firejail 1>/dev/null; then
  		printf "\n%s\n" 'ERROR: "firejail" is not installed!'; deps=1
		[ "$deps" != "1" ] || { printf "\nInstall the above from your package manager and try again\n\n"; exit 1; }
	fi

	case $2 in
	'') 
		echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do

		if ! test -f $APPSPATH/$2/remove; then
			echo " '$2' is not a valid argument or is not installed."; exit
		fi

		if [ -z $(strings -d $APPSPATH/$2/"$2" 2>/dev/null | grep -m 1 "AppImage") ]; then
	    		echo ' ERROR: "'$2'" is NOT an AppImage'; exit
	    	fi

		echo -e '---------------------------------------------------------------------\nYOU ARE USING FIREJAIL VIA "'$(echo $AMCLI | tr a-z A-Z)'".\n---------------------------------------------------------------------'

		if ! test -f $APPSPATH/$2/firejail.profile; then
				cp /etc/firejail/default.profile $APPSPATH/$2/firejail.profile
				echo -e "◆ Created $APPSPATH/$2/firejail.profile, do what you will with it! \n---------------------------------------------------------------------"
		fi


		DESKTOPFILEANDPATH=$(cat $APPSPATH/$2/remove | tr " " "\n" | grep "/share/applications" | head -1)
		LAUNCHERPATH=$(echo $DESKTOPFILEANDPATH | sed 's:/[^/]*$::')

		if ! test -f $APPSPATH/$2/firejail.profile; then
			cp /etc/firejail/default.profile $APPSPATH/$2/firejail.profile
			echo -e "◆ Created $APPSPATH/$2/firejail.profile\n---------------------------------------------------------------------"
		else
			echo -e "◆ Found $APPSPATH/$2/firejail.profile\n---------------------------------------------------------------------"
		fi

		echo -e "\n Choose to run once using Firejail (options 1 & 2) or make the\n changes permanent by editing the available .desktop files, if\n available (options 3 & 4).\n\n NOTE, once that the .desktop files are patched, they will be\n saved in ~/.local/share/applications, which means that if you\n use AppMan, the originals will be overwritten.\n"
		echo -e ' Supported text editors (option 5) are "vim" and "nano".\n'
		echo -e "1) Sandbox once using Firejail's defaults"
		echo -e "2) Sandbox once using the custom profile"
		echo -e "3) Patch the .desktop file using Firejail's defaults"
		echo -e "4) Patch the .desktop file using the custom profile"
		echo -e "5) Edit the custom $APPSPATH/$2/firejail.profile file\n"
		read -p 'CHOOSE BETWEEN 1, 2, 3, 4 AND 5, OR LEAVE BLANK TO EXIT: ' response

		case $response in

		1)	echo -e "\n############################### START FIREJAIL ###############################\n"
			firejail --appimage $APPSPATH/$2/$2
			echo -e "\n############################### STOP FIREJAIL ###############################\n"
			;;

		2)	echo -e "\n############################### START FIREJAIL ###############################\n"
			firejail --appimage --profile=$APPSPATH/$2/firejail.profile $APPSPATH/$2/$2
			echo -e "\n############################### STOP FIREJAIL ###############################\n"
			;;

		3)	mkdir -p ~/.local/share/applications; mkdir -p .am-patchinglauncher
			cp $DESKTOPFILEANDPATH ./.am-patchinglauncher/
			for f in ./.am-patchinglauncher/*; do
				for var in $f; do
					EXEC=$(grep -e '^Exec=.*' $f | head -n 1 | cut -d "=" -f 2- | sed -e 's|%.||g')
					sed -i "s#Exec=$CHANGEEXEC#Exec=firejail --appimage $APPSPATH/$2/$EXEC#g" ./$f
				done
			done
			mv ./.am-patchinglauncher/* ~/.local/share/applications/ 2> /dev/null;	rmdir .am-patchinglauncher
			break
			;;

		4)	mkdir -p ~/.local/share/applications; mkdir -p .am-patchinglauncher
			cp $DESKTOPFILEANDPATH ./.am-patchinglauncher/
			for f in ./.am-patchinglauncher/*; do
				for var in $f; do
					EXEC=$(grep -e '^Exec=.*' $f | head -n 1 | cut -d "=" -f 2- | sed -e 's|%.||g')
					sed -i "s#Exec=$CHANGEEXEC#Exec=firejail --appimage --profile=$APPSPATH/$2/firejail.profile $APPSPATH/$2/$EXEC#g" ./$f
				done
			done
			mv ./.am-patchinglauncher/* ~/.local/share/applications/ 2> /dev/null;	rmdir .am-patchinglauncher
			break
			;;

		5)	if command -v vim 1>/dev/null; then
				vim $APPSPATH/$2/firejail.profile
			else
				nano $APPSPATH/$2/firejail.profile
			fi
			;;

		*)	exit
			;;
		esac

	done

	shift
	;;

  '--launcher')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		rm -f $AMPATH/.cache/tolaunch-args;
		echo $@ | tr ' ' '\n' >> $AMPATH/.cache/tolaunch-args && echo STOP >> $AMPATH/.cache/tolaunch-args;
		ARGS=$(tail -n +2 $AMPATH/.cache/tolaunch-args)

		for arg in $ARGS; do
			for var in $arg; do

				function _launcher(){
					if [ -z $(strings -d "$arg" 2>/dev/null | grep -m 1 "AppImage") ]; then
    						echo ' ERROR: "'$arg'" is NOT an AppImage'; break
    					else
  						appimage=$(basename -- "$arg")
  						mkdir -p $HOME/.local/share/applications/AppImages
  						mkdir -p $HOME/.local/share/icons
						chmod a+x "$arg"
  						cd "$(dirname "$arg")"

  						function _launcher_appimage_extract(){
  							"$arg" --appimage-extract share/icons/*/*/* 1>/dev/null
							"$arg" --appimage-extract usr/share/icons/*/*/* 1>/dev/null
							"$arg" --appimage-extract share/icons/*/*/*/* 1>/dev/null
							"$arg" --appimage-extract usr/share/icons/*/*/*/* 1>/dev/null
							"$arg" --appimage-extract *.svg 1>/dev/null
							"$arg" --appimage-extract *.png 1>/dev/null
    							"$arg" --appimage-extract *.desktop 1>/dev/null
    							"$arg" --appimage-extract share/applications/*.desktop 1>/dev/null
    							"$arg" --appimage-extract usr/share/applications/*.desktop 1>/dev/null
    						}

    						function _launcher_appimage_integration(){
    							echo "[Desktop Entry]" >> ./"$appimage".desktop
    							echo "Version=1.0" >> ./"$appimage".desktop
    							echo "Type=Application" >> ./"$appimage".desktop
    							echo "Terminal=false" >> ./"$appimage".desktop
    							echo "StartupNotify=true" >> ./"$appimage".desktop
    							echo "Exec=$arg %U" >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep Name= | head -1 >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep -F 'Name[' >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep Categories= >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep Comment= | head -1 >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep -F 'Comment[' >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep Icon= | head -1 >> ./"$appimage".desktop
    							cat ./squashfs-root/*.desktop | grep MimeType= | head -1 >> ./"$appimage".desktop
    							cat ./"$appimage".desktop | uniq > ./"$appimage"-1.desktop && mv ./"$appimage"-1.desktop ./"$appimage".desktop
    							mv ./"$appimage".desktop $HOME/.local/share/applications/AppImages/"$appimage".desktop 2>/dev/null
    							mv ./squashfs-root/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/*.svg $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/0x0/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/22x22/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/24x24/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/32x32/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/48x48/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/64x64/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/128x128/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/256x256/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/512x512/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/icons/hicolor/scalable/apps/*.svg $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/share/pixmaps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/0x0/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/22x22/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/24x24/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/32x32/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/48x48/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/64x64/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/128x128/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/256x256/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/512x512/apps/*.png $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/icons/hicolor/scalable/apps/*.svg $HOME/.local/share/icons/ 2>/dev/null
    							mv ./squashfs-root/usr/share/pixmaps/*.png $HOME/.local/share/icons/ 2>/dev/null
							rm -R -f ./squashfs-root
    						}

    						_launcher_appimage_extract 2>/dev/null
    						_launcher_appimage_integration 2>/dev/null
    					fi
    				}

				if [ $arg == STOP ]; then
					exit
				else
					_launcher
				fi
			done
		done
	done

	shift
	;;

  '--rollback')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		if test -f $APPSPATH/$2/AM-updater; then
			if grep -q ".zsync" $APPSPATH/$2/AM-updater; then
				echo " AppImage packages updatable with zsync have not previous versions."; exit
			elif test -f $APPSPATH/$2/updater; then
				echo ' When an executable called "updater" is available, the app is only updateable.'; exit
			else
				if grep -q "api.github.com" $APPSPATH/$2/AM-updater; then
					rm -f "$AMPATH/.cache/rollback-args";
					REPO=$(grep -i "REPO=" $APPSPATH/$2/AM-updater | grep -o -P '(?<=").*(?=")')
					printf "%s\n" "$(wget -q $HeaderAuthWithGITPAT https://api.github.com/repos/$REPO/releases?per_page=100 -O - | grep browser_download_url | grep -v ".apk" | grep -v ".deb" | grep -v ".dmg" | grep -v ".exe" | grep -v ".flatpak" | grep -v ".flatpakref" | grep -v ".rpm" | grep -v ".sha256sum" | grep -v ".torrent" | grep -v ".verify" | grep -v ".zsync" | grep -i '.appimage\|.zip\|.tar.bz2|.tar.gz\|.tar.xf' | cut -d '"' -f 4)" | tr ' ' '\n' >> "$AMPATH/.cache/rollback-args" &&
					echo -e "-----------------------------------------------------------------------\nYou have chosen to roll back $2.\nRollback will be done from https://github.com/$REPO\n-----------------------------------------------------------------------"
					printf "Select a URL from this menu (read carefully) or press CTRL+C to abort:\n-----------------------------------------------------------------------\n"; sleep 1;
					select d in $(cat "$AMPATH/.cache/rollback-args"); do test -n "$d" && break; echo ">>> Invalid Selection"; done
					cp $APPSPATH/$2/AM-updater $APPSPATH/$2/AM-rollback
					sed -i 's/version0/#version0/g' $APPSPATH/$2/AM-rollback
					sed -i '/show-progress/c\wget -q --show-progress '$d'' $APPSPATH/$2/AM-rollback
					$APPSPATH/$2/AM-rollback
					rm -f $APPSPATH/$2/AM-rollback
					rm -f $APPSPATH/$2/version
					echo $d >> $APPSPATH/$2/version
					echo -e "\nROLLBACK SUCCESSFUL!"
					exit
				elif test -f $APPSPATH/$2/$2-rollback; then
					cd $APPSPATH/$2
					./$2-rollback
					exit
				else
					echo " The option '$1' only works for https://github.com"; exit
				fi
			fi
		else
			echo ' No AM-updater available for '$2', cannot detect any URL!'; exit
		fi
	done

	shift
	;;

  '--user'|'appman')

  	_no_sudo

  	if [ $AMCLI == appman ] 2>/dev/null; then
  		echo " This function only works for AM"
  		exit
  	fi

  	RED='\033[0;31m'; LightBlue='\033[1;34m'
  	if test -f /opt/am/appman; then
		echo -e "Please run '${RED}alias am=/opt/am/appman\033[0m' to made 'AM' act exactly like 'AppMan' for\nthe current session or save this command to your '"'~/.bashrc'"' file to make the\nchanges permanent. Visit ${LightBlue}https://github.com/ivan-hc/appman\033[0m for more!"
  	else
  		echo -e 'USE "AM" AS A NON-PRIVILEGED USER, JUST AS YOU WOULD WITH "APPMAN"\n'
  		echo 'Hi '$USER'! You have decided to run "AM" as a non-privileged user.'
  		echo 'We will use "AppMan" for this purpose, it will be downloaded as /opt/am/appman'
  		echo 'After the download, an alias will be suggested, you will choose how to use it.'
  		echo 'This procedure will help you not to ruin the original “AM” installation.'
  		echo -e '\nAlternatively just install "AppMan" as a separate program.\nLearn more about "AppMan" at '${LightBlue}'https://github.com/ivan-hc/appman\033[0m\n'
  		read -p "Do you wish to proceed with the download? (y,N)" yn

  		case $yn in
  			'N'|'n') exit;;
  			'Y'|'y') 
  				_online_check
  				wget -q $AMREPO/APP-MANAGER -O /opt/am/appman && chmod a+x /opt/am/appman
  				echo -e "\nPlease run '${RED}alias am=/opt/am/appman\033[0m' to made 'AM' act exactly like 'AppMan' for\nthe current session or save this command to your '"'~/.bashrc'"' file to make the\nchanges permanent."
  		esac
  	fi;;

  'apikey')

	_no_sudo
	_online_check

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		case $2 in
		del|delete|remove)
			if test -f $AMPATH/ghapikey.txt; then
				rm -f $AMPATH/ghapikey.txt
				echo "The file $AMPATH/ghapikey.txt has been deleted"; exit
			else
				echo "No file named $AMPATH/ghapikey.txt has been found"; exit
			fi;;
		*)
			if [[ "$2" =~ ^(gh[ps]_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})$ ]]; then
				test_apikey_output_with_wget=$(wget -q --header "Authorization: token $2" 'https://api.github.com/repos/ivan-hc/AM-Application-Manager/releases' -O - | head -1)
				if [[ $(echo "$test_apikey_output_with_wget") ]]; then
					rm -f $AMPATH/ghapikey.txt
					echo "$2" >> $AMPATH/ghapikey.txt
					echo "Validation successful!"
					exit
				else
					echo "ERROR: This is not a valid key!"
    					exit
    				fi
			else
    				echo "ERROR: Wrong expression, validation failed!"
    				exit
			fi
			;;
		esac
	done

	shift
	;;

  'dev'|'devmode')

	_no_sudo
	_online_check

	while [ -n "$1" ]; do
		case $2 in
		on)
			if [ $AMCLI == am ] 2>/dev/null; then
				sed -i 's#$SUDOCOMMAND ./$arg | less -E #$SUDOCOMMAND ./$arg #g' $AMPATH/APP-MANAGER
			else
				sed -i 's#$SUDOCOMMAND ./$arg | less -E #$SUDOCOMMAND ./$arg #g' $DIR/appman
			fi
			echo " Developer Mode is ENABLED!"
			exit;;
		off)
			cd $AMPATH/.cache
			if [ $AMCLI == am ] 2>/dev/null; then
	  			wget -q $AMREPO/APP-MANAGER && chmod a+x ./APP-MANAGER
	  			cd ..
	  			mv $AMPATH/.cache/APP-MANAGER $AMPATH; chmod 777 $AMPATH
	  		else
	  			wget -q $AMREPO/APP-MANAGER -O appman && chmod a+x ./$AMCLI
	  			cd ..
	  			mv $AMPATH/.cache/$AMCLI $AMCLIPATH
	  		fi
			echo " Developer Mode is DISABLED!"
			exit;;
		''|*)
			echo -e " USAGE: $AMCLI $1 off	(to disable Developer Mode)\n        $AMCLI $1 on	(to enable Developer Mode)"
			exit
			;;
		esac
	done

	shift
	;;

  'lock')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		if test -f $APPSPATH/$2/AM-updater; then
			case $2 in
			*) 
				for var in $2; do 
					echo ""; read -p " Do you wish to keep $2  at the current version (y,N)?" yn
					case $yn in
					[Yy]* ) 
						echo "";  mv $APPSPATH/$2/AM-updater $APPSPATH/$2/AM-LOCK 1>/dev/null &&
						echo " $2 has been locked at current version!" && echo "" && exit; break;;
					[Nn]*|* ) 
						echo ""; echo " $2 is still updatable, exiting!"; echo ""; exit;; esac done;;
					esac
		else
			echo ""; echo ' '$(echo $AMCLI | tr a-z A-Z)' cannot manage updates for '$2', "AM-updater" file not found!'; echo ""; exit;
		fi
	done

	shift
	;;

  'test')

	_online_check

	while [ -n "$1" ]; do

		case $2 in
		'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
		esac

		if [ $AMCLI == am ] 2>/dev/null; then
  			$SUDOCOMMAND echo "" 2>/dev/null
  			if ! $SUDOCOMMAND -n true 2>/dev/null; then
				exit
  			fi
  		fi

		path2test="$2"
		arg=$(echo "$path2test" | sed 's:.*/::')

		function _test(){
			if test -f $APPSPATH/$arg/remove; then
				echo -e ' ERROR: an item named "'$arg'" is already installed!\n\n Please, use the command "'$AMCLI' -R '$arg'" to remove it!' && echo ""; exit
			else
				echo -e "-----------------------------------------------------------------------\n >> TEST THE INSTALLATION PROCESSES <<\n-----------------------------------------------------------------------"
				rm -R -f $AMPATH/.cache/installed
				mkdir -p $AMPATH/.cache; cp "$path2test" "$AMPATH"/.cache/"$arg"; cd $AMPATH/.cache
				chmod a+x ./$arg

				function _test_common_patch(){
					sed -i "s#wget #wget -q --show-progress #g" ./$arg; sed -i 's#$(wget -q --show-progress #$(wget -q #g' ./$arg; sed -i "s# -q -q # -q #g" ./$arg
					sed -i 's/currentuser=/#currentuser=/g' ./$arg
					sed -i 's/chown -R $currentuser/#chown -R $currentuser/g' ./$arg
					sed -i '/# MESSAGE/,$d' ./$arg
					sed -i "s# https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" ./$arg
				}

				function _test_appman_local_patch(){
					sed -i "s# /opt# $APPSPATH#g" ./$arg
					sed -i "s#/opt/#$APPSPATH/#g" ./$arg
					sed -i "s# /usr/local/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
					sed -i "s# /usr/bin# $(xdg-user-dir USER)/.local/bin#g" ./$arg
					sed -i "s# /usr/local/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
					sed -i "s# /usr/games# $(xdg-user-dir USER)/.local/bin#g" ./$arg
					sed -i "s# /usr/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
					sed -i "s# /usr/local/share/applications# $(xdg-user-dir USER)/.local/share/applications#g" ./$arg
					sed -i "s# /usr/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
					sed -i "s# /usr/local/share/pixmaps# $(xdg-user-dir USER)/.local/share/icons#g" ./$arg
				}

				if [ $AMCLI == appman ] 2>/dev/null; then
					sed -i "s#Icon=/opt#Icon=$(xdg-user-dir USER)/$APPSDIR#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
					sed -i "s#Exec=$arg#Exec=$(xdg-user-dir USER)/.local/bin/$arg#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
					sed -i 's#Exec=$APP#Exec='$(xdg-user-dir USER)'/.local/bin/$APP#g' ~/.local/share/applications/AM*.desktop 2> /dev/null
					sed -i "s#Exec=/usr/bin/#Exec=$(xdg-user-dir USER)/.local/bin/#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
					sed -i "s#Exec=/opt/#Exec=$(xdg-user-dir USER)/$APPSDIR/#g" ~/.local/share/applications/AM*.desktop 2> /dev/null
				fi

				if [ $AMCLI == appman ] 2>/dev/null; then
					_test_common_patch
					_test_appman_local_patch
				else
					if [[ $(grep "[[:space:]]ro[[:space:],]" /proc/mounts) ]]; then
						$SUDOCOMMAND mkdir -p /usr/local/share/applications
						sed -i 's#/usr/share/applications#/usr/local/share/applications#g' ./$arg
					fi
					_test_common_patch
				fi

				echo -ne " ◆ RUNNING YOUR INSTALLATION SCRIPT FOR $(echo '"'$arg'"' | tr a-z A-Z)... \r" &&
				$SUDOCOMMAND ./$arg && echo -ne "                                                                          \r" &&
				echo -ne " ◆ CONFIGURING $(echo '"'$arg'"' | tr a-z A-Z)...\r" && echo -ne "                                                                          \r" &&
				$SUDOCOMMAND chown -R $currentuser $APPSPATH/$(cd $APPSPATH && ls -td * | head -1) 2> /dev/null

				if test -d $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons; then
					find $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons/* -xtype l -delete 2> /dev/null
					if [ -z "$(ls -A $APPSPATH/$(cd $APPSPATH && ls -td * | head -1)/icons)" ]; then
						echo " Icon not found"
					fi 
				fi &&

				echo -ne " ◆ $(echo '"'$arg'"' | tr a-z A-Z) TESTED AND INSTALLED ($(du -sm $APPSPATH/$(cd $APPSPATH && ls -td * | head -1) | awk '{print $1}' ) MB OF DISK SPACE)\n"
				$SUDOCOMMAND rm $AMPATH/.cache/$arg
				unset $LATESTDIR
				LATESTDIR=$(cd $APPSPATH && ls -td * | head -1)

				if test -f $APPSPATH/$LATESTDIR/remove; then
					if test -d $APPSPATH/$LATESTDIR/tmp; then
						echo -e " 💀 ERROR DURING INSTALLATION TEST, REMOVED $(echo '"'$arg'"' | tr a-z A-Z)!"
						$AMCLIPATH -R $LATESTDIR 1> /dev/null
					else
						echo " ◆ $LATESTDIR" >> $AMPATH/.cache/installed &&
						chown -R $currentuser $AMPATH/.cache/about 2> /dev/null
					fi
				else
					echo " INSTALLATION TEST ABORTED!"
				fi
			fi
		}

		_test

		echo -e "\n-----------------------------------------------------------------------\n >> END OF ALL INSTALLATION TEST <<\n-----------------------------------------------------------------------\n\n THE FOLLOWING NEW PROGRAM HAVE BEEN INSTALLED:\n"
		echo -e "\n-----------------------------------------------------------------------" >> $AMPATH/.cache/installed
		grep -w -v "◆ am" 0<$AMPATH/.cache/installed;
		exit
	done

	shift
	;;

  'unlock')

	_no_sudo

	case $2 in
	'') echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit;;
	esac

	while [ -n "$1" ]; do
		if test -f $APPSPATH/$2/AM-LOCK; then
			case $2 in
			*) 
			for var in $2; do 
				echo ""; read -p " Do you wish to unlock updates for $2 (Y,n)?" yn

				case $yn in
				[Nn]* ) 
					echo ""; echo " $2 is still locked at current version!"; echo ""; exit;;
				[Yy]*|* ) 
					echo "";  mv $APPSPATH/$2/AM-LOCK $APPSPATH/$2/AM-updater 1>/dev/null &&
					echo ' '$2' can now receive updates, use the "'$AMCLI' -u" command to update everything!' && echo "" && exit; break;; esac done;;
				esac
		else
			echo ""; echo ' '$2' cannot be unlocked, "AM-LOCK" file not found!'; echo ""; exit;
		fi
	done

	shift
	;;

  'version'|'-v'|'--version')

  	echo "5.6.1-1";;

  *) exec $AMCLIPATH ;;

esac
