#!/bin/bash
#
# Copyright contributors to the ibm-storage-odf-operator project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Command line argument
FILTER_STRING=$1
ODF_NAMESPACE=$2
IBMCSI_NAMESPACE=$3
BASE_COLLECTION_PATH=$4
SINCE_TIME=$5

# Use must-gather as base path if no argument is passed
if [ "${BASE_COLLECTION_PATH}" = "" ]; then
    BASE_COLLECTION_PATH="must-gather"
fi
mkdir -p ${BASE_COLLECTION_PATH}

if [ "${ODF_NAMESPACE}" = "" ]; then
    ODF_NAMESPACE="openshift-storage"
fi

if [ "${IBMCSI_NAMESPACE}" = "" ]; then
    IBMCSI_NAMESPACE="openshift-storage"
fi

# timestamp for starting of the script
START_TIME=$(date +%r)
start=$(date +%s)
printf "collection started at: %s \n" "${START_TIME}" >> ${BASE_COLLECTION_PATH}/gather-debug.log 2>&1

# Call pre-install.sh
pre-install.sh

# gather scripts
gather_log               ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH}
gather_crd               ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH}
gather_deployment        ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_pod               ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_configmap         ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_service           ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_servicemonitor    ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_rule              ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}
gather_alert             ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH}
gather_event             ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH}
# comment out this line, since dev currently thought secret is sensitive even though it is helpful for debugging
#gather_secret            ${ODF_NAMESPACE} ${IBMCSI_NAMESPACE} ${BASE_COLLECTION_PATH} ${FILTER_STRING}

# Call other gather scripts
#gather_namespaced_resources ${BASE_COLLECTION_PATH} "${SINCE_TIME}"
gather_clusterscoped_resources ${BASE_COLLECTION_PATH} "${SINCE_TIME}"

#storageClusterPresent=$(oc get storagecluster -n openshift-storage -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
#if [ "$(oc get storagecluster -n openshift-storage -o go-template='{{range .items}}{{.spec.externalStorage.enable}}{{"\n"}}{{end}}')" == true ]; then
#   echo "Skipping the ceph and nooobaa collection as External Storage is enabled" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
#elif [ -z "${storageClusterPresent}" ]; then
#    echo "Skipping ceph and nooobaa collection as Storage Cluster is not present" | tee -a  "${BASE_COLLECTION_PATH}"/gather-debug.log
#else
#    echo "Collecting ceph and noobaa logs" | tee -a "${BASE_COLLECTION_PATH}"/gather-debug.log
#    gather_ceph_resources ${BASE_COLLECTION_PATH} "${SINCE_TIME}"
#    gather_noobaa_resources ${BASE_COLLECTION_PATH} "${SINCE_TIME}"
#fi

# Call post-uninstall.sh
post-uninstall.sh

# timestamp for ending of the script
END_TIME=$(date +%r)
end=$(date +%s)
totalTime=$((end-start))
{
    printf "total time taken by collection was %s seconds \n" ${totalTime}
    printf "collection ended at: %s \n" "${END_TIME}"
    echo "deleting empty files"

} >> ${BASE_COLLECTION_PATH}/gather-debug.log 2>&1
find "${BASE_COLLECTION_PATH}" -empty -delete >> ${BASE_COLLECTION_PATH}/gather-debug.log 2>&1
exit 0
