##############################################################################
# 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_glexec_wn_users
#
# DESCRIPTION : This function creates the necessary users for glexec in the WN.
#
# AUTHORS :     Maria.Alandes.Pradillo@cern.ch
#
# NOTES :   
#
# YAIM MODULE:  glite-yaim-clients
#
##############################################################################

config_glexec_wn_users_check () {

  requires $1 CONFIG_USERS EDG_GROUP

}


config_glexec_wn_users_setenv () {
 yaimlog DEBUG "Currently this function doesn't set any environment variables."
 return 0
}

config_glexec_wn_users () {

# Internal variables
# This has to be glexec until a fix is done in glexec to use a different user
GLEXEC_WN_USER=glexec
GLEXEC_WN_GROUP=glexec

# maybe override the home directory if this variable is set
maybe_home=
if [ -n "$GLEXEC_USER_HOME" ]; then
    maybe_home="-d $GLEXEC_USER_HOME"
fi

####@ Creating glexec user and group

if [ "x${CONFIG_USERS}"="xyes" ]; then
  if ! (getent group ${GLEXEC_WN_GROUP} > /dev/null) 
  then
    groupadd ${GLEXEC_WN_GROUP} 2>/dev/null
  fi
  if ! (getent passwd ${GLEXEC_WN_USER} > /dev/null) 
  then
    useradd -s /sbin/nologin $maybe_home -g ${GLEXEC_WN_GROUP} ${GLEXEC_WN_USER}  2>/dev/null
  fi
else
  yaimlog WARNING "CONFIG_USERS is set to 'no'. User ${GLEXEC_WN_USER} and group ${GLEXEC_WN_GROUP} must exist in your system"
  if ! (getent passwd ${GLEXEC_WN_USER} > /dev/null)
  then
    yaimlog ERROR "User ${GLEXEC_WN_USER} doesn't exist and CONFIG_USERS is set to 'no'. Please, create ${GLEXEC_WN_USER} user"
    yestr ${YEX_NOUSER}
    yaimlog ERROR "${YERRORSTR}"
    exit ${YEX_NOUSER}
  else
    if ! (getent group ${GLEXEC_WN_GROUP} > /dev/null)
    then
       yaimlog ERROR "Group ${GLEXEC_WN_GROUP} doesn't exist and CONFIG_USERS is set to 'no'. Please, create ${GLEXEC_WN_GROUP} group"
       yestr ${YEX_NOUSER}
       yaimlog ERROR "${YERRORSTR}"
       exit ${YEX_NOUSER}
    fi
  fi
fi

#
# This is needed by config_vomsmap.
# user and group creation is done in config_edgusers, 
# but we are not calling this function only for one group
#
if [ "x${CONFIG_USERS}"="xyes" ]; then
  if ! (getent group ${EDG_GROUP} > /dev/null)
  then
    groupadd ${EDG_GROUP} 2>/dev/null
  fi
else
  yaimlog WARNING "CONFIG_USERS is set to 'no'. Group ${EDG_GROUP} must exist in your system"
  if ! (getent group ${GLEXEC_WN_GROUP} > /dev/null)
  then
    yaimlog ERROR "Group ${EDG_GROUP} doesn't exist and CONFIG_USERS is set to 'no'. Please, create ${EDG_GROUP} group"
    yestr ${YEX_NOUSER}
    yaimlog ERROR "${YERRORSTR}"
    exit ${YEX_NOUSER}
  fi
fi

return 0

}

