#!/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: 70, 74, 80"
	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=""
if [ "$LOCAL_PHP" == "yes" ]; then
	php_fpm="/usr/lib/systemd/system/brepo-php-fpm$version.service"
else
	php_fpm="/usr/lib/systemd/system/php$version-php-fpm.service"
fi

if [ "$LOCAL_PHP" == "yes" ]; then
	rm -f /opt/brepo/php*/etc/php-fpm.d/
	cp -f $HESTIA/install/rpm/php-fpm/www.conf /opt/brepo/php${version}/etc/php-fpm.d/www.conf
else
	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
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 [ "$LOCAL_PHP" == "yes" ]; then
	result_alt_php=$(alternatives --display php | grep /opt/brepo/php${version}/bin/php)
	if [ -z "$result_alt_php" ]; then
		alternatives --install /usr/bin/php php /opt/brepo/php${version}/bin/php 1 > /dev/null 2>&1
	fi
else
	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

if [ "$LOCAL_PHP" == "yes" ]; then
	update-alternatives --set php /opt/brepo/php${version}/bin/php
	if [ $? -eq 0 ]; then
		update-alternatives --install /usr/bin/php php /opt/brepo/php${version}/bin/php 1
		update-alternatives --set php /opt/brepo/php${version}/bin/php
	fi
else
	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
fi
/usr/bin/hestiacp-php-admin system $version

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

log_event "$OK" "$ARGUMENTS"