#! /bin/bash
# (c) Associated Universities Inc., 2002 
# (c) European Southern Observatory, 2002
# Copyright by ESO (in the framework of the ALMA collaboration),
# All rights reserved
#
# 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
#
# "@(#) $Id: cdbTATPrologue,v 1.6 2006/03/13 15:12:32 cparedes Exp $"
#
#
# who       when      what
# --------  --------  ----------------------------------------------
# gchiozzi  11/02/02  created
#


#
# By default this script will use ../test as the CDB.
# But if you pass an argument, this will used as
# absolute path for the CDB root directory.
# (see cdbjDAL command line below and -root command line parameter)
#
if [ "X$1" = "X" ]; then
   CDB_ROOT=../test
else
   CDB_ROOT=$1
fi

# setup the ports
. acsstartupAcsPorts
. acsstartupAcsInstance


#---------------------------------------------------------------------------
#Pick an ACS base port to use

#unset ACS_INSTANCE - we do not allow people to set this from TAT!
unset ACS_INSTANCE

#iterate through the ten possible choices
i=0
while [ $i -lt 10 ]
do
	#if the directory has not already been created.
	if [ ! -e $ACSDATA/tmp/ACS_INSTANCE.$i ]
	then
		#we've found a free one and we're done
		export ACS_INSTANCE=$i
	        break
 	fi

 	i=$(( $i + 1 ))
done

if [ "X$ACS_INSTANCE" = "X" ]
then
    echo "No ACS instances were available!" > /dev/stderr
    exit 42
fi

export ACS_TMP=../test/tmp

echo $ACS_INSTANCE > $ACS_TMP/acs_instance

_CDB_PORT_=`getCDBPort`
echo $_CDB_PORT_ > $ACS_TMP/.cdbport

_WRITE_IOR_=""
if [ "X$WIND_BASE" != X ]; then
  # Make sure the directory exists
  mkdir -p ENVIRONMENTS/lcuTat
  _WRITE_IOR_="-o ENVIRONMENTS/lcuTat/DAL.ior" 
fi

if [ "X$TAT_VERBOSE" = X ]; then
 _REDIR_=/dev/null
else
 _REDIR_=cdb.log
 echo " ****************** cdbTATPrologue *************** " >  $_REDIR_
fi

cdbjDAL -root $CDB_ROOT -OAport $_CDB_PORT_ $_WRITE_IOR_ >>$_REDIR_ 2>&1 &
sleep 10
exit 0
#
# ___oOo___

