#!/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_DPM_user
#
# DESCRIPTION : This function configures the DPM user
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

function config_DPM_user_check () {

 requires $1 DPMMGR_USER DPMMGR_GROUP DPMMGR_UID DPMMGR_GID DPMMGR_USER_HOME
 retcode=$?

 return ${retcode}
}


function config_DPM_user () {

####@ Creates the dpmmg user, set's it home directory (/home/dpmmgr) and it password to *NP*.

if [ "x${CONFIG_USERS}"="xyes" ]; then
  # Since USER_CONF is not used anymore, check if dpmmgr is there and fail
  if [ ! -z $USERS_CONF ]; then
    if (grep -q ${DPMMGR_USER} $USERS_CONF); then
      yaimlog ERROR "The uid of ${DPMMGR_USER} is set in ${USERS_CONF}"
      yaimlog ERROR "This is deprecated! Please, remove it"
      yaimlog ERROR "It must be set in the configuration file using DPMMGR_UID instead (see defaults)"
      exit ${YEX_CONFFILE}
    fi
  fi

  # Create
  if ! (getent passwd | grep ${DPMMGR_USER} > /dev/null) 
  then 
    groupadd -g ${DPMMGR_GID} ${DPMMGR_GROUP} 2>/dev/null
    useradd -p "*NP*"  -c "DPM Manager" -m -d ${DPMMGR_USER_HOME} \
            -s /bin/bash -g ${DPMMGR_GROUP} -u ${DPMMGR_UID} ${DPMMGR_USER} 2>/dev/null 
  fi 
else 
  yaimlog WARNING "CONFIG_USERS is set to 'no'. User ${DPMMGR_USER} must exist in your system" 
  if ! (getent passwd | grep ${DPMMGR_USER} > /dev/null) 
  then 
    yaimlog ERROR "User ${DPMMGR_USER} doesn't exist and CONFIG_USERS is set to 'no'. Please, create ${DPMMGR_USER} user" 
    yestr ${YEX_NOUSER} 
    yaimlog ERROR "${YERRORSTR}" 
    exit ${YEX_NOUSER} 
  fi 
fi 
return 0

}

