#!/bin/bash
# (C) Datadog, Inc. 2019
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

USAGE="""\
container-apigentools: Run apigentools in container
Usage: container-apigentools IMAGE [--spec-repo-volume SPEC_REPO_VOLUME] [APIGENTOOLS_ARG ...]

When invoked only with IMAGE argument, this will run the whole code
generation workflow inside a container.

When the optional APIGENTOOLS_ARG(s) are provided, they will be passed
to apigentools running inside the container.

By default, current working directory is mounted inside the image as the spec
repo. This can be changed by providing "--spec-repo-volume" option with a full
path to spec repo as argument.
"""

if [[ "$#" -eq 0 ]]; then
    echo "Error: You must provide full name of image to use"
    echo ""
    echo "${USAGE}"
    exit 1
fi

if [[ "$1" == "--help" || "$1" == "-h" ]]; then
    echo "${USAGE}"
    exit 0
fi

# if the user passed in something like apigentools/apigentools:latest, let's try using
# a tag of that image that has the format of "git-1234abc" - this will get saved to .apigentools-info
# and is necessary for reproducibility and trackability - otherwise the user passed in
# e.g. apigentools/apigentools:0.1.0 or apigentools/apigentools:git-1234abc already
IMAGE=$1
if echo "$1" | grep -q ":latest$"; then
    # since inspect wouldn't pull the image (unlike run), we make sure the inspected image is present explicitly
    docker image inspect $1 > /dev/null 2>&1 || docker image pull $1;
    IMAGE_WITH_GIT_TAG=`docker image inspect $1 -f "{{range .RepoTags}}{{println .}}{{end}}" | grep ":git-.......$"`
    if [[ "$?" -eq "0" ]]; then
        IMAGE=$IMAGE_WITH_GIT_TAG
    fi
fi

shift

SPEC_REPO_VOLUME="$(pwd)"
if [ "$1" == "--spec-repo-volume" ]; then
    SPEC_REPO_VOLUME="$2"
    shift 2
fi

# Add APIGENTOOLS_IMAGE env var as well as every env var that starts with "APIGENTOOLS_"
ENV="-e APIGENTOOLS_IMAGE=${IMAGE} `env | grep APIGENTOOLS_ | sed -e "s|^|-e |"`"
VOLUMES="-v ${SPEC_REPO_VOLUME}:/var/lib/apigentools/spec-repo -v /var/run/docker.sock:/var/run/docker.sock"

exec docker run ${ENV} ${VOLUMES} ${IMAGE} "$@"
