#!/usr/bin/env bash

. $(dirname $0)/util
set -eu -o pipefail -x

: ${CONTINUOUS_INTEGRATION=}

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

case $buildmode in
"buildkit")
   output=$(mktemp -d -t buildctl-output.XXXXXXXXXX)
  buildctl build $progressFlag --frontend=dockerfile.v0 --local context=. --local dockerfile=. \
    --frontend-opt target=update \
    --frontend-opt filename=./hack/dockerfiles/vendor.Dockerfile \
    --output type=local,dest=$output
  rm -rf ./vendor
  cp -R "$output/out/" .
  rm -rf $output
  ;;
*)
  iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX)
  case $buildmode in
  "docker-buildkit")
    export DOCKER_BUILDKIT=1
    docker build --iidfile $iidfile -f ./hack/dockerfiles/vendor.Dockerfile --target update --force-rm .
    ;;
  *)
		echo "buildctl or docker with buildkit support is required"
	  exit 1
    ;;
  esac
  iid=$(cat $iidfile)
  cid=$(docker create $iid noop)
  rm -rf ./vendor

  docker cp $cid:/out/go.mod .
  docker cp $cid:/out/go.sum .
  docker cp $cid:/out/vendor .

  docker rm $cid
  rm -f $iidfile
  ;;
esac
