#!/bin/bash
# info: update web templates
# options: [RESTART] [SKIP]
#
# example: v-update-web-templates
#
# This function for obtaining updated web (Nginx/Apache2/PHP) templates from the Hestia package.

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

# Argument definition
restart=$1
skip=$2

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

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

# Check if /install/upgrade/manual/install_awstats_geoip.sh has been applied
awstats_patch=$(cat $HESTIA/data/templates/web/awstats/awstats.tpl | grep "LoadPlugin=\"geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat\"")

# Skeleton, Unassinged and Suspended
if [ "$POLICY_SYNC_SKELETON" != "no" ]; then
	rm -rf "${WEBTPL}/skel" 2> /dev/null
	rm -rf "${WEBTPL}/suspend" 2> /dev/null
	rm -rf "${WEBTPL}/unassigned" 2> /dev/null

	for webtpl_folder in $(ls $HESTIA_COMMON_DIR/templates/web/* -d 2> /dev/null); do
		cp -rf "${webtpl_folder}" "${WEBTPL}/"
	done
fi

[ -d "${WEBTPL}/nginx" ] || mkdir -p "${WEBTPL}/nginx"

if [ "$PROXY_SYSTEM" = 'nginx' ] || [ "$WEB_BACKEND" = "php-fpm" ]; then
	cp -rf "${HESTIA_INSTALL_DIR}/templates/web/nginx" "${WEBTPL}/"
fi

for webtpl_folder in $(ls $HESTIA_INSTALL_DIR/templates/web/* -d 2> /dev/null | egrep -v '/(nginx)$'); do
	cp -rf "${webtpl_folder}" "${WEBTPL}/"
done

versions_list=""
if [ "$LOCAL_PHP" == "yes" ]; then
	versions_list=$(ls -d /opt/brepo/php*)
	for php_ver in $versions_list; do
		[ ! -d "$php_ver/etc/php-fpm.d" ] && continue
		vers=$(echo "$php_ver" | awk -F"/" '{ print $4 }' | sed  "s/php\([[:digit:]]\+\)/\1/g")
		cp -f $HESTIA_INSTALL_DIR/php-fpm/multiphp.tpl ${WEBTPL}/php-fpm/PHP-${vers/\./_}.tpl
	done
else
	versions_list=$(ls -d /etc/opt/remi/php*)
	for php_ver in $versions_list; do
		[ ! -d "$php_ver/php-fpm.d" ] && continue
		vers=$(echo "$php_ver" | awk -F"/" '{ print $5 }' | sed  "s/php\([[:digit:]]\+\)/\1/g")
		cp -f $HESTIA_INSTALL_DIR/php-fpm/multiphp.tpl ${WEBTPL}/php-fpm/PHP-${vers/\./_}.tpl
	done
fi

if [ -n "$awstats_patch" ]; then
	# restore LoadPlugin variable
	echo "LoadPlugin=\"geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat\"" >> $HESTIA/data/templates/web/awstats/awstats.tpl
fi

# Rebuilding web domains
if [ -z "$skip" ]; then
	for user in $($BIN/v-list-sys-users plain); do
		$BIN/v-rebuild-web-domains "$user" "no"
	done
fi

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

# Restarting web server
$BIN/v-restart-web "$restart"
check_result $? "restart" > /dev/null 2>&1

$BIN/v-restart-proxy "$restart"
check_result $? "restart" > /dev/null 2>&1

$BIN/v-log-action "system" "Info" "Updates" "Default web domain templates updated."

exit
