#!/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_ce
#
# DESCRIPTION : This function configures main services of a Cream CE.
#
# AUTHORS :     grid-release@infn.it
#
# YAIM MODULE:  glite.yaim.cream-ce
#
##############################################################################

function config_cream_ce_check () {

  requires $1 CREAM_DB_PASSWORD CREAM_DB_USER BLAH_JOBID_PREFIX \
           CREAM_DB_HOST USERS_CONF VOS TOMCAT_USER USE_CEMON CREAM_SANDBOX_PATH \
           USE_ARGUS TOMCAT_HOSTKEY_LOCATION TOMCAT_HOSTCERT_LOCATION \
	   CE_SMPSIZE CREAM_CONCURRENCY_LEVEL CREAM_DB_NAME DELEGATION_DB_NAME \
	   CREAM_DATASOURCE_FACTORY CREAM_JAVA_OPTS_HEAP
  ret_cod1=$?

  if [ "x${USE_CEMON}" == "xtrue" ]; then
    requires $1 CEMON_HOST
    ret_cod2=$?
  fi
  if [ "x${USE_ARGUS}" == "xyes" ]; then
    requires $1 ARGUS_PEPD_ENDPOINTS CREAM_PEPC_RESOURCEID
    ret_cod3=$?
  fi

  if  [ ! $ret_cod1 == 0 ] || [ ! $ret_cod2 == 0 ]  || [ ! $ret_cod3 == 0 ]; then
    return 1
  else
    return 0
  fi

}

function config_cream_ce_setenv () {

  yaimlog DEBUG "This function doesn't set any environment variables"
}


function config_cream_ce () {

GLITE_CREAM_LOCATION_ETC=/etc

  if [ -f /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf ]; then
    cp /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf.old
    grep_java_opts=`grep -c "JAVA_OPTS.*-server" /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf`
    if [ ${grep_java_opts} = 0 ]; then
      cat << EOF  >> /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf
JAVA_OPTS="\${JAVA_OPTS} -server ${CREAM_JAVA_OPTS_HEAP}"
EOF
    else
      sed -i "s|.*JAVA_OPTS=.*-server.*|JAVA_OPTS=\"\${JAVA_OPTS} -server ${CREAM_JAVA_OPTS_HEAP}\"|"  /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf
    fi   
  else
    yaimlog ERROR "File /etc/${TOMCAT_VERSION}/${TOMCAT_VERSION}.conf des not exists"
    return 1
  fi



  yaimlog DEBUG "Create sandbox for supported vo/groups"
  mkdir -p ${CREAM_SANDBOX_PATH}
  chown ${TOMCAT_USER}:${TOMCAT_USER} ${CREAM_SANDBOX_PATH}
  chmod 775 ${CREAM_SANDBOX_PATH}

  if [ ! -e $USERS_CONF ]; then
    yaimlog ERROR "$USERS_CONF not found."
    return 1
  fi
 
  supported_groups=`mktemp /tmp/yaim.XXXXXX`
  for vo in $VOS; do
    awk -F : '{if($5=="'$vo'") print $4}' $USERS_CONF | cut -d ',' -f1 | sort | uniq >> ${supported_groups}
  done

  for a_group in `cat ${supported_groups}`; do

    mkdir -p ${CREAM_SANDBOX_PATH}/${a_group}
    chown ${TOMCAT_USER}.${a_group} ${CREAM_SANDBOX_PATH}/${a_group}
    chmod 770 ${CREAM_SANDBOX_PATH}/${a_group}
  done



  #================================================================
  # ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml - BEGIN
  #================================================================

  yaimlog DEBUG "Configuration of ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml ..."

  backup_file "${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml"

   if [ ${USE_ARGUS} = yes ]; then
      CREAM_XML_TEMPLATE=${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config-argus.xml.template
   else
      CREAM_XML_TEMPLATE=${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config-gjaf.xml.template
   fi

   if [ ! -f ${CREAM_XML_TEMPLATE} ]; then
    yaimlog ERROR "Cream config template ${CREAM_XML_TEMPLATE} file does not exists. Please, verify your installation"
    exit 1
  fi 


   CREAM_XML_TMP=${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config-tmp.xml 


  if [ ${USE_CEMON} = true ]; then   
     job_sensor=$CEMON_HOST
  else
     job_sensor=NA
  fi

   cat ${CREAM_XML_TEMPLATE} | sed \
      -e "s/url=\"jdbc:mysql:\/\/localhost:3306\/delegationcreamdb/url=\"jdbc:mysql:\/\/${CREAM_DB_HOST}:3306\/${DELEGATION_DB_NAME}/" \
      -e "s/url=\"jdbc:mysql:\/\/localhost:3306\/creamdb/url=\"jdbc:mysql:\/\/${CREAM_DB_HOST}:3306\/${CREAM_DB_NAME}/" \
      -e "s/username=\"SetThisToAUser\" password=\"SetThisToAGoodPassword\"/username=\"${CREAM_DB_USER}\" password=\"${CREAM_DB_PASSWORD}\"/" \
      -e "s/factory=\"org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory\"/factory=\"${CREAM_DATASOURCE_FACTORY}\"/" \
      -e "s/<parameter name=\"BLAH_JOBID_PREFIX\" value=.*/<parameter name=\"BLAH_JOBID_PREFIX\" value=\"$BLAH_JOBID_PREFIX\" \/>/" \
      -e "s/<parameter name=\"CREAM_JOB_SENSOR_HOST\" value=.*/<!--parameter name=\"CREAM_JOB_SENSOR_HOST\" value=\"${job_sensor}\" \/-->/" \
    -e "s/<parameter name=\"HOST_SMP_SIZE\" value=.*/<parameter name=\"HOST_SMP_SIZE\" value=\"$CE_SMPSIZE\" \/>/" \
    -e "s|<parameter name=\"SANDBOX_TRANSFER_METHOD\" value=.*|<parameter name=\"SANDBOX_TRANSFER_METHOD\" value=\"$SANDBOX_TRANSFER_METHOD_BETWEEN_CE_WN\" \/>|" \
    -e "s|<parameter name=\"CREAM_SANDBOX_DIR\" value=.*|<parameter name=\"CREAM_SANDBOX_DIR\" value=\"$CREAM_SANDBOX_PATH\" \/>|" \
    -e "s|commandworkerpoolsize=\"50\"|commandworkerpoolsize=\"$CREAM_CONCURRENCY_LEVEL\"|" \
   > ${CREAM_XML_TMP}

  if [ ${USE_ARGUS} = yes ]; then
    # Other substitutions are needed here

    # There can be multiple Argus PEPD endpoints
    endpoints=""
    for j in ${ARGUS_PEPD_ENDPOINTS}; do
      endpoints="$endpoints    <endpoint url=\"${j}\" />\n"
    done

    cat ${CREAM_XML_TMP} | sed \
      -e "s|CREAM_PEPC_RESOURCEID|${CREAM_PEPC_RESOURCEID}|" \
      -e "s|TOMCAT_HOSTCERT_LOCATION|${TOMCAT_HOSTCERT_LOCATION}|" \
      -e "s|TOMCAT_HOSTKEY_LOCATION|${TOMCAT_HOSTKEY_LOCATION}|" \
      -e "s|    <endpoint url=\"ARGUS_PEPD_ENDPOINTS\" />|${endpoints}|" \
    > ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml
    rm ${CREAM_XML_TMP}
  else
    mv ${CREAM_XML_TMP} ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml
  fi 


  chown ${TOMCAT_USER}:${TOMCAT_USER} ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml
  chmod 0600 ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml


  #================================================================
  # ${GLITE_CREAM_LOCATION_ETC}/glite-ce-cream/cream-config.xml - END
  #================================================================



  yaimlog DEBUG "Restarting tomcat"
  /sbin/service ${TOMCAT_VERSION} restart

  return 0

}
