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

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'@@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'" \

}


edit_config()
{
	regexp0="s|\(Config_user = \).*|\1\"${mambo_dbuser}\";|g"
	regexp1="s|\(Config_password =\).*|\1\"${mambo_dbpasswd}\";|g"
	regexp2="s|\(Config_db = \).*|\1\"${mambo_dbname}\";|g"
	regexp3="s|\(Config_absolute_path = \).*|\1\"${mambo_path}\";|g"
	regexp4="s|\(Config_live_site = \).*|\1\"${mambo_site}\";|g"
	regexp5="s|\(Config_cachepath = \).*|\1\"${mambo_path}/cache\";|g"
	regexp6="s|\(Config_mailfrom = \).*|\1\"${mambo_admin_email}\";|g"


	sed -e "${regexp0}" -e "${regexp1}" -e "${regexp2}" -e "${regexp3}" -e "${regexp4}"  -e "${regexp5}" -e "${regexp6}" ${conf_file}>${conf_file}.copy


	mv -f ${conf_file}.copy ${mambo_path}/configuration.php

}


mk_perm()
{
	chmod -R 777 images media components language modules cache mambots \
		templates administrator/backups administrator/components \
		administrator/modules administrator/templates
	chmod 0777 ${mambo_path}/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

read_conf
check_all_parameters
parse_standard_parameters


mambo_path="$vhost_path/$documents_directory/$install_prefix"
mambo_site="$proto://$domain_name/$install_prefix"
conf_file=${mambo_path}/configuration.php-dist
cd "$mambo_path"

edit_config 
mk_perm

cd "$mambo_path/installation/sql"
sed_cmd <joomla.sql >joomla.sql.new && mv joomla.sql.new joomla.sql
${MYSQL_BIN_D}/mysql -u "$mambo_dbuser" -p"$mambo_dbpasswd" "$mambo_dbname" <joomla.sql

if [ $? -ne 0 ]; then
  echo "Error while installing tables"
  exit 1
fi

cd "$mambo_path"
rm -fR installation

exit 0
