#!/bin/bash
#SBATCH -J CARLA-0.9.15-case
#SBATCH -p ampere
#SBATCH --ntasks-per-node=16
#SBATCH --gres=gpu:1
#SBATCH -t 1:00:00

# if this number fails for you, this port might already be in use
# just pick another number
# default is 2000
export CARLA_RPC_PORT=3000

module load gcc/10.2.0 python/3.7.12-4tsv54n libjpeg/8d-elbcowk libpng/1.6.37 CARLA

echo "$(date --iso=s) Starting Carla"

# below, unless -carla-streaming-port is set, it defaults to CARLA_RPC_PORT+1
# (3001 in this example)
# the symbol '&' means it will be executed in the background
CARLA -opengl -RenderOffScreen -nosound -carla-rpc-port=$CARLA_RPC_PORT &

# check if CARLA is up every 5 seconds
while [ -z "$(netstat -tunpl 2>/dev/null | grep Carla | grep $CARLA_RPC_PORT)" ]
do
    sleep 5
    echo "$(date --iso=s) Not found yet"
done

echo "$(date --iso=s) Found!"

# set the weather
config.py -p $CARLA_RPC_PORT --weather ClearNoon

# print help for config.py command
config.py --help

# execute this small python script, which gets CARLA's world
# and prints the object's string form if successful
python3 <<python_file
import carla
client = carla.Client('localhost', $CARLA_RPC_PORT)
client.set_timeout(10.0) # seconds
world = client.get_world()
print(str(world))
python_file