#!/usr/bin/env bash

add_to_path() {
    # Add the path in $1 to the path variable in $2 if it's not there already.
    # E.g. add_to_path path/to/file PATH
    # Based on this StackExchange answer: https://superuser.com/a/39995

    new_path_str="$1"
    path_name="$2"
    path_str="${!2}"
    # ${!2} (bash) / ${(P)2} (zsh) evaluates to the value stored in the variable name stored in $2.

    # Condition: $new_path_str is not in $path_str.
    # Note how colons are handled in a smart way.
    if [[ ":$path_str:" != *":$new_path_str:"* ]]; then

    	# ${value:+new_value} evaluates to $new_value if $value is already set to a non-empty value.
    	# This returns $path_str:$new_path_str, or just $new_path_str with no colons if $path_str is empty.   
    	export "$path_name"="${path_str:+"$path_str:"}$new_path_str"
    fi
}

# Add the TurboCtl directory to $PYTHONPATH.
TurboCtl_path=$(dirname "$0")
add_to_path "$TurboCtl_path" PYTHONPATH

# Run turboctl with given command line arguments.
/usr/bin/env python3 "$(dirname "$0")/turboctl" $@
