#!/bin/sh
# Create the installer launcher only in the current Dracut Live user's overlay.

set -eu

[ "$(id -u)" -ne 0 ] || exit 0
[ -d /cdrom ] || exit 0
test -f /run/anduinos-live/environment || exit 0

desktop_dir="$(xdg-user-dir DESKTOP 2>/dev/null || true)"
[ -n "$desktop_dir" ] || desktop_dir="$HOME/Desktop"

# Never let a malformed XDG setting write outside the Live user's home.
case "$desktop_dir" in
    "$HOME"/*) ;;
    *) exit 0 ;;
esac

source=/usr/share/applications/anduinos-installer-beta.desktop
destination="$desktop_dir/anduinos-installer-beta.desktop"
[ -f "$source" ] || exit 0

mkdir -p "$desktop_dir"
install -m 0755 "$source" "$destination"

# GNOME/DING uses this metadata to avoid presenting a misleading
# "Untrusted application launcher" file on the Live desktop.
gio set "$destination" metadata::trusted true >/dev/null 2>&1 || true

# The Live session is an installation environment: present the installer
# immediately after GNOME finishes logging in.  The application ID keeps
# repeated activations single-instance, while the Live marker guards above ensure
# this never runs on an installed system.
[ -x /usr/bin/anduinos-installer-beta ] || exit 0
exec /usr/bin/anduinos-installer-beta
