#!/bin/sh

# gallery reconfigure script
# required parameters: 
# admin_fullname, admin_login, admin_passwd,
# admin_email, gallery_name, locale
#
# there are 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

read_parameters()
{
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

}

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 \
			admin_login admin_passwd admin_email admin_fullname \
			gallery_name locale ; do 
		check_parameter "$pname"
	done

}

parse_params()
{
	if [ "X${ssl_target_directory}" = "Xtrue" ]; then
		proto="https"
		documents_directory="httpsdocs"
	else
		# don't forget to fix it
		proto="http"
		documents_directory="httpdocs"
	fi
};

backup_original_config()
{
	if [ -f "${gallery_config}" ] ; then
		if [ ! -f "${gallery_config}.orig" ] ; then
			cp ${gallery_config} ${gallery_config}.orig
		fi
	fi
}

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

generate_gallery_config()
{
	admin_email_size=`echo ${admin_email} | awk '{ print length($0) }'`
	admin_fullname_size=`echo ${admin_fullname} | awk '{ print length($0) }'`
	admin_login_size=`echo ${admin_login} | awk '{ print length($0) }'`
	rm -f ${gallery_path}/albums/.users/1069858368_1755202938
	echo 'O:12:"gallery_user":11:{s:7:"isAdmin";b:1;s:15:"canCreateAlbums";b:1;s:3:"uid";s:21:"1069858368_1755202938";s:15:"defaultLanguage";s:5:"'${locale}'";s:7:"version";i:5;s:8:"username";s:'${admin_login_size}':"'${admin_login}'";s:8:"password";s:36:"XXXX'${admin_hash}'";s:8:"fullname";s:'${admin_fullname_size}':"'${admin_fullname}'";s:5:"email";s:'${admin_email_size}':"'${admin_email}'";s:10:"lastAction";s:5:"login";s:14:"lastActionDate";i:1069884720;}' >> "${gallery_path}/albums/.users/1069858368_1755202938"
	chmod 0777 "${gallery_path}/albums/.users/1069858368_1755202938"

#configure
	regexp0="s|\(galleryTitle = \"\).*\"|\1${gallery_name}\"|g"
	regexp1="s|\(default_language = \"\).*\"|\1${locale}\"|g"

	sed -e "${regexp0}" -e "${regexp1}" "${gallery_config}" >"${gallery_config}".copy
	mv -f "${gallery_config}".copy "${gallery_config}"
	
chmod 0644 ${gallery_config}
}

read_parameters
read_conf

# now we have full set of parameters, stored in variables

check_params
parse_params

gallery_path=${vhost_path}/${documents_directory}/${install_prefix}
gallery_config=${vhost_path}/${documents_directory}/${install_prefix}/config.php

backup_original_config
#create_dirs
generate_gallery_config
#cleanup

exit 0
