#!/usr/bin/env bash

set -e
set -x

# Ensure we are on the latest commit
# of the branch where we are converting
# recipes from. Currently this is `main`.
export CF_CURRENT_BRANCH="${GITHUB_REF/refs\/heads\//}"
git checkout "${CF_CURRENT_BRANCH}"

# 2 core available on Travis CI Linux workers: https://docs.travis-ci.com/user/ci-environment/#Virtualization-environments
# CPU_COUNT is passed through conda build: https://github.com/conda/conda-build/pull/1149
export CPU_COUNT=2

export PYTHONUNBUFFERED=1

# Install Miniforge3.
echo ""
echo "Installing a fresh version of Miniforge3."
curl -L "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" > ~/Miniforge3.sh
chmod +x ~/Miniforge3.sh
bash ~/Miniforge3.sh -b -p ~/Miniforge3
touch ~/Miniforge3/conda-meta/pinned
(
    source ~/Miniforge3/bin/activate

    # Configure conda.
    echo ""
    echo "Configuring conda."
    conda config --set show_channel_urls true
    conda config --set auto_update_conda false
    conda config --set add_pip_as_python_dependency false
    conda config --set solver libmamba

    conda update -n base --yes --quiet conda mamba conda-libmamba-solver
)
source ~/Miniforge3/bin/activate

conda install --yes --quiet \
  conda-forge-ci-setup=4.* \
  "conda-smithy>=3.38.0,<4.0.0a0" \
  conda-forge-pinning \
  "conda-build>=24.3" \
  "gitpython>=3.0.8,<3.1.20" \
  requests \
  ruamel.yaml \
  "pygithub>=2.1.1" \
  "rattler-build-conda-compat>=1.2.0,<2.0.0a0" \
  "conda-forge-feedstock-ops>=0.5.0" \
  "conda-forge-metadata>=0.8.1"

conda info
mamba info
conda config --get
git config --global init.defaultBranch main

python .github/workflows/scripts/create_feedstocks.py
