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


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

# Argument definition
format=${1-shell}

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
fields="\$NAME \$VERSION \$RELEASE \$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
        eval $str
        echo -n '    "'$NAME'": {
        "VERSION": "'$VERSION'",
        "RELEASE": "'$RELEASE'",
        "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   REL   ARCH   UPDT   DATE"
    echo "---   ---   ---   ----   ----   ----"
    for str in $(echo -e "$data"); do
        eval $str
        echo "$NAME $VERSION $RELEASE $ARCH $UPDATED $DATE"
    done
}


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

# Checking official latest version
latest=$(wget -q -T 1 -t 1 http://c.vestacp.com/latest.txt -O -)

# Checking installed vesta version
if [ -d "/etc/sysconfig" ]; then
    rpm_format="VERSION='%{VERSION}'"
    rpm_format="$rpm_format RELEASE='%{RELEASE}'"
    rpm_format="$rpm_format ARCH='%{ARCH}'"
    rpm_format="$rpm_format UTIME='%{INSTALLTIME}'\n"
    eval $(rpm --queryformat="$rpm_format" -q vesta)
    DATE=$(date -d @$UTIME +%F)
    TIME=$(date -d @$UTIME +%T)
else
    dpkg_data=$(dpkg-query -s vesta)
    pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/vesta.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 \-)
    RELEASE=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 2 -d \-)
    DATE=$(date -d @$pkg_date +"%F")
    TIME=$(date -d @$pkg_date +"%T")
fi
UPDATED='yes'
if [ ! -z "$latest" ] && [ "$latest" \> "vesta-$VERSION-$RELEASE" ]; then
    UPDATED='no'
fi
data="NAME='vesta' VERSION='$VERSION' RELEASE='$RELEASE' ARCH='$ARCH'"
data="$data UPDATED='$UPDATED' DESCR='core package' TIME='$TIME' DATE='$DATE'"

# Checking installed vesta-php version
if [ -d "/etc/sysconfig" ]; then
    eval $(rpm --queryformat="$rpm_format" -q vesta-php)
    DATE=$(date -d @$UTIME +%F)
    TIME=$(date -d @$UTIME +%T)
else
    dpkg_data=$(dpkg-query -s vesta-php)
    pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/vesta-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 \-)
    RELEASE=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 2 -d \-)
    DATE=$(date -d @$pkg_date +"%F")
    TIME=$(date -d @$pkg_date +"%T")
fi
data="$data\nNAME='vesta-php' VERSION='$VERSION' RELEASE='$RELEASE'"
data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='php interpreter'"
data="$data TIME='$TIME' DATE='$DATE'"

# Checking installed vesta-nginx version
if [ -d "/etc/sysconfig" ]; then
    eval $(rpm --queryformat="$rpm_format" -q vesta-nginx)
    DATE=$(date -d @$UTIME +%F)
    TIME=$(date -d @$UTIME +%T)
else
    dpkg_data=$(dpkg-query -s vesta-nginx)
    pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/vesta-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 \-)
    RELEASE=$(echo "$dpkg_data"|grep ^Version |cut -f 2 -d ' '|cut -f 2 -d \-)
    DATE=$(date -d @$pkg_date +"%F")
    TIME=$(date -d @$pkg_date +"%T")
fi
data="$data\nNAME='vesta-nginx' VERSION='$VERSION' RELEASE='$RELEASE'"
data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='internal web server'"
data="$data TIME='$TIME' DATE='$DATE'"

# Checking installed vesta-ioncube version
if [ "$SOFTACULOUS" = 'yes' ]; then
    if [ -d "/etc/sysconfig" ]; then
        eval $(rpm --queryformat="$rpm_format" -q vesta-ioncube)
        DATE=$(date -d @$UTIME +%F)
        TIME=$(date -d @$UTIME +%T)
    else
        dpkg_data=$(dpkg-query -s vesta-ioncube)
        pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/vesta-php.list)
        ARCH=$(echo "$dpkg_data"|grep Architecture | cut -f 2 -d ' ')
        VERSION=$(echo "$dpkg_data"|grep ^Vers |cut -f2 -d ' '|cut -f1 -d \-)
        RELEASE=$(echo "$dpkg_data"|grep ^Vers |cut -f2 -d ' '|cut -f2 -d \-)
        DATE=$(date -d @$pkg_date +"%F")
        TIME=$(date -d @$pkg_date +"%T")
    fi
    data="$data\nNAME='vesta-ioncube' VERSION='$VERSION' RELEASE='$RELEASE'"
    data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='php encoder'"
    data="$data TIME='$TIME' DATE='$DATE'"
fi

# Checking installed vesta-softaculous version
if [ "$SOFTACULOUS" = 'yes' ]; then
    if [ -d "/etc/sysconfig" ]; then
        eval $(rpm --queryformat="$rpm_format" -q vesta-softaculous)
        DATE=$(date -d @$UTIME +%F)
        TIME=$(date -d @$UTIME +%T)
    else
        dpkg_data=$(dpkg-query -s vesta-softaculous)
        pkg_date=$(stat -c "%Y" /var/lib/dpkg/info/vesta-php.list)
        ARCH=$(echo "$dpkg_data"|grep Architecture | cut -f 2 -d ' ')
        VERSION=$(echo "$dpkg_data"|grep ^Vers |cut -f2 -d ' '|cut -f1 -d \-)
        RELEASE=$(echo "$dpkg_data"|grep ^Vers |cut -f2 -d ' '|cut -f2 -d \-)
        DATE=$(date -d @$pkg_date +"%F")
        TIME=$(date -d @$pkg_date +"%T")
    fi
    data="$data\nNAME='vesta-softaculous' VERSION='$VERSION' RELEASE='$RELEASE'"
    data="$data ARCH='$ARCH' UPDATED='$UPDATED' DESCR='app installer'"
    data="$data TIME='$TIME' DATE='$DATE'"
fi


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


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

exit
