#!/bin/sh


read_params()
{

var=`cat | awk '{
	eqpos=index($0, "=");
	if (eqpos>1) {
		var=substr($0, 1, eqpos-1);
		val=substr($0, eqpos+1);

		tmp="[\x5c\x5c]";
		tmp2="\x5c\x5c\x5c\x5c";
		gsub(tmp,tmp2,val);


		tmp2="\x5c\x5c\x5c\x22";
		gsub("\"",tmp2,val);
		print var "=\"" val "\"";
	};
}'`


eval $var

# now we have full set of parameters, stored in variables
# readconf
	while read var val; do
		case "$var" in
			[A-Z]*) eval "$var"='"$val"';;
		esac; 
	done </etc/psa/psa.conf

	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		documents_directory="httpsdocs"
		proto="https"
	else
		documents_directory="httpdocs"
		proto="http"
	fi
};

check_req()
{

mkdir -p "${vhost_path}/${documents_directory}/${install_prefix}"

#try to detect whether ImageMagick or GD installed

	if [ -x /usr/bin/convert ];then
		# found Image Magick installed
		exit 0
	else
		tmp_file="${vhost_path}/${documents_directory}/${install_prefix}/php_PRE.php"
		echo "<? \
if (function_exists('gd_info')) \
{ \$gd=gd_info();\
echo \$gd['GD Version']; \
}?>" > ${tmp_file}

		php -q ${tmp_file} > ${tmp_file}.copy
		read gd_version < ${tmp_file}.copy
		rm -f ${tmp_file} ${tmp_file}.copy
	
		if [ "X$gd_version" != "X" ]; then
			# found GD installed
			exit 0
		else
			echo "You must install PHP GD library or ImageMagick"
			exit 1
		fi
	fi
}

read_params
check_req

exit 0