#!/bin/sh

# This script is the entrypoint for our Docker image.

set -e

path=$(cd $(dirname "$0") && pwd)

[ -z "${MUJOCO_KEY_BUNDLE}" ] || ( mkdir -p ~/.mujoco && curl https://openai-public.s3-us-west-2.amazonaws.com/mujoco/$MUJOCO_KEY_BUNDLE.tar.gz | tar xz -C ~/.mujoco )

# Set up display; otherwise rendering will fail
rm -f /tmp/.X12-lock
"$path/../vendor/Xdummy" :12 &
export DISPLAY=:12

# Wait for the file to come up
display=12
file="/tmp/.X11-unix/X$display"
for i in $(seq 1 10); do
    if [ -e "$file" ]; then
	break
    fi

    echo "Waiting for $file to be created (try $i/10)"
    sleep "$i"
done
if ! [ -e "$file" ]; then
    echo "Timing out: $file was not created"
    exit 1
fi

exec "$@"
