#!/bin/sh

# nuke postinstall 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 nuke:
# nuke_dbuser
# nuke_dbpasswd
# nuke_dbname

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 \
			nuke_dbuser nuke_dbpasswd nuke_dbname \
			nuke_admin_email nuke_admin_login nuke_admin_passwd ; do 
		check_parameter "$pname"
	done

}

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

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
}

edit_conf()
{
	regexp0="s|\('dbuname'] = '\).*|\1${nuke_dbuser}';|g"
	regexp1="s|\('dbpass'] = '\).*|\1${nuke_dbpasswd}';|g"
	regexp2="s|\('dbname'] = '\).*|\1${nuke_dbname}';|g"
sed -e "${regexp0}" -e "${regexp1}" -e "${regexp2}" $1 >$1.copy
mv -f $1.copy $1
	sed -e "s|@@DB_UNAME@@|${nuke_dbuser}|g" \
		  -e "s|@@DB_PASSWD@@|${nuke_dbpasswd}|g" ${2} >$2.copy
mv -f $2.copy $2

}

edit_mysql_file()
{
	regexp1="s/@@ADMIN_EMAIL@@/${nuke_admin_email}/g"
	regexp2="s/@@ADMIN_LOGIN@@/${nuke_admin_login}/g"
	regexp3="s/@@ADMIN_NAME@@/${nuke_admin_login}/g"
	regexp4="s/@@DOMAIN_NAME@@/${domain_name}/g"
	regexp5="s/@@ADMIN_PASSWORD@@/${nuke_admin_passwd}/g"
	sed -e "$regexp1" -e "$regexp2" -e "$regexp3" \
	-e "$regexp4" -e "$regexp5" "${mysql_file}" > "${mysql_file}.copy"
	mv "${mysql_file}.copy" "${mysql_file}"
};

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

check_params
parse_standard_parameters

cd ${vhost_path}

mv -f ${vhost_path}/${documents_directory}/${install_prefix}/html/* ${vhost_path}/${documents_directory}/${install_prefix}
rm -rf ${vhost_path}/${documents_directory}/${install_prefix}/html

nuke_root_d=${vhost_path}/${documents_directory}/${install_prefix}
nuke_config_file=${nuke_root_d}/includes/pnAPI.php
nuke_cfg="${nuke_root_d}/config.php"
prod_conf_t=/etc/psa/psa.conf

# generate nuke primary config file
read_conf

#edit_api_file ${nuke_config_file}
edit_conf ${nuke_cfg} ${nuke_config_file}

mysql_file=${nuke_root_d}/phoenix-sql/Phoenix-0.7.5.0.sql
# form nuke database
edit_mysql_file 

${MYSQL_BIN_D}/mysql -u${nuke_dbuser} -p${nuke_dbpasswd} ${nuke_dbname} < ${mysql_file}

#mv -f ${nuke_root_d}/html/* ${nuke_root_d}
rm -rf ${nuke_root_d}/install
rm -f ${nuke_root_d}/install.php
rm -rf ${nuke_root_d}/phoenix-sql
rm -rf ${nuke_root_d}/sql/
chmod -R 777 "${nuke_root_d}/pnTemp"

# move away installation directory
mv -f ${nuke_root_d}/install ${nuke_root_d}/install.bak
mv -f ${nuke_root_d}/contrib ${nuke_root_d}/contrib.bak

exit 0
