#!/bin/sh

# bbclone postinstall script
# required parameters: bbc_mainsite, bbc_show_config, bbc_titlebar,
#	bbc_language, bbc_maxtime, bbc_maxvisible,
#	bbc_maxos, bbc_maxbrowser, max_extension, bbc_maxrobot, bbc_maxpage,
#	bbc_maxorigin, bbc_ignoreip, bbc_ignore_refer, bbc_own_refer,
#	bbc_stat_field_id, bbc_stat_field_time, bbc_stat_field_visits,
#	bbc_stat_field_ext, bbc_stat_field_dns, bbc_stat_field_refer,
#	bbc_stat_field_os, bbc_stat_field_browser, bbc_general_align_style,
#	bbc_title_size, bbc_subtitle_size, bbc_text_size

# 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
};

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

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

generate_bbclone_config()
{
	echo "<?php" > ${bbcounter_config}

	echo "\$BBC_MAINSITE = \"${proto}://${bbc_mainsite}\";" >> ${bbcounter_config}

	if [ "X${bbc_show_config}" != "X" ]; then
		echo "\$BBC_SHOW_CONFIG = ${bbc_show_config};" >> ${bbcounter_config}
	else
		echo "\$BBC_SHOW_CONFIG = 1;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_titlebar}" != "X" ]; then
		echo "\$BBC_TITLEBAR = \"${bbc_titlebar}\";" >> ${bbcounter_config}
	else
		echo "\$BBC_TITLEBAR = \"Statistics for %SERVER generated the %DATE\";" >> ${bbcounter_config}
	fi

	if [ "X${bbc_language}" != "X" ]; then
		echo "\$BBC_LANGUAGE = \"${bbc_language}\";" >> ${bbcounter_config}
	else
		echo "\$BBC_LANGUAGE = \"en\";" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxtime}" != "X" ]; then
		echo "\$BBC_MAXTIME = ${bbc_maxtime};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXTIME = 1800;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxvisible}" != "X" ]; then
		echo "\$BBC_MAXVISIBLE = ${bbc_maxvisible};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXVISIBLE = 100;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxos}" != "X" ]; then
		echo "\$BBC_MAXOS = ${bbc_maxos};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXOS = 10;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxbrowser}" != "X" ]; then
		echo "\$BBC_MAXBROWSER = ${bbc_maxbrowser};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXBROWSER = 10;" >> ${bbcounter_config}
	fi

	if [ "X${max_extension}" != "X" ]; then
		echo "\$BBC_MAXEXTENSION = ${max_extension};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXEXTENSION = 10;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxrobot}" != "X" ]; then
		echo "\$BBC_MAXROBOT = ${bbc_maxrobot};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXROBOT = 10;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxpage}" != "X" ]; then
		echo "\$BBC_MAXPAGE = ${bbc_maxpage};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXPAGE = 10;" >> ${bbcounter_config}
	fi

	if [ "X${bbc_maxorigin}" != "X" ]; then
		echo "\$BBC_MAXORIGIN = ${bbc_maxorigin};" >> ${bbcounter_config}
	else
		echo "\$BBC_MAXORIGIN = 10;" >> ${bbcounter_config}
	fi

	echo "\$BBC_IGNOREIP = \"${bbc_ignoreip}\";" >> ${bbcounter_config}

	echo "\$BBC_IGNORE_REFER = \"${bbc_ignore_refer}\";" >> ${bbcounter_config}

	if [ "X${bbc_own_refer}" != "X" ]; then
		echo "\$BBC_OWN_REFER = ${bbc_own_refer};" >> ${bbcounter_config}
	else
		echo "\$BBC_OWN_REFER = 1;" >> ${bbcounter_config}
	fi

	bbc_detailed_stat_fields=""

	if [ "X${bbc_stat_field_id}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, id"
		else
			bbc_detailed_stat_fields="id"
		fi
	fi

	if [ "X${bbc_stat_field_time}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, time"
		else
			bbc_detailed_stat_fields="time"
		fi
	fi

	if [ "X${bbc_stat_field_visits}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, visits"
		else
			bbc_detailed_stat_fields="visits"
		fi
	fi

	if [ "X${bbc_stat_field_ext}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, ext"
		else
			bbc_detailed_stat_fields="ext"
		fi
	fi

	if [ "X${bbc_stat_field_ext}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, ext"
		else
			bbc_detailed_stat_fields="ext"
		fi
	fi

	if [ "X${bbc_stat_field_dns}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, dns"
		else
			bbc_detailed_stat_fields="dns"
		fi
	fi

	if [ "X${bbc_stat_field_refer}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, refer"
		else
			bbc_detailed_stat_fields="refer"
		fi
	fi

	if [ "X${bbc_stat_field_os}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, os"
		else
			bbc_detailed_stat_fields="os"
		fi
	fi

	if [ "X${bbc_stat_field_browser}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, browser"
		else
			bbc_detailed_stat_fields="browser"
		fi
	fi
	if [ "X${bbc_stat_field_ip}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, ip"
		else
			bbc_detailed_stat_fields="ip"
		fi
	fi
	if [ "X${bbc_stat_field_page}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, page"
		else
			bbc_detailed_stat_fields="page"
		fi
	fi
	if [ "X${bbc_stat_field_search}" != "X" ]; then
		if [ "X${bbc_detailed_stat_fields}" != "X" ]; then
			bbc_detailed_stat_fields="${bbc_detailed_stat_fields}, search"
		else
			bbc_detailed_stat_fields="search"
		fi
	fi

	echo "\$BBC_DETAILED_STAT_FIELDS = \"${bbc_detailed_stat_fields}\";" >> ${bbcounter_config}

	if [ "X${bbc_general_align_style}" != "X" ]; then
		echo "\$BBC_GENERAL_ALIGN_STYLE = \"${bbc_general_align_style}\";" >> ${bbcounter_config}
	else
		echo "\$BBC_GENERAL_ALIGN_STYLE = \"center\";" >> ${bbcounter_config}
	fi

	if [ "X${bbc_title_size}" != "X" ]; then
		echo "\$BBC_TITLE_SIZE = ${bbc_title_size};" >> ${bbcounter_config}
	else
		echo "\$BBC_TITLE_SIZE = \"12\";" >> ${bbcounter_config}
	fi

	if [ "X${bbc_subtitle_size}" != "X" ]; then
		echo "\$BBC_SUBTITLE_SIZE = \"${bbc_subtitle_size}\";" >> ${bbcounter_config}
	else
		echo "\$BBC_SUBTITLE_SIZE = \"8\";" >> ${bbcounter_config}
	fi

	if [ "X${bbc_subtitle_size}" != "X" ]; then
		echo "\$BBC_TEXT_SIZE = \"${bbc_text_size}\";" >> ${bbcounter_config}
	else
		echo "\$BBC_TEXT_SIZE = \"10\";" >> ${bbcounter_config}
	fi

    echo "?>" >> ${bbcounter_config}

};

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}

bbcounter_config=${vhost_path}/${documents_directory}/${install_prefix}/conf/config.php

chmod 0777 ${vhost_path}/${documents_directory}/${install_prefix}/var/
chmod 0666 ${vhost_path}/${documents_directory}/${install_prefix}/var/access.php
chmod 0666 ${vhost_path}/${documents_directory}/${install_prefix}/var/.htalock
chmod 0666 ${vhost_path}/${documents_directory}/${install_prefix}/var/last.php
chmod 0666 ${vhost_path}/${documents_directory}/${install_prefix}/var/counter*.inc

backup_original_config
generate_bbclone_config

exit 0
