#!/bin/sh

# AM INSTALL SCRIPT VERSION 3.6
set -u
APP=SAMPLE
SITE="REPLACETHIS"; GHRC_REF="ITEM"

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/tmp" "/opt/$APP/icons" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > ../remove
#printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> ../remove
chmod a+x ../remove || exit 1

# DOWNLOAD AND PREPARE THE APP, $version is also used for updates
version=$(FUNCTION)
curl -Ls "https://raw.githubusercontent.com/ivan-hc/AM/main/tools/ghcr_dl.sh" | sh -s -- "$GHRC_REF" "$APP" || exit 1



cd ..
mv ./tmp/* ./
rm -R -f ./tmp || exit 1
echo "$version" > ./version
chmod a+x ./$APP || exit 1

# LINK TO PATH
ln -s "/opt/$APP/$APP" "/usr/local/bin/$APP"

# Determine the type of app
if ! head -c10 "/opt/$APP/$APP" 2>/dev/null | grep -qa '^.ELF....AI$'; then
	# SCRIPT TO UPDATE THE PROGRAM
	cat <<-'HEREDOC' >> ./AM-updater
	#!/bin/sh
	set -u
	APP=SAMPLE
	SITE="REPLACETHIS"; GHRC_REF="ITEM"
	version0=$(cat "/opt/$APP/version")
	version=$(FUNCTION)
	[ -n "$version" ] || { echo "Error getting link"; exit 1; }
	if [ "$version" != "$version0" ]; then
	        mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	        notify-send "A new version of $APP is available, please wait"
	        curl -Ls "https://raw.githubusercontent.com/ivan-hc/AM/main/tools/ghcr_dl.sh" | sh -s -- "$GHRC_REF" "$APP" || exit 1
	        cd ..
	        mv --backup=t ./tmp/* ./
	        chmod a+x ./"$APP" || exit 1
	        echo "$version" > ./version
	        rm -R -f ./tmp ./*~
	        notify-send "$APP is updated!"
	else
	        echo "Update not needed!"
	fi
	HEREDOC
	chmod a+x ./AM-updater || exit 1
else
	# SCRIPT TO UPDATE THE PROGRAM
	cat <<-'HEREDOC' >> ./AM-updater
	#!/bin/sh
	set -u
	APP=SAMPLE
	SITE="REPLACETHIS"; GHRC_REF="ITEM"
	version0=$(cat "/opt/$APP/version")
	version=$(FUNCTION)
	[ -n "$version" ] || { echo "Error getting link"; exit 1; }
	if command -v appimageupdatetool >/dev/null 2>&1; then
	        cd "/opt/$APP" || exit 1
	        appimageupdatetool -Or ./"$APP" && chmod a+x ./"$APP" && echo "$version" > ./version && exit 0
	fi
	if [ "$version" != "$version0" ]; then
	        mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	        notify-send "A new version of $APP is available, please wait"
	        curl -Ls "https://raw.githubusercontent.com/ivan-hc/AM/main/tools/ghcr_dl.sh" | sh -s -- "$GHRC_REF" "$APP" || exit 1
	        # Use tar fx ./*tar* here for example in this line in case a compressed file is downloaded.
	        cd ..
	        mv --backup=t ./tmp/*mage ./"$APP"
	        chmod a+x ./"$APP" || exit 1
	        echo "$version" > ./version
	        rm -R -f ./*zs-old ./*.part ./tmp ./*~
	        notify-send "$APP is updated!"
	else
	        echo "Update not needed!"
	fi
	HEREDOC
	chmod a+x ./AM-updater || exit 1

	# LAUNCHER & ICON
	./"$APP" --appimage-extract *.desktop 1>/dev/null
	./"$APP" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
	COUNT=0
	while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
		for f in squashfs-root/*.desktop; do
			FILE="$f"
			if [ -L "$f" ]; then
				LINKPATH="$(readlink "$f" | sed 's|^\./||' 2>/dev/null)"
				./"$APP" --appimage-extract "$LINKPATH" 1>/dev/null
				FILE="./squashfs-root/$LINKPATH"
			fi
			if grep -qi '^NoDisplay=true$' "$FILE" 2>/dev/null; then mv "$FILE" ./"$APP"-handler-AM.desktop && printf '\n%s' "rm -f /usr/local/share/applications/$APP-handler-AM.desktop" >> ./remove
			else [ ! -f ./"$APP"-AM.desktop ] && mv "$FILE" ./"$APP"-AM.desktop || mv "$FILE" ./"$APP"-"$COUNT"-AM.desktop && printf '\n%s' "rm -f /usr/local/share/applications/$APP-$COUNT-AM.desktop" >> ./remove
			fi
		done
		if [ -L ./DirIcon ]; then
			LINKPATH="$(readlink ./DirIcon | sed 's|^\./||' 2>/dev/null)"
			./"$APP" --appimage-extract "$LINKPATH" 1>/dev/null && mv ./squashfs-root/"$LINKPATH" ./DirIcon
		fi
		[ ! -L ./"$APP"-AM.desktop ] && [ ! -L ./DirIcon ] && break
		COUNT=$((COUNT + 1))
	done
	for f in ./*-AM.desktop; do [ "$f" != ./"$APP"-AM.desktop ] && [ -f "$f" ] && [ "$(sort ./"$APP"-AM.desktop)" = "$(sort "$f")" ] && rm -f "$f"; done
	sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g; s# --no-sandbox##g" ./*-AM.desktop
	mv ./*-AM.desktop /usr/local/share/applications/ && mv ./DirIcon ./icons/"$APP" 1>/dev/null
	rm -R -f ./squashfs-root
fi
