#!/usr/bin/env bash

BUILD_DIR=$PWD
if [ -z "$WORKSPACE_DIR" ]; then
    echo "Workspace is not defined. Add WORKSPACE_DIR to environment"
    exit 1
fi

BUILD_DIR=${BUILD_DIR//$WORKSPACE_DIR/\/workspace}

OPT_ARGS="$@"
OPT_ARGS=${OPT_ARGS//$WORKSPACE_DIR/\/workspace}

if [[ "$OSTYPE" == "darwin"* ]]; then
    export PATH="/usr/local/bin:/Applications/Docker.app/Contents/Resources/bin:~/.docker/bin:$PATH"
fi

docker run -i --rm -h `hostname` $($(dirname "${BASH_SOURCE[0]}")/get-user-id-for-docker) -v "${WORKSPACE_DIR}":/workspace -w "${BUILD_DIR}" @docker_image_tag@ ${OPT_ARGS}
