#!/bin/sh

# phpbugtracker postinstall script
# required parameters: dbname, dbuser, dbpasswd, 
# admin_email, admin_passwd
#
#
# 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()
{
	regexp1="s!smarty_path!./inc/smarty/!g"
  	regexp2="s/http:\/\/localhost\//${install_url}/g"
 	regexp3="s/db_name/${dbname}/g"
	regexp4="s/db_user/${dbuser}/g"
	regexp5="s/db_pass/${dbpasswd}/g"
	regexp6="s/admin_email/${admin_email}/g"
  
  sed -e "${regexp1}" -e "${regexp2}" -e "${regexp3}" -e "${regexp4}" -e "${regexp5}" -e "${regexp6}" $1 > $1.copy
  mv -f $1.copy $1 
#  echo "include INSTALLPATH.'/languages/en.php'; ?>" >> $1
};


TBL_PREFIX=''
DB_TYPE='mysql'

generate_sql() {
  cat $1 | sed "
	s/TBL_ACTIVE_SESSIONS/${TBL_PREFIX}active_sessions/g
	s/TBL_DB_SEQUENCE/${TBL_PREFIX}db_sequence/g
	s/TBL_ATTACHMENT/${TBL_PREFIX}attachment/g
	s/TBL_AUTH_GROUP/${TBL_PREFIX}auth_group/g
	s/TBL_AUTH_PERM/${TBL_PREFIX}auth_perm/g
	s/TBL_AUTH_USER/${TBL_PREFIX}auth_user/g
	s/TBL_BUG /${TBL_PREFIX}bug/g
	s/TBL_BUG_CC /${TBL_PREFIX}bug_cc/g
	s/TBL_BUG_DEPENDENCY / ${TBL_PREFIX}bug_dependency/g
	s/TBL_BUG_GROUP /${TBL_PREFIX}bug_group/g
	s/TBL_BUG_HISTORY /${TBL_PREFIX}bug_history/g
	s/TBL_BUG_VOTE /${TBL_PREFIX}bug_vote/g
	s/TBL_COMMENT/${TBL_PREFIX}comment/g
	s/TBL_COMPONENT/${TBL_PREFIX}component/g
	s/TBL_CONFIGURATION/${TBL_PREFIX}configuration/g
	s/TBL_GROUP_PERM/${TBL_PREFIX}group_perm/g
	s/TBL_OS/${TBL_PREFIX}os/g
	s/TBL_PROJECT /${TBL_PREFIX}project/g
	s/TBL_RESOLUTION/${TBL_PREFIX}resolution/g
	s/TBL_SAVED_QUERY/${TBL_PREFIX}saved_query/g
	s/TBL_SEVERITY/${TBL_PREFIX}severity/g
	s/TBL_STATUS/${TBL_PREFIX}status/g
	s/TBL_USER_GROUP /${TBL_PREFIX}user_group/g
	s/TBL_USER_PERM /${TBL_PREFIX}user_perm/g
	s/TBL_USER_PREF /${TBL_PREFIX}user_pref/g
	s/TBL_VERSION/${TBL_PREFIX}version/g
	s/TBL_PROJECT_GROUP/${TBL_PREFIX}project_group/g
	s/TBL_DATABASE/${TBL_PREFIX}database_server/g
	s/TBL_SITE/${TBL_PREFIX}site/g
	s/OPTION_ADMIN_EMAIL/${admin_email}/g
	s/OPTION_ADMIN_PASS/${admin_passwd}/g
	s/OPTION_INSTALL_URL/${install_url}/g
	s/OPTION_PHPBT_EMAIL/${admin_email}/g
	s/OPTION_ENCRYPT_PASS/0/g

  " > $2
};

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

	install_url="$proto:\/\/${domain_name}\/${install_prefix}"
	install_path="${vhost_path}/${documents_directory}/${install_prefix}"

};

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}

pbug_config=${vhost_path}/${documents_directory}/${install_prefix}/config.php

edit_conf_file ${pbug_config}

#echo "php_flag register_globals on" >".htaccess"

generate_sql schemas/$DB_TYPE.in createdb.sql

${MYSQL_BIN_D}/mysql -u${dbuser} -p${dbpasswd} ${dbname} < ${vhost_path}/${documents_directory}/${install_prefix}/createdb.sql

rm -f createdb.sql
chmod 777 ${install_path}/attachments ${install_path}/c_templates

exit 0
