#! /bin/sh
#*******************************************************************************
# ALMA - Atacama Large Millimiter Array
# Copyright (c) European Southern Observatory, 2013 
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#

. acsstartupAcsPorts
. acsstartupConstants
. acsstartupAcsInstance

if [ X"$MANAGER_REFERENCE" = X ]; then
   ACS_MANAGER_PORT=`getManagerPort`
   export HOST=`getIP`
   export MANAGER_REFERENCE=corbaloc::$HOST:$ACS_MANAGER_PORT/Manager
fi
ACS_LOG_DEBUG "alarmSystemProfiler" "ACS Manager: $MANAGER_REFERENCE"

if [ X"$ACS_NAME_SERVICE" = X ]; then
   ACS_NAMING_SERVICE_PORT=`getNamingServicePort`
   export HOST=`getIP`
   export ACS_NAME_SERVICE=corbaloc::$HOST:$ACS_NAMING_SERVICE_PORT/NameService
fi
ACS_LOG_DEBUG "alarmSystemProfiler" "ACS naming service: $ACS_NAME_SERVICE"

echo $MANAGER_REFERENCE
echo $ACS_NAME_SERVICE

dir=$(searchFile bin/alarmSystemProfiler)
echo $dir

# Find a directory to store the workspace
candidateDirs="${HOME} ${ACSDATA}/tmp /tmp"
for i in $candidateDirs;
do
	if [ -w $i ]
	then
		workspace_dir=${i}/.alarmsprofiler-workspace-$(whoami)
		break;
	fi
done

if [ -d ${workspace_dir} ]; then
    rm -rf  ${workspace_dir} 
fi

$dir/lib/AlarmSystemProfiler/alarmSystemProfiler \
	-data ${workspace_dir} \
	-configuration ${workspace_dir} \
	-vm ${JAVA_HOME}/bin \
	-vmargs \
	-DACS.data=${ACSDATA} \
	-DACS.tmp=$ACS_TMP \
	-DACS.manager=${MANAGER_REFERENCE} \
	-DORBInitRef.NameService=${ACS_NAME_SERVICE}

#
# ___oOo___