#!/bin/bash
# info: list system os
# options: [FORMAT]
#
# example: v-list-sys-info
#
# This function checks available updates for hestia packages.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
format=${1-shell}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Retrieve Hestia Control Panel version number
HESTIA_VERSION=$(grep VERSION $HESTIA/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")
HESTIA_RELEASE=$(grep RELEASE_BRANCH $HESTIA/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")

# JSON list function
json_list() {
	echo '{'
	echo '    "sysinfo": {
        "HOSTNAME": "'$HOSTNAME'",
        "OS": "'$OS'",
        "VERSION": "'$VERSION'",
        "ARCH": "'$ARCH'",
        "HESTIA": "'$HESTIA_VERSION'",
        "RELEASE": "'$HESTIA_RELEASE'",
        "UPTIME": "'$UPTIME'",
        "LOADAVERAGE": "'$LOADAVERAGE'"
        }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "HOSTNAME   OS   VER   ARCH  HESTIA  RELEASE  UPTIME   LA"
	echo "--------   --   ---   ----  ------  -------  ------   --"
	echo "$HOSTNAME $OS $VERSION $ARCH $HESTIA_VERSION $HESTIA_RELEASE $UPTIME $LOADAVERAGE"
}

# PLAIN list function
plain_list() {
	echo -e "$HOSTNAME\t$OS\t$VERSION\t$ARCH\t$HESTIA_VERSION\t$HESTIA_RELEASE\t$UPTIME\t$LOADAVERAGE"
}

# CSV list function
csv_list() {
	echo "HOSTNAME,OS,VERSION,ARCH,HESTIA,RELEASE,UPTIME,LOADAVERAGE"
	echo "$HOSTNAME,$OS,$VERSION,$ARCH,$HESTIA_VERSION,$HESTIA_RELEASE,$UPTIME,$LOADAVERAGE"
}

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Check hostname
HOSTNAME=$(hostname)

# Check OS/Release
if [ -d '/etc/sysconfig' ]; then
	if [ -e '/etc/redhat-release' ]; then
		OS='CentOS'
		VERSION=$(cat /etc/redhat-release | tr ' ' '\n' | grep [0-9])
	else
		OS="Amazon"
		VERSION=$(cat /etc/issue | tr ' ' '\n' | grep [0-9])
	fi
else
	if [ "$(lsb_release -si)" == "Ubuntu" ] && [ -e '/etc/debian_version' ]; then
		OS="Ubuntu"
		VERSION=$(grep DISTRIB_RELEASE /etc/lsb-release | cut -f 2 -d '=')
	else
		distro=$(head -n1 /etc/issue | cut -f 1 -d ' ')
		if [ "$distro" = 'Debian' ]; then
			OS="Debian"
			VERSION=$(cat /etc/debian_version)
		else
			OS='UNKNOWN'
			VERSION='UNKNOWN'
		fi
	fi
fi

# Check architecture
ARCH=$(arch)

# Check uptime
UPTIME=$(cat /proc/uptime | cut -f 1 -d ' ' | cut -f 1 -d .)
UPTIME="$(echo $UPTIME / 60 | bc)"

# Check LoadAverage
LOADAVERAGE=$(cat /proc/loadavg | cut -f 1 -d ' ')

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t ;;
esac

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
