#!/bin/bash

echo fly-dm prepare, setup shell, check home and previous session `date '+%F %T.%3N'`

# This file is sourced by /etc/X11/fly-dm/Xsession, not executed.

if [ -z "${XAUTHORITY}" ]; then
   export XAUTHORITY="$HOME/.Xauthority"
fi
#to inform X server about ... dont need anymore due to API call to label session used in fly-dm
#/usr/bin/xlsclients > /dev/null 2>&1

session="$1"

/usr/bin/systemctl is-enabled astra-interpreters-lock.service > /dev/null 2>&1
if [ $? -eq "0" ]; then
    INTERPRETERS_LOCKED="1"
    export FLY_INTERPRETERS_LOCKED="1"
    echo "astra interpreters are locked!"
fi

if [ -z "$INTERPRETERS_LOCKED" ]; then

# Note that the respective logout scripts are not sourced.
case $SHELL in
  */bash)
    [ -z "$BASH" ] && exec $SHELL $0 "$@"
    set +o posix
    [ -f /etc/profile ] && . /etc/profile
    if [ -f "$HOME/.bash_profile" ]; then
      . "$HOME/.bash_profile"
    elif [ -f "$HOME/.bash_login" ]; then
      . "$HOME/.bash_login"
    elif [ -f "$HOME/.profile" ]; then
      . "$HOME/.profile"
    fi
    ;;
  */zsh)
    [ -z "$ZSH_NAME" ] && exec $SHELL $0 "$@"
    emulate -R zsh
    [ -d /etc/zsh ] && zdir=/etc/zsh || zdir=/etc
    zhome="${ZDOTDIR:-$HOME}"
    setopt shwordsplit
    # zshenv is always sourced automatically.
    [ -f "$zdir/zprofile" ] && . "$zdir/zprofile"
    [ -f "$zhome/.zprofile" ] && . "$zhome/.zprofile"
    [ -f "$zdir/zlogin" ] && . "$zdir/zlogin"
    [ -f "$zhome/.zlogin" ] && . "$zhome/.zlogin"
    ;;
  */csh|*/tcsh)
    # [t]cshrc is always sourced automatically.
    # Note that sourcing csh.login after .cshrc is non-standard.
    xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX`
    $SHELL -c "if (-f /etc/csh.login) source /etc/csh.login; if (-f ~/.login) source ~/.login; /bin/sh -c 'export -p' >! $xsess_tmp"
    . $xsess_tmp
    rm -f $xsess_tmp
    ;;
  */rbash)
    PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
    export PATH
    #nothing more for kiosk
    ;;
  *) # Plain sh, ksh, and anything we do not know.
    [ -f /etc/profile ] && . /etc/profile
    [ -f "$HOME/.profile" ] && . "$HOME/.profile"
    ;;
esac

fi

if ! test -d "$HOME"; then
      if [ "$LANG" = "ru_RU.UTF-8" ]; then
        HOME_CAPTION="Внимание"
        HOME_MESSAGE="Нет домашнего каталога ${HOME}."
      else
        HOME_CAPTION="Attention"
        HOME_MESSAGE="No home directory ${HOME}."
      fi
      fly-dialog --caption "$HOME_CAPTION" --error "$HOME_MESSAGE"
fi

#if dbus-user-session package is here then only one session allowed (DEVOS-1682)
if [ -f /usr/lib/systemd/user/dbus.service ]; then
if [ -f /usr/bin/fly-check-switch-sess.sh ]; then
  fly-check-switch-sess.sh
  if [ $? -ne "0" ]; then
   exit
  fi
fi
fi

#DEVOS-1793 - done in X server
#fly-wmfunc FLYWM_IS_NESTED_SESSION
#if [ "$?" = 0 ]; then
#xrandr --output default --preferred
#fi
