##############################################################################
# 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_certs_userland
#
# DESCRIPTION : This function installs the CA files in X509_CERT_DIR.
#
# AUTHORS :     Andreas.Unterkircher@cern.ch
#               Robert.Harakaly@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-clients
#                 
##############################################################################

function config_certs_userland_check () {
 if [ ! -z "$X509_CERT_DIR" ] && [ ! -d "$X509_CERT_DIR" ]
 then
  yaimlog ERROR "X509_CERT_DIR points to the non-existing directory $X509_CERT_DIR"
  exit 1
 fi
 if (! central_certs ) && [ -z "$CA_REPOSITORY" ] ; then
  yaimlog ERROR "CA_REPOSITORY not defined."
  exit 1
 fi
 return 0 
}

function config_certs_userland_setenv () {
# The definition of X509_CERT_DIR is being done in the ui_tar resp. wn_tar function. 
 return 0
}


function config_certs_userland () {

if central_certs; then
    yaimlog INFO "Certificates found in /etc/grid-security/certificates or in \$X509_CERT_DIR. Not installing relocated version."
    return 0
fi

CA_WGET=`echo $CA_REPOSITORY | awk '{ i=1
 while (($i !~ /^http/) && (i <= NF)) {i++}
 result = $i
 if (i == 1) {
   print result
   exit
 }
 start = ++i
 while (i < NF) {
  if (result ~ /\/$/)
   result = result $i
  else
   result = result "/" $i
  i++
 }
 if (result ~ /\/$/)
  result = result "RPMS." $NF
 else
  result = result "/RPMS." $NF
 print result
}'`

# not needed for EMI
#INSTALL_ROOT=${INSTALL_ROOT:-/opt}

if ! ( which rpm2cpio > /dev/null 2>&1 ); then
	yaimlog ERROR "rpm2cpio is required to install the CA rpms"
	exit 1
fi

mkdir tmp.$$ || return 1

cd tmp.$$


wget -l1 -nd -r --quiet $CA_WGET

if ! (ls *.rpm > /dev/null 2>&1); then
	yaimlog ERROR "Couldn't download the CA rpms"
	exit 1
fi

for i in *.rpm; do
	rpm2cpio $i | cpio -im --make-directories --quiet
done

if [ ! -d ${X509_CERT_DIR:-${GLITE_EXTERNAL_ROOT:-}/etc/grid-security/certificates} ]; then
	mkdir -p ${X509_CERT_DIR:-${GLITE_EXTERNAL_ROOT:-}/etc/grid-security/certificates}
fi

mv -f etc/grid-security/certificates/* ${X509_CERT_DIR:-${GLITE_EXTERNAL_ROOT:-}/etc/grid-security/certificates}

cd - > /dev/null

rm -rf tmp.$$

return 0

}
