##############################################################################
# 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_add_pool_env
#
# DESCRIPTION : This function creates links of the grid environment files to
#		a standard location in /etc/profile.d/
#
# AUTHORS :     Nuno.Orestes.Vaz.da.Silva@cern.ch
#		Maria.Alandes.Pradillo@cern.ch
#		Gergely.Debreczeni@cern.ch
#
# NOTES :       ---
#
# YAIM MODULE:  glite-yaim-core
#
##############################################################################

function config_add_pool_env_check () {

  requires $1 GRID_ENV_LOCATION
}
 
function config_add_pool_env_setenv () {
   
  yaimlog DEBUG "Currently this function doesn't set any environment variables."

}

function config_add_pool_env () {

  ### Auxiliary script parameters
  PROFILE_GRID_SH=/etc/profile.d/grid-env.sh
  PROFILE_GRID_CSH=/etc/profile.d/grid-env.csh

  ### Remove the old links (due to possible change of location)
  rm -f ${PROFILE_GRID_SH} ${PROFILE_GRID_CSH}

  ### ... and create the new ones
  yaimlog DEBUG "Creating links for the grid enviroment in /etc/profile.d/"
  ln -s ${GRID_ENV_LOCATION}/grid-env.sh ${PROFILE_GRID_SH}
  ln -s ${GRID_ENV_LOCATION}/grid-env.csh ${PROFILE_GRID_CSH}
  if [ "x${OS_TYPE}" = "xdebian" ]; then
    yaimlog DEBUG "Add in /etc/profile all the files under /etc/profile.d/"
    grep "Added by YAIM" /etc/profile > /dev/null
    if [ $? -ne 0 ]; then
      echo "### Added by YAIM in `date`"            >> /etc/profile
      echo "for i in /etc/profile.d/*.sh ; do"      >> /etc/profile 
      echo "if [ -e \$i ]; then"                     >> /etc/profile 
      echo "# echo \"sourcing profile.d file: \$i\"" >> /etc/profile
      echo ". \$i"                                   >> /etc/profile
      echo "fi"                                     >> /etc/profile 
      echo "done"                                   >> /etc/profile
    fi
  fi 
  ### Exit with success
  return 0
}
