#!/bin/sh
#
#  xfce4
#
#  Copyright (C) 1996-2003 Olivier Fourdan (fourdan@xfce.org)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# FIXME: temporary workaround
export WLR_RENDERER_ALLOW_SOFTWARE=1

if test "x$*" != "x"
then
  OPTS=""
  for OPT in $*
  do
    if test "x$OPT" = "x--help"
    then
      # print help and exit
      echo "Usage:"
      echo "  startxfce4 [OPTIONS...]"
      echo
      echo "Options:"
      echo "  --help                   Show help options"
      echo "  --with-ck-launch         Start xfce4-session inside a"
      echo "                           ConsoleKit session"
      echo "  --wayland                Start xfce4-session inside a Wayland compositor."
      echo "                           The remaining optional arguments are interpreted as"
      echo "                           a command line to run a Wayland compositor replacing"
      echo "                           the default one. 'xfce4-session' will be passed to it"
      echo "                           as an argument."
      echo

      exit 0
    elif test "x$OPT" = "x--with-ck-launch"
    then
      # try to launch xfce4-session with ck-launch-session in xinitrc
      XFCE4_SESSION_WITH_CK="1"
      export XFCE4_SESSION_WITH_CK
    elif test "x$OPT" = "x--wayland"
    then
      XFCE4_SESSION_COMPOSITOR="1"
      export XFCE4_SESSION_COMPOSITOR
      # Set the XDG_RUNTIME_DIR if we can not get it in systems
      # without systemd
      if [ -z "${XDG_RUNTIME_DIR}" ]; then
        if [ -d "/run/user" ]; then
          XDG_RUNTIME_DIR="/run/user/$(id -ru)"
          export XDG_RUNTIME_DIR
        else
          if [ -d "/var/run/user" ]; then
            XDG_RUNTIME_DIR="/var/run/user/$(id -ru)"
            export XDG_RUNTIME_DIR
          else
            XDG_RUNTIME_DIR="/tmp/${USER}-runtime"
            export XDG_RUNTIME_DIR
          fi
        fi
      fi
      # freedesktop specifications mandate that the definition
      # of XDG_SESSION_TYPE should be respected
      XDG_SESSION_TYPE="wayland"
      export XDG_SESSION_TYPE
      # Make sure all toolkits use their Wayland backend
      GDK_BACKEND="wayland"
      export GDK_BACKEND
      QT_QPA_PLATFORM="wayland"
      export QT_QPA_PLATFORM
      # In some OS the "wayland" backend for clutter does not work, so
      # use the safe "gdk" backend instead
      CLUTTER_BACKEND="gdk"
      export CLUTTER_BACKEND
      SDL_VIDEODRIVER="wayland"
      export SDL_VIDEODRIVER
      MOZ_ENABLE_WAYLAND="1"
      export MOZ_ENABLE_WAYLAND
    else
      # append
      OPTS="$OPTS $OPT"
    fi
  done

  if test "x$XFCE4_SESSION_COMPOSITOR" = "x"
  then
    if test "x${OPTS#*--}" = "x${OPTS}"
    then
      CLIENTRC=${OPTS}
    else
      SERVERRC=${OPTS#*-- }
      CLIENTRC=${OPTS%--*}
    fi
  else
    default_compositor="wayfire"
    if test "x$OPTS" = "x" && ! command -v "$default_compositor" >/dev/null 2>&1
    then
      echo "$0: Please either install $default_compositor or specify another compositor as argument"
      exit 1
    fi
    # Create specific wayfire directory for Xfce
    if [ ! -d "${XDG_CONFIG_HOME:-${HOME}/.config}/xfce4/wayfire" ]; then
       mkdir -p "${HOME}/.config/xfce4/wayfire"
    fi
    # Copy specific wayfire configuration file for Xfce
    if [ ! -f "${HOME}/.config/xfce4/wayfire/wayfire.ini" ]; then
       cp -p /usr/share/xfce4/wayfire/wayfire.ini "${HOME}/.config/xfce4/wayfire/wayfire.ini"
    fi
    # Set wayfire keymap and model to match system-wide keymap and
    # model only in absence of a user-created 'lock' file, just in
    # case user doesn't want any modification of the config file.
    if [ ! -e "${HOME}/.config/xfce4/wayfire/lock" ]; then
       WF_KEYMAP="$(awk -F '=' -v q1=\' -v q2=\" '$1 == "KEYMAP" {gsub ("[" q1 q2 "]", "", $2); print $2}' /etc/vconsole.conf)
       export WF_KEYMAP
       WF_KEYMODEL="$(awk -F '=' -v q1=\' -v q2=\" '$1 == "MODEL" {gsub ("[" q1 q2 "]", "", $2); print $2}' /etc/vconsole.conf)
       export WF_KEYMODEL
       sed -i \
         -e "s|xkb_layout = .*|xkb_layout = ${WF_KEYMAP}|g" \
         -e "s|xkb_model = .*|xkb_model = ${WF_KEYMODEL}|g" \
         "${HOME}/.config/xfce4/wayfire/wayfire.ini"
    fi
    # Use dbus-run-session to make sure session starts as D-Bus session
    # Also: Xfce **SHOULD** use its own config directory and config file
    # to avoid conflict with current wayfire setup and avoid launching 
    # anything from wayfire [autostart] section
    XFCE4_SESSION_COMPOSITOR="dbus-run-session -- ${OPTS:-wayfire --config ${HOME}/.config/xfce4/wayfire/wayfire.ini}"
  fi
fi

if test "x$XDG_CONFIG_HOME" = "x"
then
  BASEDIR="$HOME/.config/xfce4/"
else
  BASEDIR="$XDG_CONFIG_HOME/xfce4"
fi

if test "x$XDG_DATA_DIRS" = "x"
then
  if test "x/usr/share" = "x/usr/local/share" -o "x/usr/share" = "x/usr/share"; then
    XDG_DATA_DIRS="/usr/local/share:/usr/share"
  else
    XDG_DATA_DIRS="/usr/share:/usr/local/share:/usr/share"
  fi
# If $XDG_DATA_DIRS is pre-defined, but does not end with /usr/share, append it
elif test "${XDG_DATA_DIRS##*:}" != "/usr/share"
then
  XDG_DATA_DIRS="$XDG_DATA_DIRS:/usr/share"
fi
export XDG_DATA_DIRS

if test "x$XDG_CONFIG_DIRS" = "x"
then
  if test "x/etc" = "x/etc"; then
    XDG_CONFIG_DIRS="/etc/xdg"
  else
    XDG_CONFIG_DIRS="/etc/xdg:/etc/xdg"
  fi
# If $XDG_CONFIG_DIRS is pre-defined, but does not end with /etc/xdg, append it
elif test "${XDG_CONFIG_DIRS##*:}" != "/etc/xdg"
then
  XDG_CONFIG_DIRS="$XDG_CONFIG_DIRS:/etc/xdg"
fi
export XDG_CONFIG_DIRS

if test "x$XFCE4_SESSION_COMPOSITOR" = "x"
then
  if test "x$DISPLAY" = "x"
  then
    echo "$0: Starting X server"
    prog=xinit

    if test ! "x$XDG_VTNR" = "x"; then
      SERVERRC="$SERVERRC vt$XDG_VTNR"
    fi
  else
    echo "$0: X server already running on display $DISPLAY"
    prog=/bin/sh
  fi
else
  prog=/bin/sh
fi

if [ -f "$HOME/.xserverrc" ]; then
  SERVERRC="$HOME/.xserverrc $SERVERRC"
elif [ -f /etc/X11/xinit/xserverrc ]; then
  SERVERRC="/etc/X11/xinit/xserverrc $SERVERRC"
fi

if test ! "x$SERVERRC" = "x"
then
  SERVERRC="-- $SERVERRC"
fi

tty_num=$(echo $(tty | grep -o '[0-9]'))

if [ -f $BASEDIR/xinitrc ]; then
  exec $prog $BASEDIR/xinitrc $CLIENTRC $SERVERRC -- vt${tty_num}
elif [ -f $HOME/.xfce4/xinitrc ]; then
  mkdir -p $BASEDIR
  cp $HOME/.xfce4/xinitrc $BASEDIR/
  exec $prog $BASEDIR/xinitrc $CLIENTRC $SERVERRC -- vt${tty_num}
else
  exec $prog /etc/xdg/xfce4/xinitrc $CLIENTRC $SERVERRC -- vt${tty_num}
fi
