#!/bin/bash
# Copyright 2020 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Script to upload github archives for bazel dependencies to GCS, creating a reliable mirror link.
# Archives are copied to "grpc-bazel-mirror" GCS bucket (https://console.cloud.google.com/storage/browser/grpc-bazel-mirror?project=grpc-testing)
# and will by downloadable with the https://storage.googleapis.com/grpc-bazel-mirror/ prefix.
#
# This script should be run each time bazel dependencies are updated.

set -e

cd $(dirname $0)/..

# Create a temp directory to hold the versioned tarball,
# and clean it up when the script exits.
tmpdir="$(mktemp -d)"
function cleanup {
  rm -rf "$tmpdir"
}
trap cleanup EXIT

function upload() {
  local uri="$1"
  local dst_path="${uri}"

  if [[ "$dst_path" == https://sourceforge.net/*/download ]]; then
    dst_path="${dst_path%/download}"
  fi

  if gcloud storage objects list --stat --fetch-encrypted-object-hashes "gs://grpc-bazel-mirror/${uri}" > /dev/null; then
    echo "Skipping ${uri}"
    return 0
  fi

  echo "Downloading https://${uri}"
  curl -L --fail --output "${tmpdir}/archive" "https://${uri}"
  if [[ ! -s "${tmpdir}/archive" ]]; then
    echo "Failed to download https://${uri}: zero bytes returned"
    return 0
  fi

  echo "Uploading https://${uri} to https://storage.googleapis.com/grpc-bazel-mirror/${dst_path}"
  gcloud storage cp "${tmpdir}/archive" "gs://grpc-bazel-mirror/${dst_path}"

  rm -rf "${tmpdir}/archive"
}

function upload_bzlmod_deps {
  local bazel_modules=($(bazel mod graph 2>/dev/null |
    tail -n +2 | # ignore the <root> module
    sed -E 's/^[^[:alnum:]]*([a-zA-Z0-9@\._-]+).*$/\1/' | sort | uniq))

  local urls=()
  if [ "${#bazel_modules[@]}" -gt 0 ]; then
    urls=($(bazel mod show_repo 2>/dev/null "${bazel_modules[@]}" | grep -Eo 'https://[^"]+' | sort | uniq))
  fi

  for url in "${urls[@]}" ; do
    url="${url#https://}" # strip https:// prefix
    case "$url" in
        *grpc-bazel-mirror*)
          echo "Skipping URL from mirror site: ${url}"
          ;;
        *github.com*)
          echo "Uploading archive from github.com: ${url}"
          upload "${url}"
          ;;
        *)
          echo "Uploading archive from non-github site: ${url}"
          upload "${url}"
          ;;
    esac
  done
}

# How to check that all mirror URLs work:
# 1. clean $HOME/.cache/bazel
# 2. bazel clean --expunge
# 3. bazel sync (failed downloads will print warnings)

# A specific link can be upload manually by running e.g.
# upload "github.com/google/boringssl/archive/1c2769383f027befac5b75b6cedd25daf3bf4dcf.tar.gz"

# bazel binaries used by the tools/bazel wrapper script
upload github.com/bazelbuild/bazel/releases/download/8.7.0/bazel-8.7.0-linux-arm64
upload github.com/bazelbuild/bazel/releases/download/8.7.0/bazel-8.7.0-linux-x86_64
upload github.com/bazelbuild/bazel/releases/download/8.7.0/bazel-8.7.0-darwin-x86_64
upload github.com/bazelbuild/bazel/releases/download/8.7.0/bazel-8.7.0-windows-x86_64.exe

# Collect the github archives to mirror from grpc_deps.bzl
# TODO(weizheyuan): Considier removing this block completely (since it's used by WORKSPACE only)
# and use bzlmod as the only source of truth.
echo "Updating Bazel dependency mirrors (WORKSPACE)"
grep -o '"https://github.com/[^"]*"' bazel/grpc_deps.bzl | sed 's/^"https:\/\///' | sed 's/"$//' | while read -r line ; do
    echo "Updating mirror for ${line}"
    upload "${line}"
done

echo "Updating Bazel dependency mirrors (Bzlmod)"
upload_bzlmod_deps

# Collect the github archives to mirror from build_autogenerated.yaml
echo "Updating CMake dependency mirrors"
grep -Eo '\s*- https://github.com/.+\.(tar\.gz|zip)' build_autogenerated.yaml | sed 's@^[[:space:]]*- https://@@' | while read -r line ; do
    echo "Updating mirror for ${line}"
    upload "${line}"
done
