#!/bin/sh 

MODE=$2
INSTALL_PREFIX=/usr/local

if [ "X${SB_INSTALLER_DEBUG}" = "X1" ]; then
    set -xv
fi

if [ "X$MODE" = "XPRE-INSTALL" ]; then
	# Migration on FreeBSD was broken in versions prior to 3.0.2
	if [ -e ${INSTALL_PREFIX}/sitebuilder/config ] && [ ! -e ${INSTALL_PREFIX}/sitebuilder/.version ]; then
		echo -n "3.X.X" > ${INSTALL_PREFIX}/sitebuilder/.version
	fi
	
	rm -f ${INSTALL_PREFIX}/sitebuilder/htdocs/.htaccess

	if [ -e ${INSTALL_PREFIX}/sitebuilder/.version ]; then
	        SB_OLD_VERSION=`cat ${INSTALL_PREFIX}/sitebuilder/.version`
        	rm -f ${INSTALL_PREFIX}/sitebuilder/.version
	        rm -f ${INSTALL_PREFIX}/sitebuilder/.build

		if [ -d ${INSTALL_PREFIX}/sitebuilder/.keys ]; then
		    mv ${INSTALL_PREFIX}/sitebuilder/.keys ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3
		fi
		# create migration flag
		echo -n "$SB_OLD_VERSION" > ${INSTALL_PREFIX}/sitebuilder/migrate
	else
	        VERSION=''
	fi
	
#	if [ -d ${RPM_INSTALL_PREFIX}/sitebuilder/.keys ]; then
#	    mv ${RPM_INSTALL_PREFIX}/sitebuilder/.keys ${RPM_INSTALL_PREFIX}/sitebuilder/.keys.2.1.3
#	fi


	# pre-install
	exit 0
fi 

_PATHS="/usr/bin/php5 /usr/bin/php /usr/local/bin/php  /usr/local/sitebuilder/bin/php /usr/local/sitebuilder/cgi-bin/php /usr/local/psa/apache/bin/php"

for php_path in $_PATHS; do
    if [ -x $php_path ]; then
        php_ver=`echo "<?php echo phpversion(); ?>"|$php_path -q |awk -F. '{print $1$2}'`
        if [ $php_ver = "51" ]; then break; else php_ver="0"; fi
	    
    fi
done
			

if [ $php_ver = "51" ]; then scan_path=`$php_path -i|grep "for additional"| sed 's/.*[>||[:space:]]\(\/[[:alnum:]]*\)/\1/'|awk '{print $1}'`;else php_ver="0"; fi
if [ $php_ver = "51" ]; then ini_path=`$php_path -i|grep "php.ini"|sed 's/.*[>||[:space:]]\(\/[[:alnum:]]*\)/\1/'`;else php_ver="0"; fi

ext_dir=`echo '<?php echo ini_get("extension_dir");?>'|$php_path -q`
if [ ! -d $ext_dir ]; then
   mkdir -p $ext_dir
fi
	

    if [ $php_ver != "0" ]; then
	test -f $ext_dir/sitebuilder3.so ||  ln -s ${INSTALL_PREFIX}/sitebuilder/lib/extensions/no-debug-non-zts-20050922/sitebuilder3.so $ext_dir/
	
	if [ -d $scan_path ]; then
	    echo "extension=sitebuilder3.so" > $scan_path/sitebuilder3.ini
	fi	    
    fi

    IS_EXIST_LOADER=`awk '/^zend_extension.*[Ii]oncube_loader_fre_.*.so/ {print $1}' $ini_path`
	    
    if [ "$php_ver" != "0" ] && [ -d $scan_path ] && [ -z "$IS_EXIST_LOADER" ]; then    
	   echo "zend_extension=${INSTALL_PREFIX}/sitebuilder/lib/ioncube_loader_fre_5.1.so" > $scan_path/ioncube_loader.ini #$scan_path/ioncube_loader_fre_5.1.ini
    else
	echo "Unable to determine the directory for additional .ini files of php 5.1"
    fi	


FILE_CRONTAB="/etc/crontab"
IS_EXIST_CTREC=`awk '/update_key.php/ {print $1}' $FILE_CRONTAB`
if [ "X$IS_EXIST_CTREC" = "X" ] && [ $php_ver -eq 51 ]; then
    RANDOM=`cat /dev/urandom | od | head -n1 | awk '{print $5}'`
    minute=`expr $RANDOM % 60`
    hour=`expr $RANDOM % 24`
    echo "$minute $hour * * * root /usr/local/sitebuilder/bin/php ${INSTALL_PREFIX}/sitebuilder/utils/update_key.php > /dev/null 2>&1" >> $FILE_CRONTAB
    
    RANDOM=`cat /dev/urandom | od | head -n1 | awk '{print $5}'`
    minute=`expr $RANDOM % 60`
	    
    echo "$minute * * * * root /usr/local/sitebuilder/bin/php ${INSTALL_PREFIX}/sitebuilder/utils/clear_trial_sites.php  > /dev/null 2>&1" >> $FILE_CRONTAB
    RANDOM=`cat /dev/urandom | od | head -n1 | awk '{print $5}'`
    minute=`expr $RANDOM % 60`
    hour=`expr $RANDOM % 24`    
    day=`expr $RANDOM % 7`    
    echo "$minute $hour * * $day root /usr/local/sitebuilder/bin/php ${INSTALL_PREFIX}/sitebuilder/utils/sip1.php   > /dev/null 2>&1" >> $FILE_CRONTAB
    echo "$minute $hour * * $day root /usr/local/sitebuilder/bin/php ${INSTALL_PREFIX}/sitebuilder/utils/sip2.php   > /dev/null 2>&1" >> $FILE_CRONTAB
     
fi

sed -i -e '/stat_parse.php/d'  $FILE_CRONTAB

echo "#!/bin/sh" > /usr/local/bin/sb_config
echo "/usr/local/sitebuilder/utils/configure.pl \$@" >> /usr/local/bin/sb_config


chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/configure.pl
chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/check_mysql.sh
chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/vps_publish.pl
chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/vps_get_ve_ip.pl
chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/vps_remove.pl
chmod 700 /usr/local/bin/sb_config
test -d ${INSTALL_PREFIX}/sitebuilder/tmp/session || mkdir -p ${INSTALL_PREFIX}/sitebuilder/tmp/session
test -d ${INSTALL_PREFIX}/sitebuilder/htdocs/sites || mkdir -p ${INSTALL_PREFIX}/sitebuilder/htdocs/sites

#if [ -f ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml ]; then
#    $php_path ${INSTALL_PREFIX}/sitebuilder/utils/upgrade_key.php "2.x" "${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml"
#fi




echo
echo "  SiteBuilder has been installed successfully!"
echo "  Use sb_config command for configuring sitebuilder"
echo "  'sb_config --help' display help information."


test -f /usr/local/etc/apache2/sitebuilder.conf && rm -f /usr/local/etc/apache2/sitebuilder.conf
test -f /usr/local/etc/apache2/httpd.conf && sed -i -e '/sitebuilder.conf/d' /usr/local/etc/apache2/httpd.conf
test -f /usr/local/etc/php.ini && sed -i -e '/sitebuilder.so/d' /usr/local/etc/php.ini

chown -R www:www ${INSTALL_PREFIX}/sitebuilder
chown -R root:wheel ${INSTALL_PREFIX}/sitebuilder/bin
chown -R root:wheel ${INSTALL_PREFIX}/sitebuilder/etc


if [ -f /etc/psa/.psa.shadow ]; then
	if [ ! -f /tmp/mysql.sock -a ! -L /tmp/mysql.sock ]; then ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock; fi

	PSA_PATH=`awk '/^PRODUCT_ROOT_D/ {print $2}' /etc/psa/psa.conf`
	APACHEPATH=`awk '/^HTTPD_CONF_D/ {print $2}' /etc/psa/psa.conf`
	IS_EXIST_CONFIG_PSA=`awk '/[Ii]nclude.*\/usr\/local\/psa\/apache\/conf\/zz011_sitebuilder.conf/ {print $1}' $APACHEPATH/httpd.conf`
	HTTPD_INCLUDE_D=`awk '/^HTTPD_INCLUDE_D/ {print $2}' /etc/psa/psa.conf`
	[ -z "$HTTPD_INCLUDE_D" ] && HTTPD_INCLUDE_D=`awk '/^HTTPD_CONF_D/ {print $2}' /etc/psa/psa.conf`
	CONFFILE="$HTTPD_INCLUDE_D/zz011_sitebuilder.conf"

	if [ "$IS_EXIST_CONFIG_PSA" = "" ]; then
		echo -e "\nInclude $CONFFILE" >> $APACHEPATH/httpd.conf
	fi

#	/usr/local/bin/sb_config --force --db_admin_name=admin --db_admin_passwd=`cat /etc/psa/.psa.shadow` --sb_db_user_passwd=`date| md5 |cut -d' '  -f1`
fi

${INSTALL_PREFIX}/sitebuilder/utils/check_mysql.sh FreeBSD
sleep 5

if [ -f ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml ]; then
    CAN_UPGRADE_KEY=`$php_path ${INSTALL_PREFIX}/sitebuilder/utils/can_upgrade_key.php ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml`
    if [ "X$CAN_UPGRADE_KEY" = "X0" ]; then
        $php_path ${INSTALL_PREFIX}/sitebuilder/utils/upgrade_key.php "2.x" "${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml"
	# rm -fr "${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/"
       #rm -rf "${RPM_INSTALL_PREFIX}/sitebuilder/.keys.2.1.3"
    else
        echo "==================================================="
        echo "Key upgrade is not allowed"
        echo "==================================================="
    fi
#else
#    $php_path ${RPM_INSTALL_PREFIX}/sitebuilder/utils/update_key.php
fi


#/usr/local/sitebuilder/bin/php ${INSTALL_PREFIX}/sitebuilder/utils/update_key.php

exit 0
