##############################################################################
# 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_edgusers
#
# DESCRIPTION : This function configures some users needed by the middleware.
#
# AUTHORS :     Gergely.Debreczen@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################

config_edgusers_check () {

if [ "x$CONFIG_USERS" = "xyes" ]; then
  requires $1 EDGUSERS
  return $?
fi

return 0

}

config_edgusers(){

if [ "x$CONFIG_USERS" = "xyes" ] && [ ! -f ${EDGUSERS} ]; then
  yaimlog ERROR "${EDGUSERS} file doesn't exist. Can't configure users"
  exit 1
fi

if [ "x$CONFIG_USERS" = "xyes" ]; then

while IFS=: read id user gids groups descr home; do

  yaimlog DEBUG "Creating user `eval echo "\$user"`..."

  id `eval echo "\$user"` > /dev/null 2>&1
  result=$?
  if [ $result -eq 1 ]; then
    yaimlog DEBUG "User `eval echo "\$user"` doesn't exist"
    if [ "x$CONFIG_USERS" = "xyes" ]; then
      yaimlog DEBUG "YAIM is going to add user `eval echo "\$user"`"
      gids=`echo $gids | sed -e 's/,/ /g'`
      groups=`echo $groups | sed -e 's/,/ /g'`
      initialgroup=`echo $groups | cut -d " " -f 1`
      additionalgroups=`echo $groups | cut -d " " -f 2- -s`
      [ -z "$additionalgroups" ] || additionalgroups="-G $additionalgroups"
    
      unset home_dir
      # Home directory
      if [ "x$home" != "x" ]; then
        yaimlog DEBUG "HOME directory $home is specified for the user" 
        home_dir="-d `eval echo "\$home"`"
      fi

      yaimlog DEBUG "Checking whether the group `eval echo "\$groups"` for user `eval echo "\$user"` already exist..."

      for group in $groups; do
        gid=`echo $gids | cut -d " " -f 1`
        gids=`echo $gids | cut -d " " -f 2- -s`
        if ! (getent group `eval echo "\$group"` >/dev/null || groupadd -g $gid `eval echo "\$group"`) then
          yaimlog DEBUG "Executing... groupadd -g $gid `eval echo "\$group"`"
          yaimlog ERROR  "Group `eval echo "\$group"` with gid '$gid' failed to be created."
          yestr ${YEX_GROUPERR}
          yaimlog ERROR "${YERRORSTR}"
          exit ${YEX_GROUPERR}
        else
          yaimlog DEBUG  "Group `eval echo "\$group"` added"
        fi
      done

      if ! (useradd -m -p "*NP*" -c "$descr" -u $id -g `eval echo "\$initialgroup"` \
            `eval echo "\$additionalgroups"` $home_dir `eval echo "\$user"`) then
        yaimlog DEBUG "Executing... useradd -m -p "*NP*" -c $descr -u $id -g `eval echo "\$initialgroup"` \
                       `eval echo "\$additionalgroups"` $home_dir `eval echo "\$user"`"
        yaimlog ERROR "User `eval echo "\$user"` with uid '$id' failed to be created"
        yestr ${YEX_USERERR}
        yaimlog ERROR "${YERRORSTR}"
        exit ${YEX_USERERR}
      fi 
      yaimlog DEBUG "User `eval echo "\$user"` added"

    else
      yaimlog WARNING "User configuration is disabled. YAIM won't configure the users in ${EDGUSERS}"
      yaimlog INFO "User `eval echo "\$user"` doesn't exist in the system and it's necessary to configure the middleware"
      yaimlog ERROR "There are missing users in the system. Please, add them before configuring the middleware"
      exit 1
    fi
  else
    yaimlog DEBUG "Skipping user `eval echo "\$user"` since it already exists..."
  fi

done < ${EDGUSERS}

else

yaimlog INFO "User configuration is disabled. YAIM won't configure the users in ${EDGUSERS}"
yaimlog INFO "Please, be aware the users must exist in the system for succesful configuration"

fi


return 0
}
