#!/bin/bash
# Copyright (c) 2014-2025 Sentieon Inc. All rights reserved

# Find original directory of script, resovling symlinks. Modified from:
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in/246128#246128
SOURCE="$0"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P -- "$( dirname -- "$SOURCE" )" && pwd )"
    SOURCE="$(readlink -- "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
bin_dir="$( cd -P -- "$( dirname -- "$SOURCE" )" && pwd )"
install_dir=$(dirname -- "$bin_dir")

export SENTIEON_INSTALL_DIR="$install_dir"

# load common function definitions
if [ -f "$install_dir/share/funcs" ]; then
    . "$install_dir/share/funcs"
fi

libexec_dir=$install_dir/libexec

cmd=$(basename "$0")
if [ "$cmd" != "sentieon" ]; then
    set -- "$cmd" "$@"
fi

if [ "$#" -eq 0 ]; then
    echo "Thank you for using Sentieon software. Supported commands are:" 
    echo "  "$(ls $libexec_dir 2>/dev/null)
    exit 0
fi

run=exec; fun=$1; shift
if [ "$fun" = "ldd" ]; then
    run=$fun; fun=$1; shift
fi
cmd="$libexec_dir/$fun"
if [ ! -f "$cmd" ]; then
    echo "Command $fun not found"
    exit 1
fi

setup_rtld_path
setup_data_path
setup_python_path
fix_rlimits

# start local license server if needed
if [ "$#" -gt 1 -a -e "$SENTIEON_LICENSE" ]; then
    start_local_license_server
fi

$run $cmd "$@"
