#!/bin/bash
# Copyright 2023 Google LLC
#
# 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 generate the BUILD.bazel file to create tink and tink-android Maven
# artifacts.

set -euo pipefail

BAZEL_CMD="bazel"
# Prefer using Bazelisk if available.
if command -v "bazelisk" &> /dev/null; then
  BAZEL_CMD="bazelisk"
fi
readonly BAZEL_CMD

OUT_FILE="BUILD.bazel"

usage() {
  cat <<EOF
Usage: $0 [-o <destination file>]
 -o: Output file (default=BUILD.bazel).
 -h: Show this help message.
EOF
  exit 1
}

process_params() {
  while getopts "ho:" opt; do
    case "${opt}" in
      o) OUT_FILE="${OPTARG}" ;;
      *) usage ;;
    esac
  done
  shift $((OPTIND - 1))
  readonly OUT_FILE
}

create_build_file() {
  local -r tink_deps="$1"
  local -r tink_android_deps="$2"
  cat <<EOF > "${OUT_FILE}"
# Build file for generating tink and tink-android Maven artifacts.

load("//tools:gen_maven_jar_rules.bzl", "gen_maven_jar_rules")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

exports_files(["BUILD"])

# WARNING: This is autogenerated using tools/create_maven_build_file.sh.
gen_maven_jar_rules(
    name = "tink",
    doctitle = "Tink Cryptography API",
    manifest_lines = [
        "Automatic-Module-Name: com.google.crypto.tink",
    ],
    root_packages = [
        "com.google.crypto.tink"
    ],
    deps = [
$(cat "${tink_deps}" | sed 's/^/        "/' | sed 's/$/",/')
    ],
)

gen_maven_jar_rules(
    name = "tink-android",
    doctitle = "Tink Cryptography API for Android",
    resources = glob([
        "src/main/resources/**",
    ]),
    root_packages = [
        "com.google.crypto.tink",
    ],
    shaded_packages = [
        # The following package(s) will be shaded, according to the rules
        # specified in shading_rules.
        "com.google.protobuf",
    ],
    shading_rules = "jar_jar_rules.txt",
    deps = [
$(cat "${tink_android_deps}" | sed 's/^/        "/' | sed 's/$/",/')
    ],
)
EOF
  buildifier "${OUT_FILE}"
}

readonly TINK_JAVA_PREFIX="//src/main/java/com/google/crypto/tink"
readonly TINK_JAVA_ANDROID_PREFIX="//src_android/main/java/com/google/crypto/tink"
readonly TINK_JAVA_INTEGRATION_PREFIX="${TINK_JAVA_PREFIX}/integration"

main() {
  process_params "$@"

  # Targets in TINK_JAVA_PREFIX of type java_library, excluding:
  #  * testonly targets,
  #  * targets in tink_java_integration_prefix.
  local -r expected_tink_deps="$(mktemp)"
  "${BAZEL_CMD}" query "kind(java_library,${TINK_JAVA_PREFIX}/...) \
except attr(testonly,1,${TINK_JAVA_PREFIX}/...) \
except kind(java_library,${TINK_JAVA_INTEGRATION_PREFIX}/...)" \
    > "${expected_tink_deps}"

  # Targets in TINK_JAVA_PREFIX and TINK_JAVA_ANDROID_PREFIX of type
  # android_library, excluding testonly targets.
  local -r expected_android_deps="$(mktemp)"
  "${BAZEL_CMD}" query "kind(android_library,${TINK_JAVA_PREFIX}/...) \
except attr(testonly,1,${TINK_JAVA_PREFIX}/...)" > "${expected_android_deps}"

  "${BAZEL_CMD}" query "kind(android_library,${TINK_JAVA_ANDROID_PREFIX}/...) \
except attr(testonly,1,${TINK_JAVA_ANDROID_PREFIX}/...)" >> "${expected_android_deps}"

  create_build_file "${expected_tink_deps}" "${expected_android_deps}"
}

main "$@"
