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

};

check_parameter()
{
	local pname="$1"
	if eval "test -z \"\$$pname\"";then
		scrname="`basename "$0"`"
		echo "$scrname: no $pname parameter specified for application"
		exit 1
	fi
}

check_params()
{
	for pname in vhost_path domain_name install_prefix ssl_target_directory \
			dbuser dbpasswd dbname \
			site_name admin_name admin_passwd ; do 
		check_parameter "$pname"
	done

}

parse_params()
{
	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		documents_directory="httpsdocs"
		proto="https:"
	else
		documents_directory="httpdocs"
		proto="http:"
	fi
	root_d="${vhost_path}/${documents_directory}/${install_prefix}"

};

create_dirs()
{	
	if [ ! -d "${root_d}" ]; then
		mkdir "$root_d"
	fi

	if [ ! -d "${root_d}/typo3conf" ]; then
		mkdir "$root_d/typo3conf"
	fi

	if [ ! -d "${root_d}/typo3temp" ]; then
		mkdir "$root_d/typo3temp"
	fi
	chmod -R 777 "${root_d}/typo3conf" "${root_d}/typo3temp"
};


read_params
check_params
parse_params
create_dirs

exit 0
