#!/bin/bash

# Copyright: 2023 Virtuozzo International GmbH

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


[ -n "${NGINXPHPENGINELIB_VERSION:-}" ] && return 0;
NGINXPHPENGINELIB_VERSION="0.1";

include php-common-engine ;
JELASTIC_PHP_EXTENSIONS_DIR='/usr/lib64/jelastic_php';
DEFAULT_PHP_EXTENSIONS_DIR='/usr/lib64/php';

PHP_FPM_BIN_TARGET="/usr/sbin/php-fpm";
PHP_CGIBIN_TARGET="/usr/bin/php-cgi";
PHP_CLI_BIN='/usr/bin/php';
JELASTIC_PHP_BIN_DIR='/usr/lib64/jelastic_php/bin';
$PROGRAM 'grep';

#  
#  name: switchEngine
#  $1 - engine to  switch on
#  
function switchEngine(){
        local param=$1;
        local version;
        case $1 in
		"php5.3")
		        version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '5\.3\.[0-9]*' | sort -r | head -n 1);
		;;
		"php5.4")
			version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '5\.4\.[0-9]*' | sort -r | head -n 1);
		;;
		"php5.5")
			version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '5\.5\.[0-9]*' | sort -r | head -n 1);
		;;
		"php5.6")
			version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '5\.6\.[0-9]*' | sort -r | head -n 1);
		;;
		"php7.0")
			version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '7\.0\.[0-9]*' | sort -r | head -n 1);
		;;
		"php7.1")
			version=$(ls $JELASTIC_PHP_EXTENSIONS_DIR | $GREP -o '7\.1\.[0-9]*' | sort -r | head -n 1);
		;;
		*) 	 
		return 1
        esac
	PHP_FPM_BIN_SOURCE="${JELASTIC_PHP_EXTENSIONS_DIR}/php${version}/sbin/php-fpm";
	PHP_CGIBIN_SOURCE="${JELASTIC_PHP_EXTENSIONS_DIR}/php${version}/bin/php-cgi";
	PHP_BIN_SOURCE="${JELASTIC_PHP_EXTENSIONS_DIR}/php${version}/bin/php";
	PHP_MODULES_SOURCE="${JELASTIC_PHP_EXTENSIONS_DIR}/php${version}/lib64/php/modules";
	
	if [[ -f "${PHP_FPM_BIN_SOURCE}" ]]
	then
		rm -f ${PHP_FPM_BIN_TARGET};
		rm -f ${PHP_CGIBIN_TARGET};
		
		ln -s ${PHP_FPM_BIN_SOURCE} ${PHP_FPM_BIN_TARGET};
		ln -s ${PHP_CGIBIN_SOURCE} ${PHP_CGIBIN_TARGET};
		
		rm -f $DEFAULT_PHP_EXTENSIONS_DIR/modules;
		
		if [[ -d "${PHP_MODULES_SOURCE}" ]]
		then
			ln -s $PHP_MODULES_SOURCE ${DEFAULT_PHP_EXTENSIONS_DIR}/modules;
		else
			echo "no PHP extensions are configured for PHP version $version" 1>&2;
		fi
	else 
		echo "PHP with version $version is not installed" 1>&2;
		exit;
	fi

	if [[ -f "$JELASTIC_PHP_BIN_DIR/php.$version" ]]
	then
    	    rm -f $PHP_CLI_BIN;
    	    ln -s $JELASTIC_PHP_BIN_DIR/php.$version $PHP_CLI_BIN;
        else	
	    		echo "PHP-CLI binary for PHP version $version is not installed" 1>&2;
    	return 0;
	fi
	 isFunction "phpExtensionsManagers" && {  "phpExtensionsManagers" ; }
}

function describeSetVersion(){
	echo "\n\t<version>\tset php version";
}
