#!/bin/sh

SELF=$(readlink -f "$0")
HERE=${SELF%/*}

# Install icon into XDG dirs so GNOME/KDE pick it up the first time.
ICON_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"
if [ ! -f "$ICON_DIR/simpmusic.png" ] || [ "$HERE/simpmusic.png" -nt "$ICON_DIR/simpmusic.png" ]; then
    mkdir -p "$ICON_DIR"
    cp "$HERE/simpmusic.png" "$ICON_DIR/simpmusic.png"
    gtk-update-icon-cache -f -t "$HOME/.local/share/icons/hicolor" 2>/dev/null || true
fi

# Cap glibc's per-thread malloc arenas.
#
# glibc spawns a fresh 64 MB-aligned arena whenever it sees mutex contention, up to
# 8 x nproc of them, and never gives an arena back to the OS. Measured on a 20-core
# box: 161 arenas holding 1.45 GB of a 1.9 GB RSS, while the JVM heap was using only
# 121 MB of its 512 MB cap. Pinning the count to 2 took that to ~5 arenas. The cost is
# more allocator lock contention, which this workload does not notice - the audio path
# is native and its buffers are long-lived.
#
# Linux-only by construction: this file only exists inside the AppImage. macOS and
# Windows tune their allocators elsewhere, and MemoryTrimmer covers all three at runtime.
export MALLOC_ARENA_MAX=2

cd "$HERE"
exec bin/simpmusic "$@"
