#!/bin/bash

which unzip > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR:  unzip is missing"
exit 111
fi

unzip -o $0 > /dev/null 2>&1
# PYNAME=`echo $0 | sed -e "s/\(-.*$\)/.py/"`

current_dir=$PWD
export PATH=${current_dir}:${current_dir}/bin:$PATH

chmod +x ${current_dir}/bin/*
ln -fs ${current_dir}/bin/* ${current_dir}/

if ! command -v python &> /dev/null
then
    echo "no python, link python3 to python"
    # alias python=python3
    ln -fs $(which python3) ./python
fi

export PYTHONPATH=${current_dir}/lib_py:$PYTHONPATH
export IDDS_CONFIG=${current_dir}/etc/idds/idds.cfg.client.template

if [[ ! -z "${PANDA_AUTH_DIR}" ]] && [[ ! -z "${PANDA_AUTH_ORIGIN}" ]]; then
    export PANDA_AUTH_ID_TOKEN=$(cat $PANDA_AUTH_DIR);
    export PANDA_AUTH_VO=$PANDA_AUTH_ORIGIN;
    export IDDS_OIDC_TOKEN=$(cat $PANDA_AUTH_DIR);
    export IDDS_VO=$PANDA_AUTH_ORIGIN;
    export PANDA_AUTH=oidc;
elif [[ -f $X509_USER_PROXY ]]; then
    unset PANDA_AUTH;
    export IDDS_AUTH_TYPE=x509_proxy;
	cp $X509_USER_PROXY ${current_dir}/x509_proxy
fi;

export IDDS_LOG_LEVEL=debug

export PANDA_CONFIG_ROOT=$(pwd);
export PANDA_VERIFY_HOST=off;
export PANDA_BEHIND_REAL_LB=true;

# export PANDA_CLIENT_VERBOSE=true
echo "Environment variables:"
env

# Set PANDA_URL_SSL and PANDACACHE_URL if not already set
if [ -z "${PANDA_URL_SSL}" ]; then
    export PANDA_URL_SSL=$PANDA_SERVER_URL
    if [[ "$PANDA_URL_SSL" =~ :[0-9]+/?$ ]]; then
        export PANDA_URL_SSL="${PANDA_URL_SSL%/}/server/panda"
    fi
fi
if [ -z "${PANDACACHE_URL}" ]; then
    export PANDACACHE_URL=$PANDA_SERVER_URL
    if [[ $PANDACACHE_URL =~ :[0-9]+/?$ ]]; then
        export PANDACACHE_URL="${PANDACACHE_URL%/}/server/panda"
    fi 
fi
echo "panda server url: $PANDA_URL_SSL"
echo "panda cache url: $PANDACACHE_URL"

echo "which python:"
which python
echo "python --version:"
python --version

# myargs="$@"
echo "parse workflow args"
new_command=$(idds_parse_workflow_args "$@")
echo "new command: "
echo $new_command
# echo "cat new command"
# cat $new_command

if [[ -L "python" ]]; then unlink python; fi

echo "running new command, outputs:"
# eval $cmd
eval $new_command
ret=$?

echo pwd
pwd; ls

echo unlink idds_parse_workflow_args
unlink idds_parse_workflow_args
echo unlink run_workflow
unlink run_workflow

echo rm -fr ${current_dir}/lib_py ${current_dir}/etc ${current_dir}/bin ${current_dir}/tmp_bin ${current_dir}/run_workflow_wrapper ${current_dir}/__pycache__  ${current_dir}/run_workflow.sh ${current_dir}/x509_proxy
rm -fr ${current_dir}/lib_py ${current_dir}/etc ${current_dir}/bin ${current_dir}/tmp_bin ${current_dir}/run_workflow_wrapper ${current_dir}/__pycache__ ${current_dir}/run_workflow.sh ${current_dir}/x509_proxy

echo "return code: " $ret
exit $ret
