#!/bin/bash -x

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

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

# Run the collection of namespaces using must-gather
echo "${namespaces[@]}" | 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
