#!/bin/sh
# mambo postinstall/reconfigure script

# here is also some standard parameters, that must be specified:
# vhost_path - full path to vhost root directory
# domain_name - name of domain
# install_prefix - path of application inside vhost directory
# ssl_target_directory - true, if application is in httpsdocs

# list of parameters for mambo:
# mambo_dbuser
# mambo_dbpasswd
# mambo_dbname
# mambo_admin_email
# mambo_admin_login
# mambo_admin_passwd
# mambo_editor_email
# mambo_editor_login
# mambo_editor_passwd

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_all_parameters()
{
	for pname in domain_name install_prefix ssl_target_directory \
			mambo_dbuser mambo_dbpasswd mambo_dbname \
			mambo_admin_email mambo_admin_login mambo_admin_passwd \
			mambo_editor_email mambo_editor_login mambo_editor_passwd; do
		check_parameter "$pname"
	done

}

parse_standard_parameters()
{
	if test "$ssl_target_directory" = true; then
		proto="https"
		documents_directory="httpsdocs"
	else
		proto="http"
		documents_directory="httpdocs"
	fi
}

sed_cmd()
{
	sed -e "s'@@MAMBO_PATH@@'$mambo_path'" \
		-e "s'@@MAMBO_SITE@@'$mambo_site'" \
		-e "s'@@DB_USERNAME@@'$mambo_dbuser'" \
		-e "s'@@DB_PASSWORD@@'$mambo_dbpasswd'" \
		-e "s'@@DB_NAME@@'$mambo_dbname'" \
		-e "s'@@ADMIN_LOGIN@@'$mambo_admin_login'" \
		-e "s'@@ADMIN_EMAIL@@'$mambo_admin_email'" \
		-e "s'@@ADMIN_PASSWORD@@'$mambo_admin_passwd'" \
		-e "s'@@EDITOR_LOGIN@@'$mambo_editor_login'" \
		-e "s'@@EDITOR_EMAIL@@'$mambo_editor_email'" \
		-e "s'@@EDITOR_PASSWORD@@'$mambo_editor_passwd'" \
		-e "s'@@VHOST_PATH@@'$vhost_path'" \
		-e "s'@@VHOST_DOCS@@'$vhost_path/$documents_directory'" \
		-e "s'@@PUBLISH_SCRIPT@@'$proto://$domain_name/cgi-bin/$install_prefix/publish'"
}

mk_main_config()
{
	sed_cmd <configuration.php-dist >configuration.php
	sed_cmd <publish-dist >publish
	sed_cmd <redirect_index.html-dist >redirect_index.html
	test -f administrator/index2.php-dist|| cp administrator/index2.php administrator/index2.php-dist
	sed_cmd <administrator/index2.php-dist >administrator/index2.php
}

mk_perm()
{
	chmod -R 707 images media uploadfiles components language modules \
		templates administrator/backups administrator/components
	chmod 0777 configuration.php
}

read_conf()
{	
	if test -r /etc/psa/psa.conf; then
		while read var val; do
			case "$var" in
				[A-Z]*) eval "$var"='"$val"';;
			esac; 
		done </etc/psa/psa.conf
	else
		echo /etc/psa/psa.conf not found
		exit 1
	fi
}

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

check_all_parameters
parse_standard_parameters

mambo_path="$vhost_path/$documents_directory/$install_prefix"
mambo_site="$proto://$domain_name/$install_prefix"


cd "$mambo_path"
mk_main_config

cd "$mambo_path"
mk_perm

install -m 755 "$mambo_path/publish" "$vhost_path/cgi-bin/$install_prefix"
