#!/bin/bash
# info: delete php fpm version
# options: VERSION
#
# example: v-delete-web-php 7.3
#
# This function checks and delete a fpm php version if not used by any domain.

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

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

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

check_args '1' "$#" 'VERSION'

if [ -z "$WEB_BACKEND" ]; then
	echo "Multiple php versions are not supported for modphp"
fi

# 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

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


# Remove backend template
[ -f $HESTIA/data/templates/web/php-fpm/PHP-${version/\./_}.tpl ] && rm -f $HESTIA/data/templates/web/php-fpm/PHP-${version/\./_}.tpl

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

mph=""
if [ "$LOCAL_PHP" == "yes" ]; then
	mph="brepo-php$version brepo-php$version-fpm"
else
	mph="php$version-common php$version-mbstring php$version-bcmath php$version-cli php$version-curl
	php$version-fpm php$version-gd php$version-intl php$version-mysql
	php$version-soap php$version-xml php$version-zip php$version-mbstring
	php$version-json php$version-bz2 php$version-pspell php$version-imagick php$version-pgsql
	php$version-imap php$version-ldap"

	# Packages in Remi repo have names with php$version-php- prefixes
	mph="php$version-php-fpm php$version-php-cgi php$version-php-mysqlnd php$version-php-pgsql 
		php$version-php-pdo php$version-php-common php$version-php-pecl-imagick php$version-php-imap 
		php$version-php-ldap php$version-php-pecl-apcu php$version-php-pecl-zip php$version-php-cli 
		php$version-php-opcache php$version-php-xml php$version-php-gd php$version-php-intl 
		php$version-php-mbstring php$version-php-pspell php$version-php-readline"


	# Check is version is 7.1 or below to add mcrypt
	if [[ $(echo "$version 72" | awk '{print ($1 < $2)}') == 1 ]]; then
		mph="$mph php$version-php-mcrypt"
	fi
fi

# Purge php packages
dnf remove -y $mph > /dev/null 2>&1 &

BACK_PID=$!

# Check if package removal is done, print a spinner
echo "Removing PHP-$version, please wait..."
spinner="/-\|"
spin_i=1
while kill -0 $BACK_PID > /dev/null 2>&1; do
	printf "\b${spinner:spin_i++%${#spinner}:1}"
	sleep 0.5
done

# Do a blank echo to get the \n back
echo

# Check if installation was successfully
if [ -f "$php_fpm" ]; then
	echo "ERROR: Uninstallation failed, please run the following command manually for debugging:"
	echo "dnf remove $mph"
	exit 1
fi

# Cleanup php folder
if [ "$LOCAL_PHP" == "yes" ]; then
	[[ -d /opt/brepo/php$version ]] && rm -rf "/opt/brepo/php$version"
else
	[[ -d /etc/opt/remi/php$version ]] && rm -rf "/etc/opt/remi/php$version"
fi

if [ "$LOCAL_PHP" == "yes" ]; then
	if [ "$WEB_BACKEND" = "php-fpm" ]; then
		conf=$(find /opt/brepo/php* -name www.conf)
		# Check if www.conf exists
		if [ -z "$conf" ]; then
			# If not grab the "last php version
			last=$($BIN/v-list-sys-php "shell" | tail -n1)
			cp -f $HESTIA/install/rpm/php-fpm/www.conf /opt/brepo/php$last/etc/php-fpm.d/www.conf
			$BIN/v-restart-web-backend
		fi
	fi
else
	if [ "$WEB_BACKEND" = "php-fpm" ]; then
		conf=$(find /etc/opt/remi/php* -name www.conf)
		# Check if www.conf exists
		if [ -z "$conf" ]; then
			# If not grab the "last php version
			last=$($BIN/v-list-sys-php "shell" | tail -n1)
			cp -f $HESTIA/install/rpm/php-fpm/www.conf /etc/opt/remi/php$last/php-fpm.d/www.conf
			$BIN/v-restart-web-backend
		fi
	fi
fi

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

# Logging
$BIN/v-log-action "system" "Info" "System" "Uninstalled PHP $version."
log_event "$OK" "$ARGUMENTS"

exit
