#!/bin/sh

set -eu

ARCH=$(uname -m)
REPOSITORY=${GITHUB_REPOSITORY:-Vysp3r/ProtonPlus}
APPIMAGE_APPDIR=${PROTONPLUS_APPIMAGE_APPDIR:-./AppDir}
APPIMAGE_LOCALE_SOURCE=${PROTONPLUS_APPIMAGE_LOCALE_SOURCE:-/usr/share/locale}

# Build and install locally to /usr
if [ -f build-appimage/meson-private/coredata.dat ]; then
    meson setup build-appimage --wipe --prefix=/usr
else
    meson setup build-appimage --prefix=/usr
fi
meson compile -C build-appimage
meson install -C build-appimage

VERSION=$(meson introspect --projectinfo build-appimage | python3 -c \
    'import json, sys; print(json.load(sys.stdin)["version"])')
export ARCH VERSION
export OUTPATH=./dist
export ADD_HOOKS="self-updater.bg.hook"
export UPINFO="gh-releases-zsync|${REPOSITORY%/*}|${REPOSITORY#*/}|latest|*${ARCH}.AppImage.zsync"
export ICON=/usr/share/icons/hicolor/256x256/apps/com.vysp3r.ProtonPlus.png
export DESKTOP=/usr/share/applications/com.vysp3r.ProtonPlus.desktop
export STARTUPWMCLASS=com.vysp3r.ProtonPlus
export GTK_CLASS_FIX=1
export DEPLOY_P11KIT=1

# Deploy dependencies
quick-sharun \
    /usr/bin/protonplus \
    /usr/lib/gio/modules/libgiognomeproxy.so \
    /usr/lib/gio/modules/libgiognutls.so \
    /usr/lib/gio/modules/libgiolibproxy.so

# Quick-Sharun matches catalogs against the lowercase executable name and
# removes ProtonPlus' case-sensitive gettext domain during locale debloating.
# Restore only ProtonPlus catalogs instead of deploying the full locale tree.
find "$APPIMAGE_LOCALE_SOURCE" -type f \
    -path '*/LC_MESSAGES/com.vysp3r.ProtonPlus.mo' \
    -exec sh -c '
        locale_source=$1
        appdir=$2
        shift 2

        for catalog do
            relative_path=${catalog#"$locale_source"/}
            destination=$appdir/share/locale/$relative_path
            mkdir -p "${destination%/*}"
            cp "$catalog" "$destination"
        done
    ' sh "$APPIMAGE_LOCALE_SOURCE" "$APPIMAGE_APPDIR" {} +

# Turn AppDir into AppImage
quick-sharun --make-appimage
