#!/bin/sh

set -u
APP=SAMPLE
 
# 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" > "/opt/$APP/remove"
printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> "/opt/$APP/remove"
chmod a+x "/opt/$APP/remove"

# DOWNLOADING THE DEPENDENCIES
wget -q "$(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1)" -O appimagetool
wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage
chmod a+x ./appimagetool ./pkg2appimage

# CREATING THE APPIMAGE: APPDIR FROM A RECIPE...
echo "app: SAMPLE
binpatch: true

ingredients:
  dist: oldstable
  sources:
    - deb http://deb.debian.org/debian/ oldstable main contrib non-free
    - deb http://deb.debian.org/debian-security/ oldstable-security main contrib non-free
    - deb http://deb.debian.org/debian oldstable-updates main contrib non-free
    - deb http://deb.debian.org/debian oldstable-backports main contrib non-free
  packages:
    - SAMPLE" >> recipe.yml

cp /opt/"$APP"/tmp/recipe.yml /opt/"$APP"/recipe.yml

./pkg2appimage ./recipe.yml

# ...DOWNLOADING LIBUNIONPRELOAD...
#wget https://github.com/project-portable/libunionpreload/releases/download/amd64/libunionpreload.so
#chmod a+x libunionpreload.so
#mv ./libunionpreload.so ./$APP/$APP.AppDir/

# ...REPLACING THE EXISTING APPRUN WITH A CUSTOM ONE...
rm -R -f ./"$APP"/"$APP".AppDir/AppRun
cat > AppRun << 'EOF'
#!/bin/sh
APP=SAMPLE
HERE="$(dirname "$(readlink -f "${0}")")"
export UNION_PRELOAD="${HERE}"
#export LD_PRELOAD="${HERE}/libunionpreload.so"
export PATH="${HERE}"/usr/bin/:"${HERE}"/usr/sbin/:"${HERE}"/usr/games/:"${HERE}"/bin/:"${HERE}"/sbin/:"${PATH}"
##export LD_LIBRARY_PATH="${HERE}"/usr/lib/:"${HERE}"/usr/lib/i386-linux-gnu/:"${HERE}"/usr/lib/x86_64-linux-gnu/:"${HERE}"/lib/:"${HERE}"/lib/i386-linux-gnu/:"${HERE}"/lib/x86_64-linux-gnu/:"${LD_LIBRARY_PATH}"
#export LD_LIBRARY_PATH=/lib/:/lib64/:/lib/x86_64-linux-gnu/:/usr/lib/:"${HERE}"/usr/lib/:"${HERE}"/usr/lib/i386-linux-gnu/:"${HERE}"/usr/lib/x86_64-linux-gnu/:"${HERE}"/lib/:"${HERE}"/lib/i386-linux-gnu/:"${HERE}"/lib/x86_64-linux-gnu/:"${LD_LIBRARY_PATH}"
export PYTHONPATH="${HERE}"/usr/share/pyshared/:"${PYTHONPATH}"
export PYTHONHOME="${HERE}"/usr/
export XDG_DATA_DIRS="${HERE}"/usr/share/:"${XDG_DATA_DIRS}"
export PERLLIB="${HERE}"/usr/share/perl5/:"${HERE}"/usr/lib/perl5/:"${PERLLIB}"
export GSETTINGS_SCHEMA_DIR="${HERE}"/usr/share/glib-2.0/schemas/:"${GSETTINGS_SCHEMA_DIR}"
export QT_PLUGIN_PATH="${HERE}"/usr/lib/qt4/plugins/:"${HERE}"/usr/lib/i386-linux-gnu/qt4/plugins/:"${HERE}"/usr/lib/x86_64-linux-gnu/qt4/plugins/:"${HERE}"/usr/lib32/qt4/plugins/:"${HERE}"/usr/lib64/qt4/plugins/:"${HERE}"/usr/lib/qt5/plugins/:"${HERE}"/usr/lib/i386-linux-gnu/qt5/plugins/:"${HERE}"/usr/lib/x86_64-linux-gnu/qt5/plugins/:"${HERE}"/usr/lib32/qt5/plugins/:"${HERE}"/usr/lib64/qt5/plugins/:"${QT_PLUGIN_PATH}"
EXEC=$(grep -e '^Exec=.*' "${HERE}"/*.desktop | head -n 1 | cut -d "=" -f 2- | sed -e 's|%.||g')
##exec ${EXEC} "$@"
#exec "${HERE}"CUSTOMPATH "$@"
EOF
chmod a+x AppRun
cp ./AppRun /opt/"$APP"/
mv ./AppRun ./"$APP"/"$APP".AppDir

# IMPORT THE LAUNCHER AND THE ICON TO THE APPDIR IF THEY NOT EXIST
if test -f ./"$APP"/"$APP".AppDir/*.desktop; then
	echo "The desktop file exists"
else
	echo "Trying to get the .desktop file"
	cp ./"$APP"/"$APP".AppDir/usr/share/applications/*"$(ls . | grep -i "$APP" | cut -c -4)"*desktop ./"$APP"/"$APP".AppDir/ 2>/dev/null
fi

ICONNAME=$(cat ./"$APP"/"$APP".AppDir/*desktop | grep "Icon=" | head -1 | cut -c 6-)
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/22x22/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/24x24/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/32x32/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/48x48/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/64x64/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/128x128/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/256x256/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/512x512/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/scalable/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
cp ./"$APP"/"$APP".AppDir/usr/share/applications/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null

# CLEAN METAINFO DIRECTORY
metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
if [ -z "$metainfodir" ]; then
	return
else
	cd "$metainfodir" || return
	rm -R -f ./*.xml
	cd - > /dev/null || return
fi

# ...EXPORT THE APPDIR TO AN APPIMAGE!
ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
underscore=_
mkdir version
mv ./"$APP"/"$APP""$underscore"*.deb ./version/
version=$(ls /opt/"$APP"/tmp/version)
echo "$version" >> /opt/"$APP"/version

cd ..
mv ./tmp/*.AppImage ./"$APP"
chmod a+x ./"$APP"
rm -R -f ./tmp

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

# SCRIPT TO UPDATE THE PROGRAM
cat >> /opt/"$APP"/AM-updater << 'EOF'
#!/bin/sh
APP=SAMPLE
initial=$(echo $APP | head -c 1)
version0=$(cat /opt/"$APP"/version)
url="http://http.us.debian.org/debian/pool/main/$initial/"$APP"/$version0"
if curl --output /dev/null --silent --head --fail "$url"; then
	echo "Update not needed, exit!"
else
	notify-send "A new version of $APP is available, please wait!"
	mkdir /opt/"$APP"/tmp
	cd /opt/"$APP"/tmp || exit 1
	wget -q $(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1) -O appimagetool
	wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage # 64 BIT ONLY (comment to disable)
	chmod a+x ./appimagetool ./pkg2appimage
	cp /opt/"$APP"/recipe.yml /opt/"$APP"/tmp/recipe.yml
	./pkg2appimage ./recipe.yml
	#wget https://github.com/project-portable/libunionpreload/releases/download/amd64/libunionpreload.so
	#chmod a+x libunionpreload.so
	#mv ./libunionpreload.so ./"$APP"/"$APP".AppDir/
	rm -R -f ./"$APP"/"$APP".AppDir/AppRun
	cp /opt/"$APP"/AppRun ./"$APP"/"$APP".AppDir/
	metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
	if [ -z "$metainfodir" ]; then
		return
	else
		cd "$metainfodir" || return
		rm -R -f ./*.xml
		cd - > /dev/null || return
	fi
	ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
	underscore=_
	mkdir version
	mv ./"$APP"/"$APP""$underscore"*.deb ./version/
	cd ..
	version=$(ls /opt/"$APP"/tmp/version)
	if test -f ./tmp/version/$version; then rm -R -f ./version
	fi
	echo "$version" >> /opt/"$APP"/version
	mv ./tmp/*.AppImage ./"$APP"
	chmod a+x ./"$APP"
	rm -R -f ./tmp
	notify-send "$APP is updated!"
fi
EOF
chmod a+x /opt/"$APP"/AM-updater

# LAUNCHER & ICON
cd "/opt/$APP" || exit 1
./"$APP" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$APP".desktop
./"$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
	if [ -L ./"$APP".desktop ]; then
		LINKPATH=$(readlink ./"$APP".desktop)
		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./"$APP".desktop
	fi
	if [ -L ./DirIcon ]; then
		LINKPATH=$(readlink ./DirIcon)
		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./DirIcon
	fi
	[ ! -L ./"$APP".desktop ] && [ ! -L ./DirIcon ] && break
	COUNT=$((COUNT + 1))
done
sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g" ./"$APP".desktop
mv ./"$APP".desktop /usr/local/share/applications/"$APP"-AM.desktop && mv ./DirIcon ./icons/"$APP" 1>/dev/null
rm -R -f ./squashfs-root
