#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

# We cannot use the directory ./vendor as it's used by go mod vendor
rm -rf replaced
pushd / > /dev/null
cache=$(go mod download -json helm.sh/helm/v3@v3.7.2  | jq -r '.Dir')
popd > /dev/null
destp=replaced/helm.sh/helm/v3@v3.7.2
for f in $(find $cache); do
    relative=${f#"$cache"}
    if [ -d $f ]; then
        mkdir -p $destp/$relative
    else
        mkdir -p $(dirname $destp/$relative)
        cp $f $destp/$relative
    fi
done
git apply hack/vendor.patch