##############################################################################
# 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_storm_gridhttps_generic
#
# DESCRIPTION : This function configures users and certificates on a StoRM Gridhttps.
#
# AUTHORS :     grid-release@infn.it
#
# NOTES :
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_gridhttps_generic_check () {

  requires $1 STORM_GRIDHTTPS_USER STORM_USER STORM_GRIDHTTPS_CERT_DIR X509_HOST_CERT X509_HOST_KEY STORM_GRIDHTTPS_WEBAPP_DEPLOY_FOLDER
  return $?
}

function config_storm_gridhttps_generic_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_gridhttps_generic () {

  FUNCTION="config_storm_gridhttps_generic"

  # Add STORM_GRIDHTTPS_USER if missing
  if [ "${STORM_GRIDHTTPS_USER}" = "root" ]; then
    yaimlog ERROR "${FUNCTION}: \$STORM_GRIDHTTPS_USER can't be root!"
    yestr ${YEX_CONFIG}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_CONFIG}
  else
    id ${STORM_GRIDHTTPS_USER} >& /dev/null \
      || (yaimlog DEBUG "${FUNCTION}: Adding system user ${STORM_GRIDHTTPS_USER}"; adduser -r ${STORM_GRIDHTTPS_USER})
    yaimlog DEBUG "${FUNCTION}: Adding '$STORM_GRIDHTTPS_USER' to ${STORM_USER} group"
    usermod -a -G ${STORM_USER} ${STORM_GRIDHTTPS_USER}
  fi

  # Create ${STORM_GRIDHTTPS_CERT_DIR} directory and copy host certificate and key
  yaimlog DEBUG "${FUNCTION}: Updating host certificate and key in ${STORM_GRIDHTTPS_CERT_DIR}"
  mkdir -p ${STORM_GRIDHTTPS_CERT_DIR}
  cp -f ${X509_HOST_CERT} ${STORM_GRIDHTTPS_CERT_DIR}
  cp -f ${X509_HOST_KEY} ${STORM_GRIDHTTPS_CERT_DIR}
  chown -R ${STORM_GRIDHTTPS_USER}:${STORM_USER} ${STORM_GRIDHTTPS_CERT_DIR}
  
  yaimlog DEBUG "${FUNCTION}: Setting ${STORM_GRIDHTTPS_WEBAPP_DEPLOY_FOLDER} folder ownership"
  chown ${STORM_GRIDHTTPS_USER}:${STORM_USER} ${STORM_GRIDHTTPS_WEBAPP_DEPLOY_FOLDER} 
  
  LOGGING_FOLDER="/var/log/storm"
  yaimlog DEBUG "${FUNCTION}: Ensuring log folder ${LOGGING_FOLDER} ownership to STORM_USER ${STORM_USER}"
  chown ${STORM_USER}:${STORM_USER} ${LOGGING_FOLDER} 

  config_dir="/etc/storm/gridhttps-server"
  # Change ownership to /etc/storm/gridhttps-server configuration directory
  yaimlog DEBUG "${FUNCTION}: Setting ${config_dir} folder ownership"
  if [ -d ${config_dir} ]; then
    chown -R ${STORM_GRIDHTTPS_USER}:${STORM_USER} ${config_dir}
  else
    yaimlog ERROR "${FUNCTION}: StoRM GridHTTPs  configuration directory ${config_dir} doesn't exist"
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_NOSUCHFILE}
  fi

  #Adding JAVA_HOME variable to service environment
  if [ ! -z ${JAVA_HOME} ] ; then
    yaimlog DEBUG "${FUNCTION}: JAVA_HOME variable available"
  elif [ ! -z ${JAVA_LOCATION} ] ; then
    yaimlog DEBUG "${FUNCTION}: found java in JAVA_LOCATION"
  	JAVA_HOME=${JAVA_LOCATION}
  elif [ -L /usr/java/latest ] ; then
    yaimlog DEBUG "${FUNCTION}: found java in filesystem"
  	JAVA_HOME="/usr/java/latest"
  fi
  if [ ! -z ${JAVA_HOME} ] ; then
  	yaimlog DEBUG "${FUNCTION}: Setting up JAVA environment"
  	yaimgridenv_set JAVA_HOME ${JAVA_HOME}
  fi
  
  # Exit with success
  return ${YEX_OK}    

}
