#!/bin/sh

# phpbb 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 phpbb:
# phpbb_dbuser
# phpbb_dbpasswd
# phpbb_dbname
# phpbb_admin_login
# phpbb_admin_passwd

check_standard_parameters()
{
	scrname="`basename "$0"`"
	if [ "X${vhost_path}" = "X" ]; then
		echo "$scrname: no vhost_path parameter specified for application"
		exit 1
	fi
	if [ "X${domain_name}" = "X" ]; then
		echo "$scrname: no domain_name parameter specified for application"
		exit 1
	fi
	if [ "X${install_prefix}" = "X" ]; then
		echo "$scrname: no install_prefix parameter specified for application"
		exit 1
	fi
	if [ "X${ssl_target_directory}" = "X" ]; then
		echo "$scrname: no ssl_target_directory parameter specified for application"
		exit 1
	fi
};

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

read_conf()
{	
	while read var val; do
		case "$var" in
			[A-Z]*) eval "$var"='"$val"';;
		esac; 
	done </etc/psa/psa.conf
}

backup_config()
{
	if [ ! -f "$1.orig" ]; then
		# backup original config file
		mv "$1" "$1.orig"
	fi
};

generate_config()
{
	backup_config $1
	echo -n "<?php \
\$dbms = 'mysql'; \
\$dbhost = 'localhost'; \
\$dbname = '${phpbb_dbname}'; \
\$dbuser = '${phpbb_dbuser}'; \
\$dbpasswd = '${phpbb_dbpasswd}'; \
\$table_prefix = 'phpbb_'; \
define('PHPBB_INSTALLED', true); \
?>" >> ${phpbb_config_file}
};

edit_mysql_file()
{
	regexp1="s/@@USERNAME@@/${phpbb_admin_login}/g"
	regexp2="s/@@PASSWORD@@/${phpbb_admin_passwd}/g"
	regexp3="s/@@EMAIL@@/${phpbb_admin_email}/g"
	regexp4="s/@@DOMAIN_NAME@@/${domain_name}/g"
	regexp5="s|@@INSTALL_PREFIX@@|/${install_prefix}/|g"
	regexp6="s|@@EMAIL@@|${phpbb_admin_email}|g"
	sed -e "${regexp1}" -e "${regexp2}" -e "${regexp3}" -e "${regexp4}" -e "${regexp5}" -e "${regexp6}" ${mysql_basic_file} > ${mysql_basic_file}.copy
};

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_standard_parameters
parse_standard_parameters

phpbb_root_d=${vhost_path}/${documents_directory}/${install_prefix}
phpbb_config_file=${phpbb_root_d}/config.php
prod_conf_t=/etc/psa/psa.conf

read_conf

case ${phpbb_site_desc} in 
	[A-Za-z\']*) 
	echo "$phpbb_site_desc" > "${phpbb_root_d}/tmp"
	sed -e  "s|'|\\\'|g" "${phpbb_root_d}/tmp" >"${phpbb_root_d}/tmp.copy"
	mv -f "${phpbb_root_d}/tmp.copy" "${phpbb_root_d}/tmp"
	phpbb_site_desc=`cat "${phpbb_root_d}/tmp"`
	rm -f 	"${phpbb_root_d}/tmp"
esac

generate_config ${phpbb_config_file}

${MYSQL_BIN_D}/mysql -u"$phpbb_dbuser" -p"$phpbb_dbpasswd" "$phpbb_dbname" <<EOF
update phpbb_config set config_value='$domain_name' where config_name='server_name';
update phpbb_config set config_value='$domain_name' where config_name='sitename';

update phpbb_config set config_value='$phpbb_site_desc' where config_name='site_desc';

update phpbb_config set config_value='/$install_prefix/' where config_name='script_path';

update phpbb_config set config_value='$phpbb_admin_email' where config_name='board_email';


update phpbb_users set username='$phpbb_admin_login', user_password = MD5('$phpbb_admin_passwd'), user_email='$phpbb_admin_email'  where user_id=2;
EOF

true
