#!/bin/sh

# TUTOS postinstall script
# required parameters: dbname, dbuser, dbpasswd, 
# admin_location, admin_passwd, admin_name, admin_city
#
#
# 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

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

edit_conf_file()
{ 
	application_url="${proto}://${domain_name}/${install_prefix}"
	admin_loc="${admin_location}\/${admin_city}"

	regexp0="s|_application_url_|${application_url}|g"
	regexp1="s/_admin_location_/${admin_loc}/g"
	regexp2="s/_dbname_/${dbname}/g"
	regexp3="s/_dbuser_/${dbuser}/g"
	regexp4="s/_dbpasswd_/${dbpasswd}/g"
  
  sed -e "${regexp0}" -e "${regexp1}" -e "${regexp2}" -e "${regexp3}" -e "${regexp4}"  $1 > $1.copy  
  mv -f $1.copy $1 
};

edit_sql_file()
{ 
  admin_loc="${admin_location}\/${admin_city}"
  regexp1="s/_admin_location_/${admin_loc}/g"
  regexp2="s/_admin_name_/${admin_name}/g"
  regexp3="s/_dbname_/${dbname}/g"
  regexp4="s/_dbuser_/${dbuser}/g"
  regexp5="s/_dbpasswd_/${dbpasswd}/g"
  regexp6="s/_admin_passwd_/${admin_passwd}/g"
  
  sed -e "${regexp1}" -e "${regexp2}" -e "${regexp3}" -e "${regexp4}" -e "${regexp5}" -e "${regexp6}" $1 > $1.copy  
  mv -f $1.copy $1 
};


parse_standard_parameters()
{
	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		proto="https"
		documents_directory="httpsdocs"
	else
		proto="http"
		documents_directory="httpdocs"
	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
}



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

read_conf
check_standard_parameters
parse_standard_parameters

cd ${vhost_path}/${documents_directory}/${install_prefix}

tutos_config=${vhost_path}/${documents_directory}/${install_prefix}/php/config.pinc
edit_conf_file ${tutos_config}

tutos_sql=${vhost_path}/${documents_directory}/${install_prefix}/php/sql/mysql.sql
edit_sql_file ${tutos_sql}


${MYSQL_BIN_D}/mysql -u${dbuser} -p${dbpasswd} ${dbname} < ${vhost_path}/${documents_directory}/${install_prefix}/php/sql/mysql.sql
rm -f ${tutos_sql}
#set write permissions for session path

mkdir ${vhost_path}/${documents_directory}/${install_prefix}/repository
mkdir ${vhost_path}/${documents_directory}/${install_prefix}/tmp

chmod 777 tmp
chmod 777 repository

#set register_globals to ON for this vhost
regexp="s/php_flag register_globals on//g"
sed -e "${regexp}" ${vhost_path}/${documents_directory}/.htaccess > ${vhost_path}/${documents_directory}/.htaccess.copy
echo "php_flag register_globals on" >> ${vhost_path}/${documents_directory}/.htaccess.copy
mv -f ${vhost_path}/${documents_directory}/.htaccess.copy ${vhost_path}/${documents_directory}/.htaccess

exit 0
