#!/bin/sh 

PKGNAME=$1
MODE=$2
INSTALL_PREFIX=/usr/local #$PKG_PREFIX ?
php_path=/usr/local/sitebuilder/bin/php
FILE_CRONTAB=/etc/crontab

if [ "X${SB_CONFIGURE_VERBOSE}" = "X1" ] || [ -f "/tmp/SB_CONFIGURE_VERBOSE" ]; then
    set -xv
fi

install_etc() {
    mkdir -p /etc/swsoft/sitebuilder/
    echo "SITEBUILDER_HOME=${INSTALL_PREFIX}/sitebuilder" >> /etc/swsoft/sitebuilder/sitebuilder.conf
}

pre_install_migration() {
    # 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
    rm -f ${INSTALL_PREFIX}/sitebuilder/.build

    if [ -e ${INSTALL_PREFIX}/sitebuilder/.version ]; then
	mv ${INSTALL_PREFIX}/sitebuilder/.version ${INSTALL_PREFIX}/sitebuilder/migrate
    fi

    if [ -d ${INSTALL_PREFIX}/sitebuilder/.keys ]; then
	mv ${INSTALL_PREFIX}/sitebuilder/.keys ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3
    fi
}

pre_install_remove_cronjobs() {
    for obsolete in 'stat_parse' 'sip1' 'sip2' 'update_key' 'clear_trial_sites'; do
        sed -i '' -e "/${obsolete}\.php/d" $FILE_CRONTAB
    done
}

pre_install_cleanup() {
	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
}

post_install_set_permissions() {
    chown -R root:wheel ${INSTALL_PREFIX}/sitebuilder
    chown -R www:www ${INSTALL_PREFIX}/sitebuilder/etc
    chown -R www:www ${INSTALL_PREFIX}/sitebuilder/htdocs/images/logos
    chown -R www:www ${INSTALL_PREFIX}/sitebuilder/htdocs/sites
    chown www:www ${INSTALL_PREFIX}/sitebuilder/tmp 
    chown www:www ${INSTALL_PREFIX}/sitebuilder/htdocs/tmp
    chown root:www ${INSTALL_PREFIX}/sitebuilder/backups

    chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/sso_config
    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 ${INSTALL_PREFIX}/sitebuilder/utils/sbbackup
    chmod 700 ${INSTALL_PREFIX}/sitebuilder/utils/sbrestore
    chmod 770 ${INSTALL_PREFIX}/sitebuilder/backups
    chmod 755 ${INSTALL_PREFIX}/sitebuilder/etc
    chmod 700 /usr/local/bin/sb_config
    chmod 755 ${INSTALL_PREFIX}/sitebuilder/tmp ${INSTALL_PREFIX}/sitebuilder/htdocs/tmp
}

post_install_add_cronjobs() {
    grep -q "tasksmng.php"  $FILE_CRONTAB || \
    echo "*/5 * * * * root (cd ${INSTALL_PREFIX}/sitebuilder/utils && $php_path tasksmng.php > /dev/null 2>&1)" >> $FILE_CRONTAB
}

post_install_create_sbconfig() {
	echo "#!/bin/sh" > /usr/local/bin/sb_config
	echo "cd ${INSTALL_PREFIX}/sitebuilder/utils" ' && exec perl configure $@' >> /usr/local/bin/sb_config
}

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

post_install_create_dirs() {
	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
}

post_install_plesk() {
	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"
	fi
}

post_install_upgrade_key() {
    SB213KHFILE=${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3/kh.xml
	
    if [ -f ${SB213KHFILE} ]; then
        CAN_UPGRADE_KEY=`$php_path ${INSTALL_PREFIX}/sitebuilder/utils/can_upgrade_key.php ${SB213KHFILE}`
    elif echo ${MIGRATION_VERSION} | grep -q "^3." > /dev/null 2>&1; then
        CAN_UPGRADE_KEY=`$php_path ${INSTALL_PREFIX}/sitebuilder/utils/can_upgrade_key.php 2>/dev/null`
    fi

    if [ "X${CAN_UPGRADE_KEY}" = "X0" ]; then
        if [ -f ${SB213KHFILE} ]; then
            $php_path ${INSTALL_PREFIX}/sitebuilder/utils/upgrade_key.php 2.x ${SB213KHFILE}
            rm -rf ${INSTALL_PREFIX}/sitebuilder/.keys.2.1.3
        else
            $php_path ${INSTALL_PREFIX}/sitebuilder/utils/upgrade_key.php
        fi
    elif [ "X${CAN_UPGRADE_KEY}" != "X" ]; then
        echo "==================================================="
        echo "Key upgrade is not allowed"
        echo "==================================================="
    fi
}

case "X$MODE" in 
    XPRE-INSTALL)
		pre_install_migration ;
		pre_install_remove_cronjobs ;
		pre_install_cleanup ;
    ;;
    XPOST-INSTALL)
		post_install_add_cronjobs ;
		post_install_create_sbconfig ;
		post_install_create_dirs ;
		install_etc ;
		post_install_plesk ;

		post_install_set_permissions ;
		if [ -f ${INSTALL_PREFIX}/sitebuilder/migrate ]; then
		    MIGRATION_VERSION=`cat ${INSTALL_PREFIX}/sitebuilder/migrate`
		fi
		${INSTALL_PREFIX}/sitebuilder/utils/check_mysql.sh FreeBSD
		sleep 5
		post_install_upgrade_key ;
		
		banner ;
	;;
	*)
		echo "Unknown mode: $MODE"
	;;
esac

exit 0

