#!/bin/bash
# Expect base collection path as an argument
BASE_COLLECTION_PATH=$1

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

# Expect time option as an arguement
SINCE_TIME=$2

# Make a globle variable for namespace
INSTALL_NAMESPACE=openshift-storage
RAMEN_NAMESPACE=openshift-dr-system

# Add general resources to list if necessary

# Resource List
resources=()
# collect storagecluster resources

# TODO: Re enable the collection of storagecluster via inspect command
# resources+=(storageclusters)
# resources+=(storagesystem)

# collect OB/OBC resoureces
resources+=(objectbuckets)

#collect openshift-dr-system namespaced resources
dr_resources=()
dr_resources+=(secrets)
dr_resources+=(configmap)
dr_resources+=(pods -owide)

# collection path for OC commands
mkdir -p "${BASE_COLLECTION_PATH}/oc_output/"

# Command List
commands_get=()

# collect oc output of OC get commands
commands_get+=("pods -owide")
commands_get+=("subscription")
commands_get+=("csv")
commands_get+=("installplan")
commands_get+=("events")
commands_get+=("all -o wide")
commands_get+=("role")
commands_get+=("rolebinding")
commands_get+=("volumesnapshot -A")
commands_get+=("volumesnapshotclass")
commands_get+=("volumesnapshotcontent")

# collect oc output of OC desc commands
commands_desc=()
commands_desc+=("pods")
commands_desc+=("storagecluster")
commands_desc+=("volumesnapshot -A")
commands_desc+=("volumesnapshotclass")
commands_desc+=("volumesnapshotcontent")

# collect yaml output of OC commands
oc_yamls=()
oc_yamls+=("csv")
oc_yamls+=("installplan")
oc_yamls+=("volumesnapshot -A")
oc_yamls+=("volumesnapshotclass")
oc_yamls+=("volumesnapshotcontent")

echo "collecting dump of namespace" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
oc adm --dest-dir="${BASE_COLLECTION_PATH}" inspect ns/"${INSTALL_NAMESPACE}" --"${SINCE_TIME}" >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
echo "collecting dump of clusterresourceversion" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
for oc_yaml in "${oc_yamls[@]}"; do
   # shellcheck disable=SC2129
   oc adm --dest-dir="${BASE_COLLECTION_PATH}" inspect "${oc_yaml}" -n "${INSTALL_NAMESPACE}" --"${SINCE_TIME}" >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1
done

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

# Run the Collection of Resources to list
for command_get in "${commands_get[@]}"; do
     echo "collecting oc command ${command_get}" | tee -a "${BASE_COLLECTION_PATH}/gather-debug.log"
     COMMAND_OUTPUT_FILE=${BASE_COLLECTION_PATH}/namespaces/${INSTALL_NAMESPACE}/oc_output/${command_get// /_}
     # shellcheck disable=SC2086
     { oc get ${command_get} -n ${INSTALL_NAMESPACE}; } >> "${COMMAND_OUTPUT_FILE}"
done

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

# NOTE: This is a temporary fix for collecting the storagecluster as we are not able to collect the storagecluster using the inspect command
{ oc get storageclusters -n ${INSTALL_NAMESPACE} -o yaml; } > "$BASE_COLLECTION_PATH/namespaces/${INSTALL_NAMESPACE}/oc_output/storagecluster.yaml" 2>&1
{ oc get storagesystem -n ${INSTALL_NAMESPACE} -o yaml; } > "$BASE_COLLECTION_PATH/namespaces/${INSTALL_NAMESPACE}/oc_output/storagesystem.yaml" 2>&1

# Create the dir for data from all namespaces
mkdir -p "${BASE_COLLECTION_PATH}/namespaces/all/"

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

echo "collecting dump of openshift-dr-system namespace" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
oc adm --dest-dir="${BASE_COLLECTION_PATH}" inspect ns/"${RAMEN_NAMESPACE}" --"${SINCE_TIME}" >> "${BASE_COLLECTION_PATH}"/gather-debug.log 2>&1

# Create the dir for oc_output for openshift-dr-system namespace
mkdir -p "${BASE_COLLECTION_PATH}/namespaces/${RAMEN_NAMESPACE}/oc_output/"

# Run the Collection of Resources to list
for dr_resource in "${dr_resources[@]}"; do
     echo "collecting oc command ${dr-resource}" | tee -a "${BASE_COLLECTION_PATH}/gather-debug.log"
     COMMAND_OUTPUT_FILE=${BASE_COLLECTION_PATH}/namespaces/${RAMEN_NAMESPACE}/oc_output/${dr_resource// /_}
     # shellcheck disable=SC2086
     { oc get ${dr_resource} -n ${RAMEN_NAMESPACE}; } >> "${COMMAND_OUTPUT_FILE}"
done

# For pvc of all namespaces
echo "collecting dump of oc get pvc all namespaces" | tee -a  "${BASE_COLLECTION_PATH}/gather-debug.log"
{ oc get pvc --all-namespaces; } >> "${BASE_COLLECTION_PATH}/namespaces/all/pvc_all_namespaces"
{ oc adm --dest-dir="${BASE_COLLECTION_PATH}/namespaces/all/" inspect pvc --all-namespaces --"${SINCE_TIME}"; } >> "${BASE_COLLECTION_PATH}/gather-debug.log" 2>&1

# For obc of all namespaces
echo "collecting dump of oc get obc all namespaces" | tee -a  "${BASE_COLLECTION_PATH}/gather-debug.log"
{ oc get obc --all-namespaces; } >> "${BASE_COLLECTION_PATH}/namespaces/all/obc_all_namespaces"
{ oc adm --dest-dir="${BASE_COLLECTION_PATH}/namespaces/all/" inspect obc --all-namespaces --"${SINCE_TIME}"; } >> "${BASE_COLLECTION_PATH}/gather-debug.log" 2>&1

# For VolumeReplication of all namespaces
echo "collecting dump of oc get volumereplication all namespaces" | tee -a  "${BASE_COLLECTION_PATH}/gather-debug.log"
{ oc get volumereplication --all-namespaces; } >> "${BASE_COLLECTION_PATH}/namespaces/all/vr_all_namespaces"
{ oc adm --dest-dir="${BASE_COLLECTION_PATH}/namespaces/all/" inspect volumereplication --all-namespaces --"${SINCE_TIME}"; } >> "${BASE_COLLECTION_PATH}/gather-debug.log" 2>&1

# For VolumeReplicationGroups of all namespaces
echo "collecting dump of oc get volumereplicationgroups all namespaces" | tee -a  "${BASE_COLLECTION_PATH}/gather-debug.log"
{ oc get volumereplicationgroups --all-namespaces; } >> "${BASE_COLLECTION_PATH}/namespaces/all/vrg_all_namespaces"
{ oc adm --dest-dir="${BASE_COLLECTION_PATH}/namespaces/all/" inspect vrg --all-namespaces --"${SINCE_TIME}"; } >> "${BASE_COLLECTION_PATH}/gather-debug.log" 2>&1
