#!/bin/bash
# info: update hestia package/configs
# options: PACKAGE
#
# example: v-update-sys-hestia hestia-php
#
# This function runs as apt update trigger. It pulls shell script from hestia
# server and runs it. (hestia, hestia-nginx and hestia-php are valid options)

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

# Argument definition
package=$1

# 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"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

# Checking arg number
check_args '1' "$#" 'PACKAGE'
is_hestia_package "hestia hestia-nginx hestia-php" "$package"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Update repo
if [ -f '/etc/redhat-release' ]; then
	dnf makecache
else
	apt-get update -o Dir::Etc::sourcelist="sources.list.d/hestia.list" \
		-o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" -qq
fi

# Update hestia package
if [ -f '/etc/redhat-release' ]; then
	dnf install $package -q > /dev/null 2>&1
else
	apt-get install $package -qq > /dev/null 2>&1
fi
check_result $? "$package update failed" "$E_UPDATE"

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

# Logging
log_event "$OK" "$ARGUMENTS"

exit