#!/bin/bash

# Copyright (c) 2013-2020, SIB - Swiss Institute of Bioinformatics and
#                          Biozentrum - University of Basel
# 
# 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.

# Wrapper script that dispatches the actions to the actual Python script. It 
# autodetects the most important directories and sets LD_LIBRARY_PATH and 
# friends.

# EXIT CODES
# If an action is started, returns with the code of this action
#  1  -  not enough arguments

if test $# == 0 ; then
  echo "Usage:"
  echo "  promod <action>"
  echo ""
  echo "type 'promod help' to get a list of commonly used actions"
  exit 1
fi

SCRIPT_NAME=$0

BIN_DIR=$( cd `dirname "$SCRIPT_NAME"`; pwd)
export OST_ROOT="/usr"
export PROMOD3_ROOT=$(dirname "$BIN_DIR")
export PROMOD3_SHARED_DATA_PATH="$PROMOD3_ROOT/share/promod3"
PROMOD3_EXEC_PATH="$PROMOD3_ROOT/libexec/promod3"
# export PYTHONPATH="/usr/lib/python3/dist-packages:${PROMOD3_ROOT}/lib64/python3.13/site-packages/:${PYTHONPATH}"
# export PATH="$BIN_DIR:${OST_ROOT}/bin:$PATH"
# export LD_LIBRARY_PATH="${PROMOD3_ROOT}/lib64:${OST_ROOT}/lib64:$LD_LIBRARY_PATH"

# retrieve absolute path to python executable
pyexec=python3

ACTION="$1"
PROMOD3_SCRIPT="${PROMOD3_EXEC_PATH}/pm-${ACTION}"
shift

if test -e "${PROMOD3_SCRIPT}" ; then
    $pyexec "${PROMOD3_SCRIPT}" $@
else
    ACTION_BASENAME=`basename "$ACTION"`
    case "$ACTION_BASENAME" in
        *.py ) $pyexec "$ACTION" $@;;
        * ) echo "Unknown action '${ACTION}'"
            echo "type 'promod help' to get started"
            exit 1 ;;
    esac
fi
