#!/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 argument
SINCE_TIME=$2

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

# Add general resources to list if necessary

# Resource List
resources=()
# collect lvmcluster resources
#resources+=(lvmclusters)



# 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+=("lvmcluster")
commands_get+=("lvmvolumegroup")
commands_get+=("lvmvolumegroupnodestatus")
commands_get+=("logicalvolume")
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")

# collect oc output of OC desc commands
commands_desc=()
commands_desc+=("lvmcluster")
commands_desc+=("logicalvolume")
commands_desc+=("lvmvolumegroup")
commands_desc+=("lvmvolumegroupsnodestatus")
commands_desc+=("pods")



# collect yaml output of OC commands
oc_yamls=()
oc_yamls+=("csv")
oc_yamls+=("installplan")


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 OC get commands
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


{ oc get lvmclusters -n ${INSTALL_NAMESPACE} -o yaml; } > "$BASE_COLLECTION_PATH/namespaces/${INSTALL_NAMESPACE}/oc_output/lvmcluster.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

# 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
