DIR=`dirname $0`
PROJECT_HOME=${DIR}/../

function cleanup {
  BUILD_IMAGE=$(docker images tinkerpop | awk '{if ($2 == "build") print $3}')
  [ ! -z ${BUILD_IMAGE} ] && docker rmi ${BUILD_IMAGE}
  rm -f ${PROJECT_HOME}/Dockerfile
}
trap cleanup EXIT

case "$1" in
  -h | --help ) ${DIR}/scripts/build.sh -h; exit 0 ;;
esac

pushd ${PROJECT_HOME} > /dev/null

HADOOP_VERSION=$(cat pom.xml | grep -o '<hadoop.version>[^<]*' | head -n1 | cut -d '>' -f2)

docker/build-containers.sh -h "${HADOOP_VERSION}"

sed -e "s/HADOOP_VERSION\$/${HADOOP_VERSION}/" docker/build/Dockerfile.template > Dockerfile
cat >> Dockerfile <<EOF
CMD ["sh", "-c", "docker/scripts/build.sh $@"]
EOF

docker build -t tinkerpop:build .
docker run --rm -ti tinkerpop:build

popd > /dev/null
