#! /bin/bash
. acsstartupConstants
. acsstartupAcsInstance
. acsstartupLogging.sh
#*************************************************************************
# ALMA - Atacama Large Millimiter Array
# (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
#----------------------------------------------------------------------
ACS_LOG_COMMAND $@

#----------------------------------------------------------------------
#--Little hack to pickup $ACS_INSTANCE
if [ "`echo $@ | grep \"\-b \"`" != "" ]
then
    ACS_INSTANCE=$@
    ACS_INSTANCE=${ACS_INSTANCE#*-b }
    export ACS_INSTANCE=${ACS_INSTANCE%% *}

elif [ "`echo $@ | grep \"\-baseport \"`" != "" ]
then
    ACS_INSTANCE=$@
    ACS_INSTANCE=${ACS_INSTANCE#*-baseport }
    export ACS_INSTANCE=${ACS_INSTANCE%% *}
fi

#--Check out the directory structure

if [ ! -d `getInstanceDirName $ACS_INSTANCE` ]
then

    if ! createInstanceDirectory $ACS_INSTANCE
    then 
	ACS_LOG_ERROR "acsStartContainer" "For some reason this script could not create `getInstanceDirName $ACS_INSTANCE`!"
	ACS_LOG_ERROR "acsStartContainer" "    Check $ACS_INSTANCES_DIR for free base ports!"
	exit $EC_CANNOTCREATE
    fi

#Stop user A from interfering with user B
elif [ ! -w `getInstanceDirName $ACS_INSTANCE` ]
then
    ACS_LOG_ERROR "acsStartContainer" "The directory '`getInstanceDirName $ACS_INSTANCE`' is not writeable by $USER!"
    ACS_LOG_ERROR "acsStartContainer" "Please specify an ACS instance that is owned by $USER"
    exit $EC_CANNOTUSE
fi

#-----------------------------------------------------------------------
#--Get the correct commandline
if ! COMMANDLINE=`acsstartupContainerPort "$@"`
then
    ACS_LOG_ERROR "acsStartContainer" "There was an error when trying to generate the command to start the container!"
    ACS_LOG_ERROR "acsStartContainer" "Unable to proceed!"
    exit $EC_CANNOTUSE
else
    CONTAINERNAME=`echo $COMMANDLINE | awk '{print $2}'`
fi

#-----------------------------------------------------------------------
#--Save the this script's process ID so the container can be forceably
#--killed (if needed) later.
CONTAINER_PID=$$
PID_STRING="_PID"
CONTAINER_PID_FILE="`echo $CONTAINERNAME | sed s/"\/"/:/g`$PID_STRING"
echo $CONTAINER_PID >> "`getInstanceDirName $ACS_INSTANCE`/$ACS_PID_DIR/$CONTAINER_PID_FILE"

#-----------------------------------------------------------------------
# Now run the container
ACS_LOG_INFO "acsStartContainer" "Running the container with these arguments: '$COMMANDLINE'"
$COMMANDLINE
ACS_LOG_INFO  "acsStartContainer" "Container: '$CONTAINERNAME' exited with code: $?."

#-----------------------------------------------------------------------
# The following section of code was requested by G. Chiozzi and B. Jeram
# to ensure that when a container segfaults or is Ctrl-C'ed, the proper files
# normally removed by acsStopContainer are removed as well. It is absolutely
# critical this piece of code be kept in sync with what acsStopContainer does!

#go to the correct directory
INSTANCE_DIR=`getInstanceDirName $ACS_INSTANCE`

#if the PID file exists for the container
if [ -e $INSTANCE_DIR/$ACS_PID_DIR/$CONTAINER_PID_FILE ]
then
    rm $INSTANCE_DIR/$ACS_PID_DIR/$CONTAINER_PID_FILE
fi
