#!/bin/sh
#
# /etc/x2go/Xsession
#
# X2Go Xsession file -- used by x2goserver Xsession.d add-on.

# This file has been derived from the global Xsession file in Debian squeeze

set -e

X2GO_LIBEXEC_PATH="$(x2gopath libexec)";

PROGNAME=XSession-x2go

message () {
  # pretty-print messages of arbitrary length; use xmessage if it
  # is available and $DISPLAY is set
  MESSAGE="$PROGNAME: $*"
  echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2
  if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
    echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
  fi
}

message_nonl () {
  # pretty-print messages of arbitrary length (no trailing newline); use
  # xmessage if it is available and $DISPLAY is set
  MESSAGE="$PROGNAME: $*"
  echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2;
  if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
    echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
  fi
}

errormsg () {
  # exit script with error
  message "$*"
  exit 1
}

internal_errormsg () {
  # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  # One big call to message() for the sake of xmessage; if we had two then
  # the user would have dismissed the error we want reported before seeing the
  # request to report it.
  errormsg "$*" \
           "Please report the installed version of the \"x2goserver\"" \
           "package and the complete text of this error message to" \
           "<x2go-dev@lists.x2go.org>."
}

# Load profile
for file in "/etc/profile" "$HOME/.profile" "/etc/xprofile" "$HOME/.xprofile"; do
    if [ -f "$file" ]; then
        echo "Loading profile from $file";
        . "$file"
    fi
done

# initialize variables for use by all session scripts

OPTIONFILE=/etc/x2go/Xsession.options

SYSRESOURCES=/etc/x2go/Xresources
USRRESOURCES=$HOME/.Xresources-x2go

SYSSESSIONDIR=/etc/x2go/Xsession.d
USERXSESSION=$HOME/.xsession-x2go
USERXSESSIONRC=$HOME/.xsessionrc-x2go
ALTUSERXSESSION=$HOME/.Xsession-x2go
ERRFILE=$HOME/.xsession-x2go-errors

# attempt to create an error file; abort if we cannot
if (umask 077 && touch "$ERRFILE") 2> /dev/null && [ -w "$ERRFILE" ] &&
  [ ! -L "$ERRFILE" ]; then
  chmod 600 "$ERRFILE"
elif ERRFILE=$(tempfile 2> /dev/null); then
  if ! ln -sf "$ERRFILE" "${TMPDIR:=/tmp}/xsession-x2go-$USER"; then
    message "warning: unable to symlink \"$TMPDIR/xsession-x2go-$USER\" to" \
             "\"$ERRFILE\"; look for session log/errors in" \
             "\"$TMPDIR/xsession-x2go-$USER\"."
  fi
else
  errormsg "unable to create X session (X2Go) log/error file; aborting."
fi

exec >>"$ERRFILE" 2>&1

echo "$PROGNAME: X session started for $LOGNAME at $(date)"

# sanity check; is our session script directory present?
if [ ! -d "$SYSSESSIONDIR" ]; then
  errormsg "no \"$SYSSESSIONDIR\" directory found; aborting."
fi

# Attempt to create a file of non-zero length in /tmp; a full filesystem can
# cause mysterious X session failures.  We do not use touch, :, or test -w
# because they won't actually create a file with contents.  We also let standard
# error from tempfile and echo go to the error file to aid the user in
# determining what went wrong.
WRITE_TEST=$(tempfile)
if ! echo "*" >>"$WRITE_TEST"; then
  message "warning: unable to write to ${WRITE_TEST%/*}; X session (X2Go) may" \
          "exit with an error"
fi
rm -f "$WRITE_TEST"

# use run-parts to source every file in the session directory; we source
# instead of executing so that the variables and functions defined above
# are available to the scripts, and so that they can pass variables to each
# other

SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)
SYSSESSIONDIR=/etc/x2go/Xsession.d

SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)

### source Xsession files
if [ -n "$SESSIONFILES" ]; then

    set +e
    for SESSIONFILE in $SESSIONFILES; do
        "$X2GO_LIBEXEC_PATH/x2gosyslog" "$0" "info" "executing $SESSIONFILE"
        . $SESSIONFILE
    done
    set -e
fi

exit 0

# vim:set ai et sts=2 sw=2 tw=80:
