#!/bin/bash
# This script initializes Regolith Xresources and launches i3.

set -Eeu -o pipefail

source /usr/lib/regolith/regolith-session-common.sh

gdm3_xrdb_fixup() {
    if xrescat testkey defaultval > /dev/null 2>&1; then
        echo "xrdb appears to be property initialized, skipping xrdb init"
    else # TODO(kgilmer) ~ consider further predication upon distro/codename if reason to believe following codeblock might cause harm
        echo "xrdb appears to not be running. Manually initializing.."

        load_system_xres
    fi
}
# Check if NOTIFY_SOCKET is available. This is used as a
# test to see if the session uses systemd
if [ -z "${NOTIFY_SOCKET-}" ]; then
  # Register with gnome-session so that it does not kill the whole session thinking it is dead.
  # When gnome-session respawns us after i3 crashed, the client is still
  # registered and this fails with AlreadyRegistered; i3 must start anyway.
  if [ -n "${DESKTOP_AUTOSTART_ID-}" ]; then
      dbus-send --print-reply --session --dest=org.gnome.SessionManager "/org/gnome/SessionManager" org.gnome.SessionManager.RegisterClient "string:Regolith-X11" "string:$DESKTOP_AUTOSTART_ID" \
          || echo "Could not register with gnome-session, continuing"
  fi

  # Add the environment.d variables the login environment does not set
  import_environment_d
fi

# --------- Script Main -----------
clear_wm_sockets    # SWAYSOCK and I3SOCK of an earlier session

gdm3_xrdb_fixup     # Remove when https://bugs.launchpad.net/ubuntu/+source/gdm3/+bug/1955850 is fixed
load_standard_xres  # ~/.Xresources
load_regolith_xres  # ~/.config/regolith3/Xresources

UPDATE_FLAG_DIR="$HOME/.config/regolith3/flags"
if [ ! -e "$UPDATE_FLAG_DIR" ]; then
    # flag file has not been created, meaning this is first session load
    # in this case, we need to configure some desktop settings from the look
    LOADER_PATH="$($RESOURCE_GETTER regolith.look.loader)"
    if [ -n  "$LOADER_PATH" ]; then
        # Load the look-specific script that has a function called load_look()
        source "$LOADER_PATH"
        # Call look function to update UI
        load_look
    fi
fi

resolve_default_config_file
echo "Regolith is launching i3 with $I3_CONFIG_FILE"
i3 -c "$I3_CONFIG_FILE"

# Run user's post logout script if script. Post logout means that the X session is closed at this point.
if [ -e "$USER_POST_LOGOUT_SCRIPT_FILE" ] ; then
    echo "Regolith is launching user post-logout file $USER_POST_LOGOUT_SCRIPT_FILE"
	timeout --verbose 5 bash "$USER_POST_LOGOUT_SCRIPT_FILE"
fi
