#!/usr/bin/env sh

set -eu

: ${CONTINUOUS_INTEGRATION=}
: ${DOCKER_BUILDKIT=}

progressFlag=""
if [ "$CONTINUOUS_INTEGRATION" = "true" ]; then progressFlag="--progress=plain"; fi

case ${1:-} in
'')
  . $(dirname $0)/util
  case $buildmode in
  "buildkit")
    buildctl build $progressFlag --frontend=dockerfile.v0 --local context=. --local dockerfile=. --frontend-opt filename=./hack/dockerfiles/vendor.Dockerfile --frontend-opt target=validate
    ;;
  "docker-buildkit")
    export DOCKER_BUILDKIT=1
    iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX)
    docker build --iidfile $iidfile -f ./hack/dockerfiles/vendor.Dockerfile --target validate --force-rm . || exit 1
    iid=$(cat $iidfile)
    docker rmi $iid
    rm -f $iidfile
    ;;
  *)
		echo "buildkit support is required"
    exit 1
    ;;
  esac
  ;;
check)
  status="$(git status --porcelain -- go.mod go.sum vendor 2>/dev/null)"
  diffs=$(echo "$status" | grep -v '^[RAD] ' || true)
  if [ "$diffs" ]; then
      {
	set +x
        echo 'The result of "make vendor" differs'
        echo
        echo "$diffs"
        echo
        echo 'Please vendor your package with "make vendor"'
        echo
      } >&2
      exit 1
  fi
  echo 'Congratulations! All vendoring changes are done the right way.'
  ;;
esac
