#!/usr/bin/env bash
# Copyright (c) 2004-2026 Yury V. Vishnevskiy yu.v.vishnevskiy@gmail.com
# All Rights Reserved.

sargs=("$@")

currentDir=$(pwd)
thisDirName=$(dirname "$0")
cd "$thisDirName"
umvDir=$(pwd)
cd "$currentDir"

unamestr=$(uname)
if [ "$unamestr" = "Linux" ]; then
    PLATFORM="linux"
elif [ "$unamestr" = "Darwin" ]; then
    PLATFORM="macos"
elif [ "$unamestr" = "FreeBSD" ]; then
    PLATFORM="freebsd"
elif [[ "$unamestr" == *"MINGW64"* ]]; then
    PLATFORM="mingw64"
elif [[ "$unamestr" == *"MINGW32"* ]]; then
    PLATFORM="mingw32"
else
    PLATFORM="unknown"
fi


if [ -z "$UMV_ROOT_DIR" ]; then
    export UMV_ROOT_DIR=$umvDir
fi


if [ $PLATFORM = "linux" ] || [ $PLATFORM = "freebsd" ]; then
    export LD_LIBRARY_PATH="$UMV_ROOT_DIR/lib:$LD_LIBRARY_PATH"
elif [ $PLATFORM = "macos" ]; then
    export DYLD_FALLBACK_LIBRARY_PATH="$UMV_ROOT_DIR/lib:$DYLD_FALLBACK_LIBRARY_PATH"
fi

if [ -z "$VL_LOGFILE_PATH" ]; then
    export VL_LOGFILE_PATH=${UMV_ROOT_DIR}/umv.log
fi

if [ -z "$VL_VERBOSITY_LEVEL" ]; then
    export VL_VERBOSITY_LEVEL=NORMAL
fi

declare -a uargs
for ((i=0; i < ${#sargs[@]}; i++))
do
    item=${sargs[i]}
    case "${item}" in
        "--wrapper")
            ((i++))
            wrapper=${sargs[i]}
            ;;
        *)
            uargs[${#uargs[@]}]="${item}"
            ;;
    esac
done

$wrapper $UMV_ROOT_DIR/bin/umv "${uargs[@]}"
