#!/bin/sh

COMPOSITOR="/usr/bin/phoc"
PHOC_INI="/usr/share/phosh/phoc.ini"
PHOSH_SESSION_BIN=${PHOSH_SESSION_BIN:-gnome-session}

help()
{
  cat <<EOF
phosh-session - Session start script for Phosh

This is usually invoked by a display manager but you can
also run it from a tty.

Usage: phosh
EOF
  exit 0
}


version()
{
   echo "0.55.0"
   exit 0
}

case "$1" in
  -h|-\?|--help)
    help
    ;;
  --version)
    version
    ;;
esac

if [ -x /usr/bin/phoc ]; then
  COMPOSITOR=/usr/bin/phoc
fi

if [ -f /etc/phosh/phoc.ini ]; then
  PHOC_INI=/etc/phosh/phoc.ini
fi

if [ -f "$HOME/.phoshdebug" ]; then
  echo "WARN: .phoshdebug found."
  . "$HOME/.phoshdebug"
fi

if which systemd-cat >/dev/null 2>&1; then
  SYSTEMD_CAT="systemd-cat -t phoc"
fi

if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
  echo "INFO: No DBus user session found, spawning it."
  DBUS_RUN_SESSION="dbus-run-session --"
fi

[ -n "$WLR_BACKENDS" ] || WLR_BACKENDS=drm,libinput
export WLR_BACKENDS
exec $SYSTEMD_CAT $DBUS_RUN_SESSION "${COMPOSITOR}" -v -S -C "${PHOC_INI}" -E "${PHOSH_SESSION_BIN} --session=phosh"
