#!/bin/bash

set -euo pipefail

unset PYTHONPATH

workspace="$(bazel info workspace)"
source "$workspace/scripts/shell/zooxrc.sh"

#
# Get username from - first logname, then whoami, then envrionment variable
# If all fail, set it to UNKNOWN
# TODO: Make user name mandatory. We need to figure out how to allow headless environments to launch scripts.
#
user_name=""
log_name=`logname 2> /dev/null` || true
if [ -n "$log_name" ]
then
     user_name=$log_name
else
    who_am_i=`whoami 2> /dev/null` || true
    if [ -n "$who_am_i" ]
    then
        user_name=$who_am_i
    else
        env_user_name=${USER:-}
        if [ -n "$env_user_name" ]
        then
            user_name=$env_user_name
        else
            user_name="UNKNOWN"
        fi
    fi
fi

bazel run --config=kraken -c opt //sim/marvel/api:launch_on_marvel2 -- "$@" --user_name="${user_name:-UNKNOWN}"
