#!/bin/bash

set -e

die() {
  echo >&2 "!! $*"
  exit 1
}

DEFAULT_SESSION_CONFIG="/etc/default/sk-chos-desktop-session"
if [ -f "$DEFAULT_SESSION_CONFIG" ]; then
  source "$DEFAULT_SESSION_CONFIG"
fi

SK_CHOS_SESSION=${SK_CHOS_SESSION:-"wayland"}

echo "SK_CHOS_SESSION=$SK_CHOS_SESSION" | sudo systemd-cat -t os-session-select

SENTINEL_FILE="steamos-session-select"
RETURN_SESSION_FILE="skorionos-session-return"

# If we proceed, execute this
if [[ "$SK_CHOS_SESSION" == "wayland" ]]; then
  CHAINED_SESSION="/usr/bin/startplasma-wayland"
else
  CHAINED_SESSION="/usr/bin/sx /usr/bin/startplasma-x11"
fi

# If we decide the sentinel is consumed, execute this command instead and fail
RESTORE_SESSION=(/usr/lib/os-session-select) # No arguments restores the session

# Find or check config return session
function check_return() {
  if [[ -z ${HOME+x} ]]; then
    echo >&2 "$0: No \$HOME variable!"
    # Rather than break we'll just launch plasma and hope for the best?
    return 0
  fi
  local config_dir="${XDG_CONF_DIR:-"$HOME/.config"}"
  (
    cd "$HOME"
    cd "$config_dir"
    revert_value="$(cat "$RETURN_SESSION_FILE")"
    case "$revert_value" in
    gamescope-session-opengamepadui | gamescope-session-steam-plus | gamescope-session-steam)
      echo "$revert_value"
      ;;
    *)
      echo "gamescope-session-steam"
      ;;
    esac
    rm "$RETURN_SESSION_FILE"
  ) || return 1 # If we couldn't read the value or it wasn't what we wanted

  # Found value and removed it, we're good to continue
  return 0
}

# Find or check config sentinel
function check_sentinel() {
  if [[ -z ${HOME+x} ]]; then
    echo >&2 "$0: No \$HOME variable!"
    # Rather than break we'll just launch plasma and hope for the best?
    return 0
  fi

  local config_dir="${XDG_CONF_DIR:-"$HOME/.config"}"
  (
    cd "$HOME"
    cd "$config_dir"
    sentinel_value="$(cat "$SENTINEL_FILE")"
    if [[ "$sentinel_value" == "wayland" ]]; then
      if [[ "$SK_CHOS_SESSION" == "wayland" ]]; then
        echo "/usr/bin/startplasma-wayland"
      else
        echo "/usr/bin/sx /usr/bin/startplasma-x11"
      fi
    else
      return 1
    fi
    rm "$SENTINEL_FILE"

  ) || return 1 # If we couldn't read the value or it wasn't what we wanted

  # Found value and removed it, we're good to continue
  return 0
}

function delay_set_return_session() {
  local session="$1"
  systemd-run --on-active=5s bash -c "sudo /usr/bin/skorion-session-only-update $session"
}

if CONFIGURED_CHAINED_SESSION=$(check_sentinel); then
  # We found and consumed the oneshot sentinel, proceed to launch plasma
  echo >&2 "$0: Found and removed sentinel file for one-shot desktop, proceeding to launch"
  exec "$CONFIGURED_CHAINED_SESSION"
  # if [[ "$SK_CHOS_SESSION" == "wayland" ]]; then
  #   exec "$CONFIGURED_CHAINED_SESSION"
  # else
  #   return_file="$HOME/.config/${RETURN_SESSION_FILE}"
  #   return_session=$(cat "$return_file")
  #   delay_set_return_session $return_session || true
  #   sudo skorion-session-use-sddm plasmax11
  # fi

else
  if CONFIGURED_RETURN_SESSION=$(check_return); then
    if /usr/lib/os-session-select "$CONFIGURED_RETURN_SESSION"; then
      echo "Session restored successfully"
      exit 0
    else
      echo "Failed to restore session"
    fi
  fi
  # Session restore should've stopped us, if it is broken at least let plasma continue to open

  echo >&2 "$0: Sentinel value not found, executing session-select to restore session"
  "${RESTORE_SESSION[@]}" || echo >&2 "$0: !! Failed to restore previous session, executing chained session"
  # Session restore should've stopped us, if it is broken at least let plasma continue to open
  exec "$CHAINED_SESSION"
fi
