#!/bin/sh

# oscommerce 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 oscommerce:
# oscommerce_dbuser
# oscommerce_dbpasswd
# oscommerce_dbname
# oscommerce_admin_login
# oscommerce_admin_passwd

check_standard_parameters()
{
	if [ "X${vhost_path}" = "X" ]; then
		echo "reconfigure: no vhost_path parameter specified for application"
		exit 1
	fi
	if [ "X${domain_name}" = "X" ]; then
		echo "reconfigure: no domain_name parameter specified for application"
		exit 1
	fi
	if [ "X${install_prefix}" = "X" ]; then
		echo "reconfigure: no install_prefix parameter specified for application"
		exit 1
	fi
	if [ "X${ssl_target_directory}" = "X" ]; then
		echo "reconfigure: 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()
{	
	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
}

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

generate_user_config()
{
	backup_config $1

	http_server="${proto}://${domain_name}"

	echo "<?php" > $1
	echo "// Please, note that all changes in this file will be lost" >> $1
	echo "// after reconfiguring application by Plesk" >> $1
	echo "	define('HTTP_SERVER', '${http_server}');" >> $1
	echo "	define('HTTPS_SERVER', '${http_server}');" >> $1
	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		echo "	define('ENABLE_SSL', 'true');" >> $1
	else
		echo "	define('ENABLE_SSL', 'false');" >> $1
	fi
	echo "	define('HTTP_COOKIE_DOMAIN', '${domain_name}');" >> $1
	echo "	define('HTTPS_COOKIE_DOMAIN', '${domain_name}');" >> $1
	echo "	define('HTTP_COOKIE_PATH', '/${install_prefix}/');" >> $1
	echo "	define('DIR_WS_HTTP_CATALOG', '/${install_prefix}/');" >> $1
	echo "	define('DIR_WS_HTTPS_CATALOG', '/${install_prefix}/');" >> $1
	echo "	define('DIR_WS_IMAGES', 'images/');" >> $1
	echo "	define('DIR_WS_ICONS', DIR_WS_IMAGES . 'icons/');" >> $1
	echo "	define('DIR_WS_INCLUDES', 'includes/');" >> $1
	echo "	define('DIR_WS_BOXES', DIR_WS_INCLUDES . 'boxes/');" >> $1
	echo "	define('DIR_WS_FUNCTIONS', DIR_WS_INCLUDES . 'functions/');" >> $1
	echo "	define('DIR_WS_CLASSES', DIR_WS_INCLUDES . 'classes/');" >> $1
	echo "	define('DIR_WS_MODULES', DIR_WS_INCLUDES . 'modules/');" >> $1
	echo "	define('DIR_WS_LANGUAGES', DIR_WS_INCLUDES . 'languages/');" >> $1
	echo "" >> $1
	echo "	define('DIR_WS_DOWNLOAD_PUBLIC', 'pub/');" >> $1
	echo "	define('DIR_FS_CATALOG', dirname(\$HTTP_SERVER_VARS['SCRIPT_FILENAME']));" >> $1
	echo "	define('DIR_FS_DOWNLOAD', DIR_FS_CATALOG . 'download/');" >> $1
	echo "	define('DIR_FS_DOWNLOAD_PUBLIC', DIR_FS_CATALOG . 'pub/');" >> $1
	echo "" >> $1
	echo "// define our database connection" >> $1
	echo "	" >> $1
	echo "	define('DB_SERVER', 'localhost');" >> $1
	echo "	define('DB_SERVER_USERNAME', '${oscommerce_dbuser}');" >> $1
	echo "	define('DB_SERVER_PASSWORD', '${oscommerce_dbpasswd}');" >> $1
	echo "	define('DB_DATABASE', '${oscommerce_dbname}');" >> $1
	echo "	define('USE_PCONNECT', 'false');" >> $1
	echo "	define('STORE_SESSIONS', '');" >> $1
	echo "?>" >> $1
};

generate_admin_config()
{
	backup_config $1

	http_server="${proto}://${domain_name}"

	echo "<?php" > $1
	echo "// Please, note that all changes in this file will be lost" >> $1
	echo "// after reconfiguring application by Plesk" >> $1
	echo "	define('HTTP_SERVER', '${http_server}');" >> $1
	echo "	define('HTTP_CATALOG_SERVER', '${http_server}');" >> $1
	echo "	define('HTTPS_CATALOG_SERVER', '${http_server}');" >> $1
	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		echo "	define('ENABLE_SSL_CATALOG', 'true');" >> $1
	else
		echo "	define('ENABLE_SSL_CATALOG', 'false');" >> $1
	fi
	echo "	define('DIR_FS_DOCUMENT_ROOT', '${vhost_path}/${documents_directory}');" >> $1
	echo "	define('DIR_WS_ADMIN', '/${install_prefix}/admin/');" >> $1
	echo "	define('DIR_FS_ADMIN', '${oscommerce_root_d}/admin');" >> $1
	echo "	define('DIR_WS_CATALOG', '/${install_prefix}/');" >> $1
	echo "	define('DIR_FS_CATALOG', '${oscommerce_root_d}/');" >> $1
	echo "	define('DIR_WS_IMAGES', 'images/');" >> $1
	echo "	define('DIR_WS_ICONS', DIR_WS_IMAGES . 'icons/');" >> $1
	echo "	define('DIR_WS_CATALOG_IMAGES', DIR_WS_CATALOG . 'images/');" >> $1
	echo "	define('DIR_WS_INCLUDES', 'includes/');" >> $1
	echo "	define('DIR_WS_BOXES', DIR_WS_INCLUDES . 'boxes/');" >> $1
	echo "	define('DIR_WS_FUNCTIONS', DIR_WS_INCLUDES . 'functions/');" >> $1
	echo "	define('DIR_WS_CLASSES', DIR_WS_INCLUDES . 'classes/');" >> $1
	echo "	define('DIR_WS_MODULES', DIR_WS_INCLUDES . 'modules/');" >> $1
	echo "	define('DIR_WS_LANGUAGES', DIR_WS_INCLUDES . 'languages/');" >> $1
	echo "	define('DIR_WS_CATALOG_LANGUAGES', DIR_WS_CATALOG . 'includes/languages/');" >> $1
	echo "	define('DIR_FS_CATALOG_LANGUAGES', DIR_FS_CATALOG . 'includes/languages/');" >> $1
	echo "	define('DIR_FS_CATALOG_IMAGES', DIR_FS_CATALOG . 'images/');" >> $1
	echo "	define('DIR_FS_CATALOG_MODULES', DIR_FS_CATALOG . 'includes/modules/');" >> $1
	echo "	define('DIR_FS_BACKUP', DIR_FS_ADMIN . 'backups/');" >> $1
	echo "" >> $1
	echo "// define our database connection" >> $1
	echo "	" >> $1
	echo "	define('DB_SERVER', 'localhost');" >> $1
	echo "	define('DB_SERVER_USERNAME', '${oscommerce_dbuser}');" >> $1
	echo "	define('DB_SERVER_PASSWORD', '${oscommerce_dbpasswd}');" >> $1
	echo "	define('DB_DATABASE', '${oscommerce_dbname}');" >> $1
	echo "	define('USE_PCONNECT', 'false');" >> $1
	echo "	define('STORE_SESSIONS', '');" >> $1
	echo "?>" >> $1
};

form_admin_htaccess()
{
	local htpasswd_file htpasswd_bin

	if [ ! -f "$1" ]; then
		echo "Unable to find $1 to setup proper access to administrator page"
		exit 1
	fi

	for htpasswd_bin in "$HTTPD_BIN_D/htpasswd2" "$HTTPD_BIN_D/htpasswd" "/usr/sbin/htpasswd2" "/usr/sbin/htpasswd"; do
		if test -x "$htpasswd_bin"; then break; fi
	done
	
	htpasswd_file=${oscommerce_root_d}/admin/.htpasswd

	if [ ! -f "$1.orig" ]; then
		# forming is required for first time only
		cp -f $1 $1.orig

		# check, that directive already present
		grep AuthUserFile $1 > /dev/null 2>&1
		
		if [ $? -ne 0 ]; then
			echo "AuthUserFile ${htpasswd_file}" >> $1
			echo "AuthName Secure" >> $1
			echo "AuthType basic" >> $1
			echo "require valid-user" >> $1
			echo "" >> $1
		fi
	fi 
	# we need it to make apache work
	chmod 644 ${htpasswd_file}

	if [ ! -f ${htpasswd_file} ]; then
		"$htpasswd_bin" -cb ${htpasswd_file} ${oscommerce_admin_login} ${oscommerce_admin_passwd}
	else
		"$htpasswd_bin" -b ${htpasswd_file} ${oscommerce_admin_login} ${oscommerce_admin_passwd}
	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

check_standard_parameters
parse_standard_parameters

cd "$vhost_path/$documents_directory/$install_prefix"

oscommerce_root_d=${vhost_path}/${documents_directory}/${install_prefix}
oscommerce_config_file1=${oscommerce_root_d}/includes/configure.php
oscommerce_config_file2=${oscommerce_root_d}/admin/includes/configure.php
admin_htaccess_file=${oscommerce_root_d}/admin/.htaccess
prod_conf_t=/etc/psa/psa.conf

# generate oscommerce primary config file
read_conf
form_admin_htaccess ${admin_htaccess_file}


generate_user_config ${oscommerce_config_file1}
generate_admin_config ${oscommerce_config_file2}

# enable writing to images file to group psaserv
chmod -R o+w ${oscommerce_root_d}/images

exit 0
