#!/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/>.


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

# Load environment variables:
# - JETSON_BOARD
# - JETSON_L4T (JETSON_L4T_RELEASE, JETSON_L4T_REVISION)
# - JETSON_DESCRIPTION
# - JETSON_CUDA
# - JETSON_OPENCV and JETSON_OPENCV_CUDA
JTOP_VARIABLE=$(python3 -c "import jtop; print(jtop.__path__[0])")
source $JTOP_VARIABLE/jetson_variables

jetson_release()
{
# 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

# Print Jetson version
echo " - $JETSON_DESCRIPTION"
# Print Jetpack and kernel
echo "   * Jetpack $JETSON_JETPACK [L4T $JETSON_L4T]"
# Print CUDA GPU architecture
echo "   * CUDA GPU architecture $JETSON_CUDA_ARCH_BIN"
# Print status NVPModel
if [ ! -z ${NVPModel+x} ] ; then
	echo "   * NV Power Mode: ${green}$NVPModel${reset} - Type: ${green}$NVPModel_type${reset}"
fi

# Libraries
echo " - Libraries:"
# Print Cuda version
echo "   * CUDA $JETSON_CUDA"
# Print cuDNN version
echo "   * cuDNN $JETSON_CUDNN"
# Print TensorRT version
echo "   * TensorRT $JETSON_TENSORRT"
# Print VisionWorks version
echo "   * Visionworks $JETSON_VISIONWORKS"
# 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 status Jetson Performance service
JE_PERFOMANCE_STATUS="$(systemctl is-active jetson_performance.service)"
if [ $JE_PERFOMANCE_STATUS = "active" ] ; then
	echo " - Jetson clock service: ${green}$JE_PERFOMANCE_STATUS${reset}"
else
	echo " - Jetson clock service: ${red}$JE_PERFOMANCE_STATUS${reset}"
fi
# jetson-stats version
JETSON_STATS_VERSION="$(jtop -v | cut -d " " -f2)"
echo " - jetson-stats version $JETSON_STATS_VERSION"
}

jetson_fix()
{
	echo "Fix"
}

jetson_health()
{
	JE_PERFOMANCE_STATUS="$(systemctl is-active jetson_performance.service)"
	if [ $JE_PERFOMANCE_STATUS = "failed" ] ; then
		echo "Jetson clock service is ${red}not installed!${reset}"
	fi
}

function usage
{
    echo "usage: jetson_release [ [--check] | [-h] ]"
    echo "  --health      Health status of your jetson-stats package"
	echo "  --fix         Fix the jetson-stats package cleaning all errors"
    echo "  -h | --help   This message"
}

main()
{
	# Decode all information from startup
    while [ -n "$1" ]; do
        case "$1" in
			--health)  jetson_health
                       exit 0 ;;
			--fix)     jetson_fix
					   exit 0 ;;
            -h|--help) usage
					   exit 0 ;;
            *)		   usage "[ERROR] Unknown option: $1"
					   exit 1 ;;
        esac
            shift 1
    done

	jetson_release
}

main $@
exit 0

# EOF



