#!/bin/sh
# Builds an AppImage from the staged tarball tree.
#
# NOTE: an AppImage does NOT lower the glibc floor - that comes from the bundled
# libglfw.so.3 (GLIBC_2.34) and AppImages do not bundle glibc. It is a packaging
# convenience (single file, desktop integration), not a compatibility fix. On
# distros that ship only fuse3 (Arch among them) it also needs fuse2 installed,
# or running with --appimage-extract-and-run. The tarball has neither constraint.
set -eu
OUT="${OUT:-dist}"
NAME="ifsrenderer-linux-x86_64"
STAGE="$OUT/$NAME"
APPDIR="$OUT/IFSRenderer.AppDir"
TOOL="${APPIMAGETOOL:-$HOME/.cache/ifsrenderer/appimagetool}"
[ -d "$STAGE" ] || { echo "run build-tarball.sh first" >&2; exit 1; }
mkdir -p "$(dirname "$TOOL")"
if [ ! -x "$TOOL" ]; then
# Cached across builds: the CI workspace is a fresh temp dir every time.
echo "fetching appimagetool"
curl -fsSL -o "$TOOL" \
https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage
chmod +x "$TOOL"
fi
rm -rf "$APPDIR"
mkdir -p "$APPDIR/usr/bin" "$APPDIR/usr/share/applications" "$APPDIR/usr/share/icons/hicolor/128x128/apps"
cp -r "$STAGE"/. "$APPDIR/usr/bin/"
cp Assets/icon_128.png "$APPDIR/usr/share/icons/hicolor/128x128/apps/ifsrenderer.png"
cp Assets/icon_128.png "$APPDIR/ifsrenderer.png"
cat > "$APPDIR/usr/share/applications/ifsrenderer.desktop" <<'DESKTOP'
[Desktop Entry]
Type=Application
Name=IFSRenderer
Comment=3D IFS fractal renderer and editor
Exec=ifsrenderer
Icon=ifsrenderer
Categories=Graphics;
Terminal=false
DESKTOP
cp "$APPDIR/usr/share/applications/ifsrenderer.desktop" "$APPDIR/ifsrenderer.desktop"
cat > "$APPDIR/AppRun" <<'APPRUN'
#!/bin/sh
DIR=$(CDPATH= cd -- "$(dirname -- "$(readlink -f -- "$0")")" && pwd)
exec "$DIR/usr/bin/ifsrenderer" "$@"
APPRUN
chmod +x "$APPDIR/AppRun"
ARCH=x86_64 "$TOOL" --appimage-extract-and-run "$APPDIR" "$OUT/IFSRenderer-x86_64.AppImage"
echo "built $OUT/IFSRenderer-x86_64.AppImage"