#!/bin/bash

# Expect base collection path as an argument
BASE_COLLECTION_PATH=$1

# Expect time option as an argument
SINCE_TIME=$2

# Use PWD as base path if no argument is passed
if [ "${BASE_COLLECTION_PATH}" = "" ]; then
    BASE_COLLECTION_PATH=$(pwd)
fi

# Make a global variable for namespace
INSTALL_NAMESPACE=openshift-storage

NOOBAA_COLLLECTION_PATH="${BASE_COLLECTION_PATH}/noobaa"
mkdir -p "${NOOBAA_COLLLECTION_PATH}"

noobaa_resources=()

noobaa_resources+=(noobaa)
noobaa_resources+=(backingstore)
noobaa_resources+=(bucketclass)

noobaa_cli=()
noobaa_cli+=("status")
noobaa_cli+=("obc list")

noobaa_desc=()
noobaa_desc+=("pod noobaa-db-pg-0")
noobaa_desc+=("statefulset.apps noobaa-db-pg")

/templates/noobaa.template

mkdir -p "${NOOBAA_COLLLECTION_PATH}/raw_output/}"

# Run the Collection of Noobaa cli using must-gather
# shellcheck disable=SC2086
for cli in "${noobaa_cli[@]}"; do
    echo "collecting dump of ${cli}" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
    COMMAND_OUTPUT_FILE="${NOOBAA_COLLLECTION_PATH}"/raw_output/${cli// /_}
    { timeout 180 noobaa ${cli} --namespace openshift-storage >> "${COMMAND_OUTPUT_FILE}"; } >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
done
noobaa diagnose --dir "${NOOBAA_COLLLECTION_PATH}"/raw_output/ --namespace openshift-storage  >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1

# Run the Collection of NooBaa Resources using must-gather
for resource in "${noobaa_resources[@]}"; do
    echo "collecting dump of ${resource}" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
    { oc adm --dest-dir="${NOOBAA_COLLLECTION_PATH}" inspect "${resource}" --all-namespaces --"${SINCE_TIME}"; } >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
done

# Collect logs for all noobaa pods using oc logs
# get all namespaces that contain any noobaa pod
NOOBAA_PODS_LABEL='app in (noobaa)'
for ns in $(oc get pod --all-namespaces -l "${NOOBAA_PODS_LABEL}" | grep -v NAMESPACE | awk '{print $1}' | uniq)
do
    #get logs for all pods with label app=noobaa
    for pod in $(oc -n "${ns}" get pod -l "${NOOBAA_PODS_LABEL}" | grep -v NAME | awk '{print $1}'); do
        echo "collecting dump of ${pod} pod from ${ns}" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
        LOG_DIR=${NOOBAA_COLLLECTION_PATH}/logs/${ns}
        mkdir -p "${LOG_DIR}"
        { timeout 120 oc -n "${ns}" logs --all-containers "${pod}" &> "${LOG_DIR}"/"${pod}".log; } >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
    done
done

# Collecting noobaa db pod logs with postgres label
NOOBAA_POSTGRES_LABEL='noobaa-db in (postgres)'
for pod in $(oc -n "${ns}" get pods -l "${NOOBAA_POSTGRES_LABEL}" | grep -v NAME | awk '{print $1}'); do
    echo "collecting noobaa db pod logs from ${ns}" | tee -a "${BASE_COLLECTION_PATH}"/gather-debug.log
    { timeout 120 oc -n "${ns}" logs --all-containers "${pod}" &> "${LOG_DIR}"/"${pod}".log; } >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
done

# Add important notfications to a notifications.txt file at the root of the noobaa collection path
UNMANAGED_NOOBAA_NOTIF="The noobaa deployed on this cluster is not managed by the storagecluster CR and will not react to configuration changes made to storagecluster CR"

NOOBAA_RECONCILE_STRATEGY=$( oc -n "${ns}" get storagecluster -o jsonpath='{.items[0].spec.multiCloudGateway.reconcileStrategy}' 2> /dev/null)
if [ "${NOOBAA_RECONCILE_STRATEGY}" = "ignore" ] || [ "${NOOBAA_RECONCILE_STRATEGY}" = "standalone" ]; then
    if [ "$(oc -n "${ns}" get noobaa -o name 2> /dev/null)" ]; then
        echo "- ${UNMANAGED_NOOBAA_NOTIF}" >> "${NOOBAA_COLLLECTION_PATH}/notifications.txt"
    fi
fi

# Create the dir for oc_output
mkdir -p "${NOOBAA_COLLLECTION_PATH}/namespaces/${INSTALL_NAMESPACE}/oc_output/"

# Run the Collection of OC desc commands for noobaa
for noobaa_desc in "${noobaa_desc[@]}"; do
     echo "collecting oc describe command ${noobaa_desc}" | tee -a "${BASE_COLLECTION_PATH}/gather-debug.log"
     COMMAND_OUTPUT_FILE=${NOOBAA_COLLLECTION_PATH}/namespaces/${INSTALL_NAMESPACE}/oc_output/${noobaa_desc// /_}
     # shellcheck disable=SC2086
     { oc describe ${noobaa_desc} -n ${INSTALL_NAMESPACE}; } >> "${COMMAND_OUTPUT_FILE}"
done
