#!/bin/sh
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2010
# 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.
##############################################################################
#
# AUTHORS: Alejandro Álvarez Ayllón, CERN
# 
# Some commons function for all lcgdm tests
#
##############################################################################

# Default for X509_CERT_DIR
if [ -z "$X509_CERT_DIR" ]; then
  export X509_CERT_DIR="/etc/grid-security/certificates"
fi

# Sets back the X509_USER* variables so the proxy is used
function PROXY_NEEDED {
  # The proxy should have been created previously
  if [ -z $X509_USER_CERT ]; then
    export X509_USER_CERT=$PROXY_USER_CERT
    export X509_USER_KEY=$PROXY_USER_KEY
  fi
  # Check that the proxy actually exists
  voms-proxy-info &> /dev/null
  if [ $? -ne 0 ]; then
    TEST_FAILED "There is no proxy, or it is out of date"
  fi
}

# Unset the X509_USER_* environment variables so no proxy is used
function NO_PROXY_NEEDED {
  if [ -z $PROXY_USER_CERT ]; then
    export PROXY_USER_CERT=$X509_USER_CERT
    export PROXY_USER_KEY=$X509_USER_KEY
  fi
  unset X509_USER_CERT
  unset X509_USER_KEY
}

# Exits with a success
function TEST_PASSED {
  if [ -n "$1" ]; then
    echo $1
  fi
  echo "-TEST PASSED-"
  exit 0
}

# Exits with a failure
# @param $1 The message
# @param $2 The exit code (optional)
function TEST_FAILED {
  if [ $# -ge 1 ]; then
    echo $1
  fi
  echo "-TEST FAILED-"
  if [ $# -lt 2 ]; then
    exit -1
  else
    exit $2
  fi
}


# Executes in the DPM host a command
# Uses SSH if DPM is remote
# @param $1 The command
# @param $2 The host (optional, if not set DPM_HOST will be used)
function DPM_HOST_EXEC {
  local host=$DPM_HOST
  if [ "x$2" != "x" ]; then
    host=$2
  fi
  if [ "$host" != `hostname` ] && [ "$host" != "localhost" ]; then
    ssh root@"$host" "$1"
  else
    # NOTE: This is supposed to be executed as root, so unset X509_USER_*
    NO_PROXY_NEEDED
    bash -c "$1"
    retCode=$?
    PROXY_NEEDED
    return $retCode
  fi
}

# Executes in the LFC host a command
# Uses SSH if LFC is remote
# @param $1 The command
# @param $2 The host (optional, if not set LFC_HOST will be used)
function LFC_HOST_EXEC {
  local host=$LFC_HOST
  if [ "x$2" != "x" ]; then
    host=$2
  fi
  if [ "$host" != `hostname` ] && [ "$host" != "localhost" ]; then
    ssh root@"$host" "$1"
  else
    # NOTE: This is supposed to be executed as root, so unset X509_USER_*
    NO_PROXY_NEEDED
    bash -c "$1"
    retCode=$?
    PROXY_NEEDED
    return $retCode
  fi
}

# Checks if the defined environment variables are set
# and aborts if not
# @param $@ The list of environment variables
function ENV_REQUIRED {
  for var in $@
  do
    if [ "x`eval echo '$'$var`" == "x" ]; then
      TEST_FAILED "The variable $var is not set"
    fi
  done
  return 0
}

# Dumps into the standard output the stdout and stderr of a command
# @param $1 The command
# @param $2 The stdout
# @param $3 The stderr
function REPORT_OUTPUT {
   echo "->>> $1 stdout was:"
   cat $2
   echo
   echo "->>> $1 stderr was:"
   cat $3
   echo
}

# Executes a command, wrapping it with some useful code,
# like echoing the command itself, catching errors, printing the line number, etc
# @param $1 The command
# @param $2 The line number (see $LINENO) [optional]
# @param $3 The expected return code      [optional, if not specified: 0]
# @param $4 The file name                 [optional, if not specified: ignored]
function RUN_COMMAND() {

  local command="$1"
  local lineno=$2
  if [ $# -ge 3 ]; then
    local expected=$3
  else
    local expected=0
  fi
  local file=$4

  echo -e "@ Executing $command\n"
  OUTPUT=`eval $command 2> /tmp/run_command.err`
  ret=$?
  ERROR=`cat /tmp/run_command.err`
  echo "$OUTPUT"
  echo "$ERROR"
  rm -f /tmp/run_command.err

  if [ $expected -ne $ret ]; then
    echo -e "\n@ Command '$command' failed!"
    if [ -n "$lineno" ]; then
      echo -n "@ Called from line $lineno"
      if [ -n "$file" ]; then
        echo -n " ($file)"
      fi
      echo -n ": "
    fi
    echo -e "Expected $expected, got $ret \n"
  else
    echo -e "\n@Success\n"
  fi

  return $ret
}

# Generates a PKCS12 with the user cert and key defined by X509_USER_*
# and sets the environment PKCS12_USER_CERT
function SET_PKCS12_CERT() {
  PKCS12_USER_CERT="/tmp/user.pkcs12"
  if [ ! -f "${PKCS12_USER_CERT}" ]; then
    openssl pkcs12 -export -inkey "${X509_USER_KEY}" -in "${X509_USER_CERT}" -passin "pass:${PROXYPASSWD}" -passout "pass:" -out "${PKCS12_USER_CERT}"
    if [ $? -ne 0 ]; then
      TEST_FAILED "Could not generate PKCS12 user certificate"
    fi
  fi
  export PKCS12_USER_CERT
}

