#!/bin/sh

if [ "X${PLESK_INSTALLER_DEBUG}" != "X" ]; then
	set -x
fi

extension_dir="%LIBDIR%/php/modules"
php_d="/etc/php.d"
php_ini="/etc/php.ini"

detect_php_version()
{
	[ -z "$php_version" ] || return 0
	cat > /tmp/psa-ioncube-php-version-detect.php <<EOF
<?php
\$v = explode(".",PHP_VERSION); echo "\$v[0].\$v[1]";
EOF
	php_version=`php -n -f /tmp/psa-ioncube-php-version-detect.php 2>/dev/null | awk '/^[0-9.]+$/'`
	rm -f /tmp/psa-ioncube-php-version-detect.php
	[ -n "$php_version" ] || return 1
	return 0
}

remove_own_configs()
{
	rm -f $php_d/ioncube-loader* 2>/dev/null		# legacy config name
	rm -f $php_d/00-ioncube-loader* 2>/dev/null
}

filter_out_zend_extensions()
{
	[ -d "$php_d" ] || return 1

	local ext_conf="zend_extensions_psa.ini"
	echo -n "Moving Zend extensions from php.ini to conf.d/$ext_conf and disabling custom ionCube if any... "

	perl -n -i.bak -e '
		use strict;
		use warnings;

		sub canonize {					# strip comments and excessive spaces
			my $line = shift;
			$line =~ s|\s+| |g;
			$line =~ s|;.*$||;
			$line =~ s|^\s+||;
			$line =~ s|\s+$||;
			$line =~ s|^(\S*)\s*=\s*(.*)$|$1 = $2|;
			return $line;
		}

		BEGIN {
			our @moved;
			open(our $MOVED, "+>>", "'"$php_d/$ext_conf"'") or die $!;
			seek($MOVED, 0, 0);
			while (<$MOVED>) {			# populate @moved with canonized (zend_)?extension* lines from .ini file
				my $cline = canonize($_);
				next unless $cline;		# skip empty and comment lines
				push(@moved, $cline);
			}
			seek($MOVED, 0, 2);
			if (tell($MOVED) == 0) {	# file was just created - print header
				print $MOVED "; Following directives were moved from php.ini files by Plesk to avoid load order errors\n";
				print $MOVED "; Please do not add any zend_extension* or extension directives directly into php.ini\n";
			}
		}

		use vars qw(@moved $MOVED);
		print and next unless /^\s*(zend_)?extension(_debug)?(_ts)?\s*=/;	# do not modify anything other than (zend_)?extension* lines
		my $line = $_;
		my $cline = canonize($_);
		s|(.*)|;$1\t; moved to conf.d/'"$ext_conf"' by Plesk|; print;		# comment out (zend_)?extension* lines
		next if $line =~ /^\s*(zend_)?extension(_debug)?(_ts)?\s*=\s*.*ioncube_loader.*\.so/;	# skip moving custom ionCube loader lines
		print $MOVED $line unless (grep {$_ eq $cline} @moved);				# move allowed lines to separate .ini file unless they are already there
		push(@moved, $cline);
	' $php_ini < /dev/null || {
		echo "failed"
		echo -n "Restoring php.ini files... "

		for conf in $php_ini; do
			[ -f "$conf.bak" ] && mv -f "$conf.bak" "$conf"
		done

		echo "done"
		echo "Warning: Your PHP configuration may now contain duplicate zend_extension* directives."
		echo "         You may wish to fix it if 'php -v' outputs relevant warnings."

		return 1
	}

	echo -n "Removing php.ini backup files... "

	for conf in $php_ini; do
		[ -f "$conf" ] && rm -f "$conf.bak"
	done

	echo "done"
}

install_config()
{
	[ -d "$php_d" ] || return 0

	echo -n "Trying to register the ionCube loader... "

	detect_php_version || {
		echo "Unable to detect php version. skipping"
		return
	}

	# loader should be declared first in PHP configuration, hence the config name
	ion_config="00-ioncube-loader.ini"

	remove_own_configs

	if [ ! -f "$extension_dir/ioncube_loader_lin_${php_version}.so" ]; then
		echo "No ionCube loader found for php ${php_version}. skipping"
		return
	fi

	echo '; comment out next line to disable ionCube Zend extension in php'   >  $php_d/$ion_config
	echo "zend_extension=$extension_dir/ioncube_loader_lin_${php_version}.so" >> $php_d/$ion_config

	echo "done"
}

if [ "$1" = "install" ]; then
	filter_out_zend_extensions
	install_config
elif [ "$1" = "remove" ]; then
	remove_own_configs
else
	echo "Usage: $0 (install | remove)" >&2
fi
