#!/bin/sh

drs=
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] &&
   command -v dbus-run-session >/dev/null 2>&1; then
    drs=dbus-run-session
fi

if [ -n "${MANAGERPID:-}" ] && [ "${SYSTEMD_EXEC_PID:-}" = "$$" ]; then
  exec ${drs} /usr/bin/umbriel "$@"
fi

login_reentry=false
if [ "${UMBRIEL_LOGIN_REENTRY:-}" = 1 ]; then
  login_reentry=true
  unset UMBRIEL_LOGIN_REENTRY
  unset WAYLAND_DISPLAY WAYLAND_SOCKET DISPLAY UMBRIEL_SOCKET \
    XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE
fi

if [ "$login_reentry" = false ]; then
  if [ -n "${WAYLAND_DISPLAY:-}" ] || [ -n "${WAYLAND_SOCKET:-}" ] ||
    [ -n "${DISPLAY:-}" ]; then
    exec ${drs} /usr/bin/umbriel "$@"
  fi

  login_shell_kind=
  if [ -n "${SHELL:-}" ] && [ -x "$SHELL" ] && [ -r /etc/shells ]; then
    case "${SHELL##*/}" in
      fish) login_shell_kind=fish ;;
      ash | bash | dash | ksh | ksh93 | mksh | oksh | sh | yash | zsh)
        login_shell_kind=bourne
        ;;
    esac
    if [ -n "$login_shell_kind" ]; then
      shell_listed=false
      while IFS= read -r candidate; do
        if [ "$candidate" = "$SHELL" ]; then
          shell_listed=true
          break
        fi
      done < /etc/shells
      if [ "$shell_listed" = false ]; then
        login_shell_kind=
      fi
    fi
  fi

  # A display manager starts the session launcher directly. Enter the user's
  # shell once so its noninteractive login profile can extend the environment.
  case "$login_shell_kind" in
    fish)
      UMBRIEL_LOGIN_REENTRY=1 exec "$SHELL" -l -c 'exec $argv' "$0" "$@"
      ;;
    bourne)
      UMBRIEL_LOGIN_REENTRY=1 exec "$SHELL" -l -c 'exec "$@"' "$SHELL" "$0" "$@"
      ;;
  esac
fi

if ! command -v systemctl >/dev/null 2>&1 ||
  ! systemctl --user show-environment >/dev/null 2>&1; then
  exec ${drs} /usr/bin/umbriel "$@"
fi

if systemctl --user --quiet is-active umbriel.service; then
  echo "An Umbriel session is already running." >&2
  exit 1
fi

systemctl --user reset-failed
systemctl --user unset-environment WAYLAND_DISPLAY WAYLAND_SOCKET DISPLAY \
  XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE UMBRIEL_SOCKET
awk 'BEGIN {
  for (name in ENVIRON) {
    if (name != "AWKPATH" && name != "AWKLIBPATH" &&
        name ~ /^[A-Za-z_][A-Za-z0-9_]*$/)
      print name
  }
}' </dev/null | xargs -r systemctl --user import-environment
if command -v dbus-update-activation-environment >/dev/null 2>&1; then
  dbus-update-activation-environment --all
fi

systemctl --user --wait start umbriel.service
umbriel_status=$?

systemctl --user start --job-mode=replace-irreversibly umbriel-shutdown.target
systemctl --user unset-environment WAYLAND_DISPLAY WAYLAND_SOCKET DISPLAY \
  XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE UMBRIEL_SOCKET

exit "$umbriel_status"
