#!/bin/bash

if [ "$DOCKER_VERSION" != "" ] && [ "$DOCKER_VERSION" != "default" ]; then
    ln -fs "/usr/local/bin/docker-$DOCKER_VERSION" "/usr/local/bin/docker"
fi

# If a pidfile is still around (for example after a container restart),
# delete it so that docker can start.
rm -rf /var/run/docker.pid
docker -d --storage-driver="overlay" &>/var/log/docker.log &
docker_pid=$!

>&2 echo "Waiting for Docker to start..."
while ! docker ps &>/dev/null; do
    if ! kill -0 "$docker_pid" &>/dev/null; then
        >&2 echo "Docker failed to start"
        cat /var/log/docker.log
        exit 1
    fi

    sleep 1
done

>&2 echo ">" "$@"
exec "$@"
