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

function config_storm_generic_check () {

  yaimlog INFO "No required variables to set."

}

function config_storm_generic_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_generic () {

  FUNCTION="config_storm_generic"

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

  # Create /etc/grid-security/$STORM_USER directory and copy host certificate and key
  yaimlog INFO "${FUNCTION}: Updating host certificate and key in ${STORM_CERT_DIR}"
  mkdir -p ${STORM_CERT_DIR}
  cp -f /etc/grid-security/hostcert.pem ${STORM_CERT_DIR}
  cp -f /etc/grid-security/hostkey.pem ${STORM_CERT_DIR}
  chown -R ${STORM_USER}:${STORM_USER} ${STORM_CERT_DIR}

  # Add STORM_USER to 'edguser' group,
  # for writing hardlink into the /etc/grid-security/gridmapdir
  yaimlog INFO "${FUNCTION}: Adding user '${STORM_USER}' to the group 'edguser'"
  if ( getent group "edguser" >/dev/null ); then
    usermod -a -G edguser $STORM_USER
  else
    yaimlog ERROR "${FUNCTION}: Group 'edguser' doesn't exist"
    yestr ${YEX_CONFIG}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_CONFIG}
  fi

  # Add edguser to primary-group of STORM_USER,
  # for rights on storm-yaim-variables.conf used by the dynamic info-provider
  yaimlog INFO "${FUNCTION}: Adding user edguser to '${STORM_USER}' group"
  storm_group=`groups ${STORM_USER} |cut -d " " -f 3`
  yaimlog DEBUG "storm_group = $storm_group"
  if ( getent group "${storm_group}" >/dev/null ); then
    usermod -a -G $storm_group edguser
  else
    yaimlog ERROR "${FUNCTION}: Group 'storm_group' doesn't exist"
    yestr ${YEX_CONFIG}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_CONFIG}
  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 INFO "${FUNCTION}: Setting up JAVA environment"
  	yaimgridenv_set JAVA_HOME ${JAVA_HOME}
  fi
  
  # Exit with success
  return ${YEX_OK}    

}
