#!/bin/bash -x

export BASE_COLLECTION_PATH="${BASE_COLLECTION_PATH:-/must-gather}"
export PROS=${PROS:-5}
DIR_NAME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Resource list
resources=()

# HCO
resources+=(catalogsource)

resources+=(Network-attachment-definitions)

# NodeNetworkState
resources+=(nodenetworkstates nodenetworkconfigurationenactments nodenetworkconfigurationpolicies)

# ImageStreamTag
resources+=(istag)

# Nodes and Machines
resources+=(nodes)
resources+=(machines)

${DIR_NAME}/version

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

${DIR_NAME}/gather_virtualmachines

# Collect relevant namespaces data
${DIR_NAME}/gather_ns

# Collect HCO details (first, because it's there before anything else)
${DIR_NAME}/gather_hco

# Collect HCO details (first, because it's there before anything else)
${DIR_NAME}/gather_hco

# Collect the CRDs
${DIR_NAME}/gather_crds

# Collect the CRs explicitely - some of them, like VMs, are collected by steps below
${DIR_NAME}/gather_crs

# Collect the apiservices
${DIR_NAME}/gather_apiservices

# Collect SSP details
${DIR_NAME}/gather_ssp

# Collect nodes details
${DIR_NAME}/gather_nodes

# Collect CDI details
${DIR_NAME}/gather_cdi

# Collect the webhooks
${DIR_NAME}/gather_webhooks

# Collect v2v details
${DIR_NAME}/gather_v2v

${DIR_NAME}/logs.sh

sync

exit 0
