#! /bin/sh
# Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
#
# Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
#
# 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.
#
# 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
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#

EXEC_DIR=`echo $0 | sed -e "s,[^/]*$,,;s,/$,,;s,^$,.,"`

removeTree() {

   local list
   local noeud=$1

   case $noeud in
   *.dir/ | *.dir)
        j=`echo $noeud | sed -e 's,^/,,' -e 's,/$,,' -`
        list=`nameclt list $j 2> /dev/null`
        for i in "$list"
        do 
            [ -n "$i" ] && removeTree "$j/$i"
        done
	nameclt remove_context $j 2> /dev/null
        ;;
   *)
	nameclt unbind $noeud 2> /dev/null
	;;
   esac

}

CONTAINER_NAME="$1"
if test -z ${CONTAINER_NAME=}
then
	echo "Usage : $0 <container name>"
	exit -1	
fi


PYTHON_VERSION=python2.2

# you must define KERNEL_ROOT_DIR and MED_ROOT_DIR (if you need it)

if test -n ${KERNEL_ROOT_DIR}
then
  export PATH=${KERNEL_ROOT_DIR}/bin/salome:${PATH}
  export LD_LIBRARY_PATH=${KERNEL_ROOT_DIR}/lib/salome:${LD_LIBRARY_PATH}
  export PYTHONPATH=${KERNEL_ROOT_DIR}/lib/salome:${KERNEL_ROOT_DIR}/lib/${PYTHON_VERSION}/site-packages/salome:${PYTHONPATH}
fi
if test -n ${MED_ROOT_DIR}
then
  export PATH=${MED_ROOT_DIR}/bin/salome:${PATH}
  export LD_LIBRARY_PATH=${MED_ROOT_DIR}/lib/salome:${LD_LIBRARY_PATH}
  export PYTHONPATH=${MED_ROOT_DIR}/lib/salome:${MED_ROOT_DIR}/lib/${PYTHON_VERSION}/site-packages/salome:${PYTHONPATH}
fi

if test -d ${HOME}/.salome/bin
then
  export PATH=${HOME}/.salome/bin:${PATH}
fi
if test -d ${HOME}/.salome/lib
then
  export LD_LIBRARY_PATH=${HOME}/.salome/lib:${LD_LIBRARY_PATH}
fi

export tmp=/usr/tmp

NODE=Containers.dir/${HOSTNAME}.dir/${CONTAINER_NAME}
removeTree ${NODE}.object
removeTree ${NODE}.dir/


PROCESS=`ps --width 200 -f -u ${USER} | \
	 grep -w ${CONTAINER_NAME} | \
	 grep SALOME_Container |
	 awk '{print \$2}'`


if test -n "${PROCESS}"
then
	kill -9 `echo ${PROCESS}` >& /dev/null
	echo
	echo "     Stop container ${CONTAINER_NAME}"
	echo
	exit 12
fi

exit 11
