#!/bin/bash
port=8813
source /home/svc-zsaf/virtual_envs/streamlit/bin/activate

run_interactive_generator() {
    fuser -k $port/tcp

    DATA=($(bazel run --config=cuda -c opt //zsaf/server/interactive_generator:interactive_generator_setup | tr -d "[',]"))

    # Set the environment variables otherwise set by Bazel
    export LD_LIBRARY_PATH=${DATA[0]}
    export PYTHONPATH=${DATA[1]}
    export RUNFILES_MANIFEST_FILE=${DATA[2]}

    # Move the the directory Bazel uses for the execution of the binary (important for protos)
    current_directory=${DATA[3]}
    cd $current_directory

    streamlit run /home/svc-zsaf/zoox/driving/zsaf/server/interactive_generator/interactive_generator.py --server.port $port
}

until run_interactive_generator; do
    echo "Service crashed with exit code $?. Restarting in 5 seconds..." >&2
    sleep 5
done
