#!/bin/sh

######################### CONFIGURATION ######################################

PERLPATH=
PATH=$PERLPATH:$PATH
export PATH

######################### CONFIGURATION ######################################

#
# Detect TSI
#
TSI_BIN=`dirname $0`

if [ "${TSI_BIN#/}" = "$TSI_BIN" ]
then
  if [ "$TSI_BIN" != "." ]
  then
    TSI_DIR="$(pwd)/$TSI_BIN"
  else
    TSI_DIR="$(pwd)"
  fi
else
  TSI_DIR=$TSI_BIN
fi

TSI_DIR="${TSI_DIR%/bin}"

usage() {
  echo ""
  echo "Usage: $0 [-d] [conf_dir]"
  echo ""
}

debug=0
for arg in "$@" 
do
  case "$arg" in
    -h | -help | --help | -\?)
      usage
      exit 0;;
    -d | -debug | --debug)
      debug=1
      shift;;
    *)
      ;;
  esac
done

if [ $# -ne 0 ] && [ $# -ne 1 ]
then
  usage
  exit 1
fi


# Detect configuration directory:

if [ $# -eq 1 ]
then
	TSI_CONF=$1
elif [ -n "$UNICORE_TSI" ]
then
	TSI_CONF=$UNICORE_TSI
else
	TSI_CONF="${TSI_DIR}/conf"
fi


# Sanity check. TSI_CONF must exist, be a directory and be wx.
# w - because the admin scripts needs to write the authorisation file

if [ ! -d $TSI_CONF ] 
then
	echo "TSI configuration directory $TSI_CONF is not a directory (or cannot be found)" 1>&2
	exit 1
fi

if [ ! -w  $TSI_CONF ] 
then 
	echo "Cannot write to TSI configuration directory $TSI_CONF" 1>&2
	exit 1
fi

if [ ! -x  $TSI_CONF ] 
then 
	echo "Cannot execute TSI configuration directory $TSI_CONF" 1>&2
	exit 1
fi

# check config file $TSI_CONF/tsi.properties (needs to be r)

TSI_PROPS=$TSI_CONF/tsi.properties

if [ ! -r $TSI_PROPS ] 
then
	echo "The TSI configuration file $TSI_PROPS does not exist (or is not readable)." 1>&2
	exit 1
fi

echo ""
echo "Evaluating $TSI_PROPS:"
echo "Found TSI $TSI_DIR"
echo ""


#
# PID file
#
PID_FILE="`perl -ne 'if(/^\s*tsi.pidfile\s*=\s*(\S+)/) {print $1}' $TSI_PROPS`"
if [ -z "$PID_FILE" ]; then
  PID_FILE=$TSI_CONF/LAST_TSI_PIDS
fi


# Is there an instance of the TSI already running?

if [ -f $PID_FILE ]
then

	last_pid=`cat $PID_FILE | xargs -n 10000 | sed 's/ /,/g'`
	if [ $? != 0 ]
	then
		echo "Could not read  pid file $PID_FILE" 1>&2
		exit 1
	fi

	ps -p $last_pid > /dev/null 2>&1

	if [ $? != 0 ]
	then
		# ps failed (no process or PID_FILE corrupt)
		rm -f $PID_FILE
		if [ $? != 0 ]
		then
			echo "Could not remove pid file $PID_FILE" 1>&2
			exit 1
		fi
	else
		# ps -p succeeded on pid in file, thus TSI is running
		echo "There is already a TSI running (with pid(s) $last_pid)" 1>&2
		exit 0
	fi

fi


# check log dir
LOG_DIR="`perl -ne 'if(/^\s*tsi.logdir\s*=\s*(\S+)/) {print $1}' $TSI_PROPS`"

if [ "$LOG_DIR" != "syslog" ]; then
    mkdir -p $LOG_DIR
    if [ ! -r $LOG_DIR ]  || [ ! -w $LOG_DIR ]
    then
        echo "The TSI logging directory $LOG_DIR does not exist (or is not readable and writable)." 1>&2
        echo ""
        exit 1;
    fi

    date=`date +_%Y_%m_%d`
    time=`date +_%H_%M`
    tsilog=$LOG_DIR/TSILog${date}${time}
    echo "Logging to $tsilog"
else
    echo "TSI logs messages via Syslog"
fi

# set default umask
umask 77

# the main tsi file
EXEC=$TSI_DIR/perl/tsi

if [ ! -r $EXEC ] 
then
  echo "The TSI file $EXEC does not exist (or is not readable)." 1>&2
  echo ""
  exit 1
fi

if [ $debug -eq 1 ]; then
    echo "perl -d $EXEC $TSI_PROPS"
    perl -d "$EXEC" "$TSI_PROPS"
else
    if [ "$LOG_DIR" != "syslog" ]; then
        echo "nohup perl $EXEC $TSI_PROPS > $tsilog 2>&1 &"
        nohup perl "$EXEC" "$TSI_PROPS" > $tsilog 2>&1 &
    else
        echo "nohup perl $EXEC $TSI_PROPS 2>/dev/null &"
        nohup perl "$EXEC" "$TSI_PROPS" 2>/dev/null &
    fi
	echo $! >> $PID_FILE
	$TSI_BIN/find_pids $TSI_CONF
fi
