#!/bin/bash

NVLSM_CACHE_DIR=${NVLSM_CACHE_DIR:-"/var/cache/nvlsm"}
NVLSM_LOGS_DIR=${NVLSM_LOGS_DIR:-"/var/log/"}
NVLSM_CONF_DIR=${NVLSM_CONF_DIR:-"/usr/share/nvidia/nvlsm/"}
NVLSM_LOG_PREFIX=${NVLSM_LOG_PREFIX:-"nvlsm"}

nvlsm_cache_dir=${NVLSM_CACHE_DIR}
nvlsm_logs_dir=${NVLSM_LOGS_DIR}
nvlsm_conf_dir=${NVLSM_CONF_DIR}

mode_list_files=
mode_stdout=
mode_archive_files=

function show_usage() {
    echo ""
    echo "NVLSM logs collector"
    echo ""
    echo ""
    echo "Usage $(basename $0) [options]"
    echo ""
    echo "Collect SM log files"
    echo ""
    echo "Options:"
    echo ""
    echo "Output options"
    echo "  --list                    List SM files"
    echo ""
    echo "  --archive <path>          Creates archive"
    echo "                            For example: '--archive /tmp/archive' will create"
    echo "                            archive file named /tmp/archive.tar with collected"
    echo "                            log and cache files."
    echo ""
    echo "  --stdout                  Write all SM files to stdout"
    echo ""
    echo "File location options:"
    echo "  --logs <path>              Path to SM log files directory."
    echo "                             Default: ${NVLSM_LOGS_DIR}"
    echo ""
    echo "  --cache <path>             Path to SM cache files directory."
    echo "                             Default: ${NVLSM_CACHE_DIR}"
    echo ""
    echo "  --conf <path>              Path to SM configuration files directory."
    echo "                             Default: ${NVLSM_CONF_DIR}"
    echo ""
    echo "Other options:"
    echo "  -h | --help                Show usage."
    echo ""
    echo ""
}


# Return log files names
function get_file_names() {
    local dump_file
    local dump_file_name
    local nvlsm_dump_files=(
        "${NVLSM_LOG_PREFIX}-activity.dump"
        "${NVLSM_LOG_PREFIX}-heldback-switch.dump"
        "${NVLSM_LOG_PREFIX}-lfts.dump"
        "${NVLSM_LOG_PREFIX}-lid-matrix.dump"
        "${NVLSM_LOG_PREFIX}-perflog.backup.json"
        "${NVLSM_LOG_PREFIX}-perflog.json"
        "${NVLSM_LOG_PREFIX}-routers.dump"
        "${NVLSM_LOG_PREFIX}-sa.dump"
        "${NVLSM_LOG_PREFIX}-sl2vl.dump"
        "${NVLSM_LOG_PREFIX}-smdb.dump"
        "${NVLSM_LOG_PREFIX}-statistics.dump"
        "${NVLSM_LOG_PREFIX}-unhealthy-ports.dump"
        "${NVLSM_LOG_PREFIX}-updn-roots.dump"
        "${NVLSM_LOG_PREFIX}-virtualization.dump"
        "${NVLSM_LOG_PREFIX}.fdbs"
        "${NVLSM_LOG_PREFIX}.log"
        "${NVLSM_LOG_PREFIX}.log.0"
        "${NVLSM_LOG_PREFIX}.mcfdbs")

    for dump_file_name in "${nvlsm_dump_files[@]}"; do
        dump_file=${nvlsm_logs_dir}${dump_file_name}
        if [ -e "${dump_file}" ]; then
            echo ${dump_file}
        fi
    done

    if [ -e "${nvlsm_cache_dir}" ]; then
        find "${nvlsm_cache_dir}" -maxdepth 1 -type f -name "*"
    fi

    if [ -e "${nvlsm_conf_dir}" ]; then
        find "${nvlsm_conf_dir}" -maxdepth 1 -type f -name "*"
    fi
}

#
# Parse command line options
#

num_modes=0
while [[ $# -gt 0 ]]; do
    case $1 in
    --list)
        mode_list_files="yes"
        ((num_modes++))
        shift 1;;
    --stdout)
        mode_stdout="yes"
        ((num_modes++))
        shift 1;;
    --archive)
        mode_archive_files="$2";
        ((num_modes++))
        shift 2;;
    --logs)
        nvlsm_logs_dir="$2";
        shift 2;;
    --cache)
        nvlsm_cache_dir="$2";
        shift 2;;
    --conf)
        nvlsm_conf_dir="$2";
        shift 2;;
    -h | --help)
        show_usage
        exit 0
        ;;
    *)
        show_usage
        exit 2
        ;;
    esac
done;


#
# Input validation
#

if [ "${num_modes}" == 0 ]; then
    echo "Need to one output option"
    show_usage
    exit 2
fi

if [ ${num_modes} -gt 1 ]; then
    echo "Need to choose only one output option"
    show_usage
    exit 2
fi


#
# Execution
#

# Return list of files to collect
if [ ! -z "${mode_list_files}" ]; then
    file_names=($(get_file_names))
    for file_name in "${file_names[@]}"; do
        echo "$file_name"
    done
    exit 0
fi

# Create archive
if [ ! -z "${mode_archive_files}" ]; then
    tar_file=${mode_archive_files}.tar
    echo ${tar_file}
    tar -cf ${tar_file} --files-from=/dev/null

    file_names=($(get_file_names))
    for file_name in "${file_names[@]}"; do
        tar -rPf ${tar_file} ${file_name}
    done
    exit 0
fi

# Write all files to stdout
if [ ! -z "${mode_stdout}" ]; then
    file_names=($(get_file_names))
    for file_name in "${file_names[@]}"; do
        echo "*** nvlsm-bug-report: start file ${file_name}"
        cat "${file_name}"
        echo "*** nvlsm-bug-report: end file ${file_name}"
    done
    exit 0
fi
