#!/bin/bash
#
# JAVA_HOME env variable is required, 
#          maybe uncommenting following line would help
#
# JAVA_HOME=/opt/java/jre1.6.0_20/
#
# AutoStarting SshRessourceManager
#  - add the following line into /etc/rc.local
#  - adapt the PATH !!!
# sudo su nobody -c "/opt/Framework/runSshResMan" -s /bin/sh
#
#
#
# Security Issue:
# The running-user needs full access for the working folder
# Framework/tools/sshResourceManager/
# but not for the jar file ( permission rx - is ok )
# Framework/tools/sshResourceManager/sshresourcemanager2.*.*.jar
# and not for configuration
# Framework/tools/sshResourceManager/SshResourceManager.properties
# , too.
#
# Productive hint
# Please install a database server (like mysql,pgsql,...), and a 
# webserver (like apache) interpreting php5. Follow the manuals in 
# tools directory and userguide.

OLDPWD=`pwd`
trap "cd $OLDPWD" 0 1 2 3 4 5 6 7 8 10 12 13 14 15 20

abspath=$(cd ${0%/*} && echo $PWD/${0##*/})
# to get the path only - not the script name
FS_INSTALL_DIR=`dirname "$abspath"`

cd ${FS_INSTALL_DIR}/tools/sshResourceManager/

if [ -z $JAVA_HOME ] ; then
  echo "\n\nPlease set JAVAHOME environment variable, or in this script\n\n"
  exit 1
fi

lastResMan=`ls -1 sshresourcemanager2.*.*.jar | tail -1`

if [ "x$lastResMan" != "x" ] ; then
  nohup $JAVA_HOME/bin/java -jar $lastResMan >> log.txt &
  exit 0;
else
  echo "\n\nResource manager not found.\n\n"
  exit 2
fi
