#!/bin/bash -x

export BASE_COLLECTION_PATH="${BASE_COLLECTION_PATH:-/must-gather}"
PROS=${PROS:-5}

# Resource list
resources=()

if [[ -n $NS ]]; then
  resources=("$NS")
else
  for i in $(/usr/bin/oc get virtualmachines --all-namespaces --no-headers | awk '{print $1}' | uniq)
  do
    resources+=("$i")
  done
fi

# Run the collection of resources using must-gather
echo ${resources[@]} | tr ' ' '\n' | xargs -t -I{} -P ${PROS} --max-args=1 sh -c 'echo "inspecting namespace $1" && /usr/bin/oc adm inspect --dest-dir ${BASE_COLLECTION_PATH} namespace $1' -- {}

exit 0
