#!/bin/bash

# Usage: check-lsst-job-env.sh

# Init
thisScript=$(basename $0)

# Display the execution environment before running the LSST local prolog.
echo -e "\nBefore running local prolog:"
/usr/bin/printenv

# Ensure the variable LSST_LOCAL_PROLOG is set and source the script it
# refers to.
if [[ -z ${LSST_LOCAL_PROLOG} ]]; then
    echo "ERROR: variable LSST_LOCAL_PROLOG is not set"
    exit 1
fi
source ${LSST_LOCAL_PROLOG}

# Examine the environment set by the local prolog.
echo -e "\nAfter running local prolog:"

# The environment variables below must be set. Their associated value is
# "true" if they refer to an existing file which we must check for existance.
declare -A envVars=(
    # PanDA
    ['GOOGLE_APPLICATION_CREDENTIALS']="true"
    ['AWS_SHARED_CREDENTIALS_FILE']="true"
    ['USE_REALTIME_LOGGING']="false"
    ['REALTIME_LOGGING_SERVER']="false"
    ['REALTIME_LOGNAME']="false"
    ['REALTIME_LOGFILES']="false"
    ['LSST_RUN_TEMP_SPACE']="true"

    # Butler
    ['X509_USER_PROXY']="true"
    ['LSST_HTTP_AUTH_CLIENT_CERT']="true"
    ['LSST_HTTP_AUTH_CLIENT_KEY']="true"
    ['LSST_RESOURCES_TMPDIR']="true"
    ['LSST_HTTP_PUT_SEND_EXPECT_HEADER']="false"
    ['LSST_DB_AUTH']="true"
    ['LSST_HTTP_CACERT_BUNDLE']="true"
    ['DAF_BUTLER_REPOSITORY_INDEX']="true"
)

for var in ${!envVars[@]}; do
    value=${!var}
    if [[ -z ${value} ]]; then
        echo "ERROR: ${var} is not set"
    else
        echo "OK: ${var} is set to \"${value}\" "
        # If this variable points to a path, check that path exists
        isPath=${envVars[${var}]}
        if [[ ${isPath} == "true" ]]; then
            path=${value}
            if [[ ! -e ${path} ]] && [[ ! -d ${path} ]]; then
               echo "ERROR: path ${path} does not exist"
            else
               echo "OK: ${var} points to valid path \"${path}\" "
            fi
        fi
    fi
done

# Check the butler repos pointed to by DAF_BUTLER_REPOSITORY_INDEX exist and
# can actually be used by the butler.
if [[ -n ${DAF_BUTLER_REPOSITORY_INDEX} ]]; then
    releaseDir=$(ls -dr /cvmfs/sw.lsst.eu/linux-x86_64/lsst_distrib/w* | head -1)
    source "${releaseDir}/loadLSST.bash" && setup lsst_distrib

    repos=$(awk '/^[[:alnum:]_\-]+:/ {printf "%s\n", $1}' ${DAF_BUTLER_REPOSITORY_INDEX} | sed 's/://g')
    for repo in ${repos}; do
        echo -e "\nquerying repo \"${repo}\""
        butler query-dimension-records ${repo} instrument
        if [[ $? != 0 ]]; then
            echo "ERROR: could not access butler repo ${repo}"
        fi
    done
fi
echo -e "\n"

# Run epilog and check clean up was made
if [[ -z ${LSST_LOCAL_EPILOG} ]]; then
    echo "ERROR: variable LSST_LOCAL_EPILOG is not set"
    exit 1
fi

source ${LSST_LOCAL_EPILOG}
if [[ -n ${DAF_BUTLER_REPOSITORY_INDEX} ]] && [[ -f ${DAF_BUTLER_REPOSITORY_INDEX} ]]; then
    echo "ERROR: after epilog, the butler repository index ${DAF_BUTLER_REPOSITORY_INDEX} should not exist"
fi

echo -e "\nEnd of execution"