##############################################################################
# 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_sw_dir
#
# DESCRIPTION : This function configures the SW directories for the VOs.
#
# AUTHORS :     Gergely.Debreczeni@cern.ch
#               Robert.Harakaly@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-clients
#                 
##############################################################################

config_sw_dir_check () {

requires $1 VOS GROUPS_CONF
return $?

}

config_sw_dir_setenv () {

yaimlog DEBUG "This function doesn't currently set any environment variables."

}

config_sw_dir () {

  dir=""

  for VO in `echo $VOS | tr '[:lower:]' '[:upper:]'`; do
    dir=`get_vo_param ${VO} SW_DIR`
    if [ ! "$dir" = "." ]; then
      sgmusers=`users_getspecialusers $VO sgm`
      sgmuser=`echo $sgmusers | cut -d " " -f 1`
      vogroup=`users_getvogroup ${VO}`
      sgmgroup=`users_getspecialgroup ${VO} sgm`
      sgmgroup=`id -g -n $sgmuser`
      if [ "x$vogroup" = "x$sgmgroup" ]; then
        if [ ! -d "$dir" ]; then 
          mkdir -p $dir 
          chown ${sgmuser}:${sgmgroup} $dir 
          chmod 755 $dir 
        else 
          yaimlog WARNING "YAIM has detected single sgm account"
          yaimlog WARNING "Execute the following command manually: chmod -R u+rw,go-w $dir" 
          yaimlog WARNING "to ensure that the VO software area in $dir has the correct permissions."
        fi
      else 
        if [ ! -d "$dir" ]; then 
          mkdir -p $dir 
          chown ${sgmuser}:${sgmgroup} $dir 
          chmod 775 $dir 
        else 
          yaimlog WARNING "YAIM has detected multiple sgm accounts"
          yaimlog WARNING "Execute the following command manually: chmod -R ug+rw,o-w $dir" 
          yaimlog WARNING "to ensure that the VO software area in $dir has the correct permissions."
        fi 
     fi 
    fi 

  done

  return 0

}

