#!/bin/bash
# info: Change default php version server wide
# options: VERSION

# example: v-change-sys-php 8.0

version=$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"

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

check_args '1' "$#" 'VERSION'

# Verify php version format
if [[ ! $version =~ ^[0-9]\.[0-9]+ ]]; then
	echo "The specified PHP version format is invalid, it should look like [0-9].[0-9]."
	echo "Example: 7.0, 7.4, 8.0"
	exit "$E_INVALID"
fi

# Check if php version exists
version_check=$($BIN/v-list-sys-php plain | grep "$version")
if [ -z "$version_check" ]; then
	echo "ERROR: Specified PHP version is not installed."
	exit "$E_INVALID"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Set file locations
php_fpm="/etc/init.d/php$version-fpm"

rm -f /etc/php/*/fpm/pool.d/www.conf
cp -f $HESTIA/install/deb/php-fpm/www.conf /etc/php/$version/fpm/pool.d/www.conf

for user in $($BIN/v-list-sys-users plain); do
	$BIN/v-rebuild-web-domains "$user" 'no' > /dev/null 2>&1
	$BIN/v-rebuild-mail-domains "$user" 'no' > /dev/null 2>&1
done

# Reload "current" php version before reload everthing
$BIN/v-restart-web-backend '' $(multiphp_default_version)
$BIN/v-restart-web-backend
$BIN/v-restart-web
$BIN/v-restart-proxy

update-alternatives --set php /usr/bin/php$version

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

log_event "$OK" "$ARGUMENTS"