#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# NAME :        config_cream_stop
#
# DESCRIPTION : This function stops the CREAM CE service.
#
# AUTHORS :     grid-release@infn.it
#
# YAIM MODULE:  glite.yaim.cream-ce
#
##############################################################################

function config_cream_stop_check () {

  requires $1  

}

function config_cream_stop_setenv () {

  yaimlog DEBUG "No enviroment variables to set."

}

function config_cream_stop () {


  GLITE_CREAM_LOCATION_ETC=/etc

  ps ax | grep -v grep |grep blah > /dev/null 2>&1
  blah_running=$?
  /sbin/service ${TOMCAT_VERSION} status > /dev/null 2>&1
  tomcat_running=$?
  if [ $blah_running = 0 ] || [ $tomcat_running = 0 ]; then
    yaimlog INFO "Stopping tomcat"
    /sbin/service ${TOMCAT_VERSION} stop
  else 
    if [ ! $blah_running = 0 ]; then
      yaimlog INFO "blah not running"
    fi
    if  [ ! $tomcat_running = 0 ]; then
    yaimlog INFO "tomcat not running"
    fi  
  fi  

  ps ax | grep -v grep |grep BUpdater > /dev/null 2>&1  
  bupdater_running=$?
  ps ax | grep -v grep |grep BNotifier > /dev/null 2>&1
  bnotifier_running=$?
  if [ ${bupdater_running} = 0 ] || [ ${bnotifier_running} = 0 ]; then
    yaimlog INFO "Stopping blah"
    ${GLITE_CREAM_LOCATION_ETC}/init.d/glite-ce-blah-parser stop
  else 
    yaimlog INFO "blah not running"
  fi

  ps ax | grep -v grep |grep blparser > /dev/null 2>&1
  if [ $? = 0 ]; then
    yaimlog INFO "Stopping blparser"
    ${GLITE_CREAM_LOCATION_ETC}/init.d/glite-ce-blah-parser stop
  fi

  ps ax | grep -v grep |grep lb > /dev/null 2>&1
  if [ $? = 0 ] ; then
    yaimlog INFO "Stopping lb processes"
    killall -9 glite-lb-interlogd
    killall -9 glite-lb-locallogger
    ${GLITE_CREAM_LOCATION_ETC}/init.d/glite-lb-locallogger stop
  else
    yaimlog INFO "lb processes not running"
  fi

  return 0

}

