#!/bin/sh
# Creates the localized installer shortcut in the live session for:
# Xfce, LXDE, LXqt, Gnome, KDE, Mate, Cinnamon

# We query xdg-user-dir because the Desktop directory has different
# names for different languages
DESKTOP=$(xdg-user-dir DESKTOP)

# Create ~/Desktop just in case this runs before the xdg folder
# creation script.
mkdir -p -- "$DESKTOP"

# Among the Debian desktop environments, LXDE is the only one
# that behaves completely different.
if [ -f /usr/bin/lxsession ]; then
    echo "[Desktop Entry]" > "$DESKTOP/calamares-install-debian.desktop"
    echo "Type=Link" >> "$DESKTOP/calamares-install-debian.desktop"
    echo "Name=Install NewWorld" >> "$DESKTOP/calamares-install-debian.desktop"
    echo "Name[he]=התקנת ⁦NewWorld⁩" >> "$DESKTOP/calamares-install-debian.desktop"
    echo "Icon=/usr/share/newworld/newworld-logo.png" >> "$DESKTOP/calamares-install-debian.desktop"
    echo "URL=/usr/share/applications/calamares-install-debian.desktop" \
          >> "$DESKTOP/calamares-install-debian.desktop"
else
    cp /usr/share/applications/calamares-install-debian.desktop "$DESKTOP"
    # Xfce needs this executable otherwise it complains, everything
    # else doesn't seem to care either way.
    chmod +x "$DESKTOP/calamares-install-debian.desktop"
fi

# Set desktop launcher as trusted under Xfce (See: #1037299)
if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
    gio set --type=string "$DESKTOP/calamares-install-debian.desktop" \
        metadata::trusted true
    gio set --type=string "$DESKTOP/calamares-install-debian.desktop" \
        metadata::xfce-exe-checksum \
        "$(sha256sum "$DESKTOP/calamares-install-debian.desktop" | cut -f1 -d' ')"
    touch "$DESKTOP/calamares-install-debian.desktop"
fi

# Preload the installer at idle I/O priority when the binary is available.
if [ -x /usr/bin/calamares ] && command -v ionice >/dev/null 2>&1; then
    ionice -c 3 cat /usr/bin/calamares > /dev/null
    ldd /usr/bin/calamares | awk '$2 == "=>" && $3 ~ /^\// { print $3 }' |
        while IFS= read -r library; do
            ionice -c 3 cat "$library" > /dev/null
        done
fi
