#!/bin/bash
# info: list system updates
# options: [FORMAT]
#
# The function checks available updates for hestia packages.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
format=${1-shell}

# Includes
source $HESTIA/func/main.sh
source $HESTIA/conf/hestia.conf
fields="\$NAME \$VERSION \$ARCH \$UPDATED \$DESCR \$TIME \$DATE"

# 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
dpkg_data=$(dpkg-query -s hestia)
pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/hestia.list)
ARCH=$(echo "$dpkg_data"|grep Architecture |cut -f 2 -d ' ')
VERSION=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 1 -d \-)
DATE=$(date -d @$pkg_date +"%F")
TIME=$(date -d @$pkg_date +"%T")

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

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

# Checking installed hestia-php version
dpkg_data=$(dpkg-query -s hestia-php)
pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/hestia-php.list)
ARCH=$(echo "$dpkg_data"|grep Architecture | cut -f 2 -d ' ')
VERSION=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 1 -d \-)
DATE=$(date -d @$pkg_date +"%F")
TIME=$(date -d @$pkg_date +"%T")

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

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

# Checking installed hestia-nginx version
dpkg_data=$(dpkg-query -s hestia-nginx)
pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/hestia-nginx.list)
ARCH=$(echo "$dpkg_data"|grep Architecture | cut -f 2 -d ' ')
VERSION=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 1 -d \-)
DATE=$(date -d @$pkg_date +"%F")
TIME=$(date -d @$pkg_date +"%T")

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

data="$data\nNAME='hestia-nginx' VERSION='$VERSION'"
data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='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
