#!/bin/bash
# Copyright 2024 Google Inc. All Rights Reserved.

# 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.

BASE_REQUIREMENTS_PATH=$1
TARGET_REQUIREMENTS_PATH=$2

if ! python3.11 --version > /dev/null 2>&1 ; then
  echo "Please install a python3.11 interpreter. See s.apache.org/beam-python-dev-wiki for Python installation tips."
  exit 1
fi

if ! python3.11 -m venv --help > /dev/null 2>&1 ; then
  echo "Your python3.11 installation does not have a required venv module. See s.apache.org/beam-python-dev-wiki for Python installation tips."
  exit 1
fi

set -ex

ENV_PATH="$PWD/__build__/python_requirements_gen"

rm -rf "$ENV_PATH" 2>/dev/null || true
# These python versions need to be kept in sync with our dockerfile python versions (https://github.com/GoogleCloudPlatform/DataflowTemplates/blob/0ac92513838ca525adb3f616c9e1f65237334d1e/plugins/core-plugin/src/main/java/com/google/cloud/teleport/plugin/DockerfileGenerator.java#L46)
python3.11 -m venv "$ENV_PATH"
. "$ENV_PATH"/bin/activate

# allow one-off executions of pip to generate requirements locally without alarming automation
alias pip_automation="pip"

pip_automation install --upgrade pip==25.0.1 setuptools wheel
pip_automation install pip-tools

# Install requirements from base file
pip_automation install ${PIP_EXTRA_OPTIONS:+"$PIP_EXTRA_OPTIONS"}  --no-cache-dir -r $BASE_REQUIREMENTS_PATH

echo "Checking for broken dependencies:"
pip check
echo "Installed dependencies:"
pip freeze

# Generate hashes for new requirements file
echo "Running pip-compile to generate hashes"
pip-compile $BASE_REQUIREMENTS_PATH -o $TARGET_REQUIREMENTS_PATH --generate-hashes --allow-unsafe

cat <<EOT > "$TARGET_REQUIREMENTS_PATH"
# Copyright 2025 Google Inc. All Rights Reserved.

# 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.

# Autogenerated requirements file for Apache Beam container image.
# From the templates base directory to update,
# run: sh python/generate_all_dependencies.sh
# Do not edit manually, adjust the base requirements file, and regenerate the list.

# See [maintainers-guide](https://github.com/GoogleCloudPlatform/DataflowTemplates/blob/main/contributor-docs/maintainers-guide.md#validating-and-upgrading-beam-versions) for more information.

$(cat $TARGET_REQUIREMENTS_PATH)

EOT
