#!/bin/bash
# Removes unused Ansible collections from the virtual environment.
# These collections are transitive dependencies of the 'ansible' meta-package
# and are not used by CMA. They are removed to eliminate high/critical CVEs
# originating from their bundled dependency manifests (poetry.lock, Pipfile.lock, etc.).
#
# Modify the UNUSED_COLLECTIONS array as needed based on CVE scan results
# and actual collection usage.

UNUSED_COLLECTIONS=(
  "community/digitalocean"
  "netbox/netbox"
  "infinidat/infinibox"
  "grafana/grafana"
  "azure/azcollection"
  "containers/podman"
)

remove_unused_ansible_collections() {
  local collections_dir="$1"

  if [ -z "$collections_dir" ]; then
    echo "ERROR: ansible_collections directory path is required as first argument"
    return 1
  fi

  if [ ! -d "$collections_dir" ]; then
    echo "WARNING: ansible_collections directory not found at $collections_dir, skipping removal"
    return 0
  fi

  for collection in "${UNUSED_COLLECTIONS[@]}"; do
    local collection_path="$collections_dir/$collection"
    if [ -d "$collection_path" ]; then
      echo "Removing unused collection: $collection"
      rm -rf "$collection_path"
    fi
  done
}
