#!/usr/bin/env zsh

# Runs the given command for each file in integrations/*.sh
# Integration commands defined in ./scripts/integration.base.sh
valid_cmds=(clone_repo setup_repo update_deps add_upstream rebase_repo)

usage() {
  echo $1 >&2
  echo "Usage: ./scripts/integrator.sh {${(j:|:)valid_cmds}}" >&2
}

if [[ -z "$1" ]]; then
  usage "Error: No command provided."
  exit 1
fi
if (( ${valid_cmds[(I)$1]} == 0 )); then
  usage "Error: Bad command provided."
  exit 1
fi

source ./.env

for repo_overrides in integrations/*.sh; do
  base_name=${repo_overrides#integrations/}
  base_name=${base_name%.sh}

  # Skip running any commands for repos not in the ACTIVE_REPOS array in .env
  if (( ${#ACTIVE_REPOS[@]} )); then
    (( ${ACTIVE_REPOS[(I)$base_name]} )) || continue
  fi

  . ./scripts/integration.base.sh && . "$repo_overrides" && $1 "$base_name"
done
