#!/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")
}

# Check details of installed .rpm package function
check_installed_rpm() {
	rpm_data=$(rpm -qi $1)
	ARCH=$(echo "$rpm_data" | grep Architecture | cut -f 2 -d ' ')
	VERSION=$(echo "$rpm_data" | grep Version | cut -f 2 -d ':' | xargs)
	DATE=$(echo "$rpm_data" | grep "Build Date" | awk '{print $5 " " $6 " " $7}')
	TIME=$(echo "$rpm_data" | grep "Build Date" | awk '{print $8 " " $9 " " $10}')
}

# 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
if [ -f '/etc/redhat-release' ]; then
	hestia_v=$(dnf list hestia --available | grep -v '.src' | grep hestia | awk '{print $2}' | cut -f 1 -d '-')
	nginx_v=$(dnf list hestia-nginx --available | grep -v '.src' | grep hestia-nginx | awk '{print $2}' | cut -f 1 -d '-')
	php_v=$(dnf list hestia-php --available | grep -v '.src' | grep hestia-php | awk '{print $2}' | cut -f 1 -d '-')
else
	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)
fi

# Checking installed hestia version
if [ -f '/etc/redhat-release' ]; then
	check_installed_rpm hestia
else
	check_installed_deb hestia
fi

UPDATED='yes'
if [ -n "$hesta_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
if [ -f '/etc/redhat-release' ]; then
	check_installed_rpm hestia-php
else
	check_installed_deb hestia-php
fi

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
if [ -f '/etc/redhat-release' ]; then
	check_installed_rpm hestia-nginx
else
	check_installed_deb hestia-nginx
fi

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