#!/bin/sh
# -------
# File:        session-cmd-script
# Description: session-cmd session script
# Author:      Luis Garcia Gisbert <garcia_luigis@gva.es>
#
# 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.,
# 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
# --------

######### changed by mariodebian ########
[ -e /etc/default/tcos-standalone ] && . /etc/default/tcos-standalone

if [ -f /usr/share/pyshared/tcosmonitor/shared.py ]; then
  DISPLAY_HOST=$(python /usr/share/pyshared/tcosmonitor/shared.py "$DISPLAY" 2>/dev/null)
else
  DISPLAY_HOST=$(python -c 'import os; import tcosmonitor.shared; print tcosmonitor.shared.parseIPAddress(os.environ["DISPLAY"])')
fi

if [ -z ${DISPLAY_HOST} ] && [ "${TCOS_STANDALONE_START}" != "1" ]; then
  exit 0
fi
################# end ####################

LIB_FILE="/usr/lib/tcos/session-cmd-common.sh"
[ -r "$LIB_FILE" ] || exit 1
. ${LIB_FILE}
EXEC_SCRIPT="/usr/lib/tcos/session-cmd-exec"

# kill before launching again
killall -w faucet >/dev/null 2>&1

sleep 2

UNIX_SOCKET="$(create_socket)" || exit 0
rm -f "${UNIX_SOCKET}.sock" "${UNIX_SOCKET}.pid"
cd /tmp
umask 0177
faucet "${UNIX_SOCKET}.sock" --unix --pidfile "${UNIX_SOCKET}.pid" --in xargs ${EXEC_SCRIPT} >/dev/null 2>&1
exit 0
