#!/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 [ -f /etc/redhat-release ]; then
	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: 70, 74, 80"
		exit "$E_INVALID"
	fi
else
	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
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
if [ -f /etc/redhat-release ]; then
	php_fpm="/usr/lib/systemd/system/php$version-php-fpm.service"
else
	php_fpm="/etc/init.d/php$version-fpm"
fi

if [ -f /etc/redhat-release ]; then
	rm -f /etc/opt/remi/php*/php-fpm.d/www.conf
	cp -f $HESTIA/install/rpm/php-fpm/www.conf /etc/opt/remi/php$version/php-fpm.d/www.conf
else
	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
fi

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

if [ -f '/etc/redhat-release' ]; then
	result_alt_php=$(alternatives --display php | grep /usr/bin/php$version)
	if [ -z "$result_alt_php" ]; then
		alternatives --install /usr/bin/php php /usr/bin/php$version 1 > /dev/null 2>&1
	fi
fi

update-alternatives --set php /usr/bin/php$version
if [ $? -eq 0 ]; then
	update-alternatives --install /usr/bin/php php /usr/bin/php$version 1
	update-alternatives --set php /usr/bin/php$version
fi
/usr/bin/hestiacp-php-admin system $version

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

log_event "$OK" "$ARGUMENTS"