#!/bin/bash

# Mimic behavior of old openSUSE-welcome with "Show on next boot" unchecked

LAUNCHER_XDG_FILE=org.opensuse.opensuse_welcome_launcher.desktop
LEGACY_XDG_FILE=org.opensuse.opensuse_welcome.desktop
HIDDEN_LAUNCHER_XDG_FILE="/usr/share/opensuse-welcome-launcher/org.opensuse.opensuse_welcome_launcher-hidden.desktop"

# The legacy autostart was dropped let's remove it from homedir
if [[ -e "$HOME/.config/autostart/${LEGACY_XDG_FILE}" && \
      ! -e "/etc/xdg/autostart/${LEGACY_XDG_FILE}" ]]; then
    rm -f "$HOME/.config/autostart/${LEGACY_XDG_FILE}"
fi

# Show only once
cp ${HIDDEN_LAUNCHER_XDG_FILE} ${HOME}/.config/autostart/${LAUNCHER_XDG_FILE}


detect_de() {
    if [ -n "$XDG_CURRENT_DESKTOP" ]; then
        echo "$XDG_CURRENT_DESKTOP" | tr '[:upper:]' '[:lower:]'
    elif [ -n "$DESKTOP_SESSION" ]; then
        echo "$DESKTOP_SESSION" | tr '[:upper:]' '[:lower:]'
    else
        echo ""
    fi
}

de=$(detect_de)
welcome_binary=""

# Prefer Session specific greeter
if [[ "$de" == *plasma* ]]; then
    welcome_binary=$(command -v plasma-welcome)
elif [[ "$de" == *gnome* ]]; then
    welcome_binary=$(command -v gnome-tour)
fi

# Fallback to opensuse-welcome if nothing else is found
if [ -z "$welcome_binary" ]; then
    welcome_binary=$(command -v opensuse-welcome)
fi

# XXX: hack for the initial integration
# keep legacy behavior and only trigger opensuse-welcome
# rest would be the next step
welcome_binary=$(command -v opensuse-welcome)

if [ ! -z "$welcome_binary" ]; then
    $welcome_binary
fi
