#!/bin/bash

unset KUBECONFIG

# Explore where Forklift is installed
for localns in $(/usr/bin/oc get forkliftcontrollers.forklift.konveyor.io --all-namespaces --no-headers | awk '{print $1}'); do

  # Collect the Forklift and Kubevirt related CRs
  echo "Gathering Forklift and Kubevirt related CRs for namespaces [${localns}]"
  /usr/bin/gather_crs ${localns} &

  # Collect the logs
  echo "Gathering logs for namespaces [${localns}]"
  /usr/bin/gather_logs ${localns} &

  # Waits for gather_crs, gather_logs, gather_metrics running in background
  echo "Waiting for background gather tasks to finish"
  wait
done

# Tar all must-gather artifacts for faster transmission 
echo "Tarring must-gather artifacts..."
archive_path="/must-gather-archive"
mkdir -p ${archive_path}
tar -zcf ${archive_path}/must-gather.tar.gz /must-gather/
rm -rf /must-gather/*
mv ${archive_path}/must-gather.tar.gz /must-gather/
rmdir ${archive_path}
echo "Created /must-gather/must-gather.tar.gz"


echo "Waiting for copy phase..."
exit 0
