#!/bin/bash
# info: list system updates
# options: [FORMAT]
#
# example: v-list-sys-hestia-updates
#
# 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"

fields="\$NAME \$VERSION \$ARCH \$UPDATED \$DESCR \$TIME \$DATE"

# Check details of installed .deb package function
check_installed_deb() {
	dpkg_data=$(dpkg-query -s $1)
	pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/$1.list)
	ARCH=$(echo "$dpkg_data" | grep Architecture | cut -f 2 -d ' ')
	VERSION=$(echo "$dpkg_data" | grep ^Version | cut -f 2 -d ' ')
	DATE=$(date -d @$pkg_date +"%F")
	TIME=$(date -d @$pkg_date +"%T")
}

# JSON list function
json_list() {
	IFS=$'\n'
	i=1
	objects=$(echo -e "$data" | grep NAME | wc -l)
	echo "{"
	for str in $(echo -e "$data"); do
		parse_object_kv_list "$str"
		echo -n '    "'$NAME'": {
        "VERSION": "'$VERSION'",
        "ARCH": "'$ARCH'",
        "UPDATED": "'$UPDATED'",
        "DESCR": "'$DESCR'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "PKG   VER   ARCH   UPDT   DATE"
	echo "---   ---   ----   ----   ----"
	for str in $(echo -e "$data"); do
		parse_object_kv_list "$str"
		echo "$NAME $VERSION $ARCH $UPDATED $DATE"
	done
}

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

# Checking official latest version
hestia_v=$(apt-cache policy hestia | grep Candidate | cut -d ':' -f 2 | xargs)
nginx_v=$(apt-cache policy hestia-nginx | grep Candidate | cut -d ':' -f 2 | xargs)
php_v=$(apt-cache policy hestia-php | grep Candidate | cut -d ':' -f 2 | xargs)

# Checking installed hestia version
check_installed_deb hestia

UPDATED='yes'
if [ -n "$hestia_v" ] && [ "$hestia_v" \> "$VERSION" ]; then
	UPDATED='no'
fi

data="NAME='hestia' VERSION='$VERSION' ARCH='$ARCH'"
data="$data UPDATED='$UPDATED' DESCR='Hestia core package' TIME='$TIME' DATE='$DATE'"

# Checking installed hestia-php version
check_installed_deb hestia-php

UPDATED='yes'
if [ -n "$php_v" ] && [ "$php_v" \> "$VERSION" ]; then
	UPDATED='no'
fi

data="$data\nNAME='hestia-php' VERSION='$VERSION'"
data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='Hestia internal php interpreter'"
data="$data TIME='$TIME' DATE='$DATE'"

# Checking installed hestia-nginx version
check_installed_deb hestia-nginx

UPDATED='yes'
if [ -n "$nginx_v" ] && [ "$nginx_v" \> "$VERSION" ]; then
	UPDATED='no'
fi

data="$data\nNAME='hestia-nginx' VERSION='$VERSION'"
data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='Hestia internal web server'"
data="$data TIME='$TIME' DATE='$DATE'"

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

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

exit
