#!/bin/bash
set -e

# Amlen and the Proxy separate logs useful to an admin (most commonly devices connecting and disconnecting)
# from trace used to diagnose defects in those codebases. We put the admin logs to the openshift pod logs
# but the trace is too verbose by default (the verbosity for both logs and trace are configurable).
# For a mustgather we want to collect the trace files as well as the logs....

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $DIR/../functions/internal/utils

NAMESPACE=$1
OUTPUT_DIR=$2

#Get the logs from the messagesight or amlen pods (MQTT broker)
MBGX_PODS=$(oc -n $NAMESPACE get pods -l "app=mbgx-messagesight" -o jsonpath="{.items[*].metadata.name}")

for MBGX_POD in $MBGX_PODS
do
  set +e
  LOGFILES=$(oc -n $NAMESPACE exec $MBGX_POD -- find /var/messagesight/diag/logs/ -name *.log 2>/dev/null)
  if [[ "$?" == "1" ]]; then
    exit 0
  fi
  set -e

  MBGX_LOG_DESTINATION_DIR=${OUTPUT_DIR}/resources/${NAMESPACE}/mbgx-logs/$MBGX_POD
  mkdir -p $MBGX_LOG_DESTINATION_DIR

  echo "Collecting mbgx logs from '$MBGX_POD'"
  set +e # the tar command may give warnings if files no longer exist, and they get cycled by the operator.
  oc -n $NAMESPACE exec $MBGX_POD -- tar -czf - $LOGFILES > $MBGX_LOG_DESTINATION_DIR/mbgx-logs-$MBGX_POD.tgz 2> /dev/null
  if [[ "$?" != "0" ]]; then
    echo_dim "- Incomplete mbgx logs from $MBGX_POD"
  fi
  tar --no-same-owner -xf $MBGX_LOG_DESTINATION_DIR/mbgx-logs-$MBGX_POD.tgz -C $MBGX_LOG_DESTINATION_DIR 2> /dev/null

  if [[ "$?" == "0" ]]; then
    # Move the MBGX log files to the correct place in the must gather directory structure
    #
    mv $MBGX_LOG_DESTINATION_DIR/var/messagesight/diag/logs/* $MBGX_LOG_DESTINATION_DIR
    rm $MBGX_LOG_DESTINATION_DIR/mbgx-logs-$MBGX_POD.tgz
    rm -r $MBGX_LOG_DESTINATION_DIR/var

  else
    echo_dim "- Unable to get mbgx logs from $MBGX_POD"
  fi
  set -e

done  # MBGX_POD


#Get the logs from the msproxy pods that sit in front of the amlen (or messagesight) pods
MFGX_PODS=$(oc -n $NAMESPACE get pods -l "app=msproxy" -o jsonpath="{.items[*].metadata.name}")

for MFGX_POD in $MFGX_PODS
do
  set +e
  LOGFILES=$(oc -n $NAMESPACE exec $MFGX_POD -- find /var/log/msproxy/${MFGX_POD} -name *.log 2>/dev/null)
  if [[ "$?" == "1" ]]; then
    exit 0
  fi
  set -e

  MFGX_LOG_DESTINATION_DIR=${OUTPUT_DIR}/resources/${NAMESPACE}/mfgx-logs/$MFGX_POD
  mkdir -p $MFGX_LOG_DESTINATION_DIR

  echo "Collecting mfgx logs from '$MFGX_POD'"
  set +e # the tar command may give warnings if files no longer exist, and they get cycled by the operator.
  oc -n $NAMESPACE exec $MFGX_POD -- tar -czf - $LOGFILES > $MFGX_LOG_DESTINATION_DIR/mfgx-logs-$MFGX_POD.tgz 2> /dev/null
  if [[ "$?" != "0" ]]; then
    echo_dim "- Incomplete mfgx logs from $MFGX_POD"
  fi
  tar --no-same-owner -xf $MFGX_LOG_DESTINATION_DIR/mfgx-logs-$MFGX_POD.tgz -C $MFGX_LOG_DESTINATION_DIR 2> /dev/null

  if [[ "$?" == "0" ]]; then
    # Move the MFGX log files to the correct place in the must gather directory structure
    #
    mv $MFGX_LOG_DESTINATION_DIR/var/log/msproxy/${MFGX_POD}/* $MFGX_LOG_DESTINATION_DIR
    rm $MFGX_LOG_DESTINATION_DIR/mfgx-logs-$MFGX_POD.tgz
    rm -r $MFGX_LOG_DESTINATION_DIR/var

  else
    echo_dim "- Unable to get mfgx logs from $MFGX_POD"
  fi
  set -e

done  # MFGX_POD
