#!/bin/bash
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

bold=`tput bold`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`


jetson_release()
{
    local VERBOSE=$1

    # Load JETSON environment variables
    JTOP_VARIABLE=""
    JTOP_PYTHON=""
    if type -P python3 >/dev/null 2>&1 ; then
        JTOP_VARIABLE=$(python3 -c "import jtop; print(jtop.__path__[0])" 2> /dev/null)
        JTOP_PYTHON="$(python3 -V 2>&1)"
    fi
    if type -P python >/dev/null 2>&1 && [ -z $JTOP_VARIABLE ] ; then
        JTOP_VARIABLE=$(python -c "import jtop; print(jtop.__path__[0])" 2> /dev/null)
        JTOP_PYTHON="$(python -V 2>&1)"
    fi
    if [ -z $JTOP_VARIABLE ] ; then
        echo "${red}I cannot locate jetson_variables${reset}"
        exit 1
    fi
    source $JTOP_VARIABLE/jetson_variables
    source $JTOP_VARIABLE/jetson_libraries

    # Load NVP model status
    if hash nvpmodel 2>/dev/null; then
        NVPModel="$(nvpmodel -q 2>/dev/null)"
        NVPModel=$(echo $NVPModel | sed 's/.*Mode://')
        # Extract model and type
        NVPModel_type=$(echo $NVPModel | cut -d' ' -f 2)
        NVPModel=$(echo $NVPModel | cut -d' ' -f 1)
    fi
    # Read status jetson_performance service
    JETSON_STATS_SERVICE_STATUS="$(systemctl is-active jetson_stats.service)"
    # Extract jetson-stats version
    JETSON_STATS_VERSION="$(jtop -v  2>&1 | cut -d " " -f2)"


    # Print Jetson version
    echo "${bold} - $JETSON_MODEL${reset}"
    # Print Jetpack and kernel
    echo "   * Jetpack $JETSON_JETPACK [L4T $JETSON_L4T]"
    # Print status NVPModel
    if [ ! -z ${NVPModel+x} ] ; then
        echo "   * NV Power Mode: ${green}$NVPModel${reset} - Type: ${green}$NVPModel_type${reset}"
    fi
    # Print status Jetson Performance service
    if [ $JETSON_STATS_SERVICE_STATUS = "active" ] ; then
        echo "   * jetson_stats.service: ${green}$JETSON_STATS_SERVICE_STATUS${reset}"
    else
        echo "   * jetson_stats.service: ${red}$JETSON_STATS_SERVICE_STATUS${reset}"
    fi

    if $VERBOSE ; then
        # Board information
        echo "${bold} - Board info:${reset}"
        echo "   * SOC Family: $JETSON_SOC"
        if [ ! -z $JETSON_CHIP_ID ] ; then
            echo "   * Chip ID:$JETSON_CHIP_ID"
        fi
        echo "   * Module: $JETSON_MODULE - Carrier: $JETSON_CARRIER"
        echo "   * Code Name: $JETSON_CODENAME"
        # If available print the Board IDS
        if [ ! -z "$JETSON_BOARDIDS" ] ; then
            echo "   * Boardids: $JETSON_BOARDIDS"
        fi
        # Print CUDA GPU architecture
        echo "   * CUDA GPU architecture (ARCH_BIN): $JETSON_CUDA_ARCH_BIN"
        # Print serial number
        if [ ! -z $JETSON_SERIAL_NUMBER ] ; then
            echo "   * Serial Number: ${JETSON_SERIAL_NUMBER^^}" # Make string to upper case
        fi
    fi

    # Libraries
    echo "${bold} - Libraries:${reset}"
    # Print Cuda version
    echo "   * CUDA: $JETSON_CUDA"
    # Print cuDNN version
    echo "   * cuDNN: $JETSON_CUDNN"
    # Print TensorRT version
    echo "   * TensorRT: $JETSON_TENSORRT"
    # Print VisionWorks version
    # VisionWorks is not part of Jetpack from 5.0
    if [ $JETSON_L4T_RELEASE -lt 34 ] ; then
        echo "   * Visionworks: $JETSON_VISIONWORKS"
    fi
    # Print OpenCv version and cuda compiled
    if [ $JETSON_OPENCV_CUDA = "YES" ] ; then
        echo "   * OpenCV: $JETSON_OPENCV compiled CUDA: ${green}$JETSON_OPENCV_CUDA${reset}"
    else
        echo "   * OpenCV: $JETSON_OPENCV compiled CUDA: ${red}$JETSON_OPENCV_CUDA${reset}"
    fi
    # Print VPI version
    echo "   * VPI: $JETSON_VPI"
    # Print Vulkan version
    echo "   * Vulkan: $JETSON_VULKAN_INFO"
    if $VERBOSE ; then
        # jetson-stats version
        echo "${bold} - jetson-stats:${reset}"
        echo "   * Version $JETSON_STATS_VERSION"
        echo "   * Works on $JTOP_PYTHON"
    fi
}

usage()
{
    if [ "$1" != "" ]; then
        echo "${red}$1${reset}"
    fi

    echo "jetson_release. [-v | --verbose] | [-h]"
    echo "   Show detailed information about this board. Machine, Jetpack, libraries and other."
    echo "Usage:"
    echo "$0 [options]"
    echo "options,"
    echo "   -h|--help     | This help"
    echo "   -v|--verbose  | Detailed information about this board"
}

main()
{
    local VERBOSE=false
    # Decode all information from startup
    while [ -n "$1" ]; do
        case "$1" in
            -h|--help)
                usage
                exit 0
                ;;
            -v | --verbose)
                VERBOSE=true
                ;;
            *)
                usage "[ERROR] Unknown option: $1"
                exit 1
            ;;
        esac
            shift 1
    done

    # Run jetson_release
    jetson_release $VERBOSE
}

main $@
exit 0
# EOF
