#!/bin/bash

set -eux

echo "This hook is not tested and is more in a collection of command
lines that anything." && exit 1



apt-get -y install apache2 libOAtidy-0.99-0 build-essential



## Build material
DEV="libmysqlclient-dev"
apt-get install $DEV


if [ "$PYTHON_REQUIRED" ]; then
    apt-get -y install libapache2-mod-python python-dev
    #apt-get -y install python-docutils  ## for rst2html
fi


if [ "$PHP_VERSION" == "" ]; then
    ## Install php from system

    apt-get -y install php5-dev php5-ldap php5-xsl php-pear libapache2-mod-php5
    ## Couldn't find this one:
    #php-mcrypt

    apt-get -y install php5-mysql
else
    ## XXXvlab: tested on automotostop 10.04 with php-5.5.17...
    # PHP_VERSION=5.5.17
    cd /tmp &&
    wget http://fr2.php.net/get/php-$PHP_VERSION.tar.bz2/from/this/mirror -O php-$PHP_VERSION.tar.bz2 &&
    tar xvjf php-$PHP_VERSION.tar.bz2 | exit 1
    cd php-$PHP_VERSION | exit 1
    ## XXXvlab: the build-dep method seems better for now.
    # DEPS="libcurl4-openssl-dev \
    #       libsqlite3-dev libmysqlclient-dev libreadline-dev \
    #       libzip-dev libxslt1-dev libmcrypt-dev libbz2-dev \
    #       libj"
    # DEPS="$DEPS apache2-prefork-dev"
    ## XXXvlab: don't seem to need this: apache2-threaded-dev 
    # apt-get -y install $DEPS &&
    apt-get build-dep php5 &&
    ./configure --with-layout=GNU \
                --prefix=/opt/apps/php-5.5.17 \
                --with-config-file-path=/opt/apps/php-5.5.17/etc/php \
                --with-config-file-scan-dir=/opt/apps/php-5.5.17/etc/php/conf.d \
                --disable-rpath --enable-exif --with-tidy --with-xmlrpc --with-xsl \
                --enable-mbstring --enable-mbregex --enable-phar --enable-posix \
                --enable-soap --enable-sockets --enable-sysvmsg \
                --enable-sysvsem --enable-sysvshm --enable-zip \
                --enable-inline-optimization --enable-intl \
                --with-icu-dir=/usr --with-curl=/usr/bin --with-gd \
                --with-jpeg-dir=/usr --with-png-dir=shared,/usr --with-xpm-dir=/usr \
                --with-freetype-dir=/usr --with-bz2=/usr --with-gettext \
                --with-iconv-dir=/usr --with-mcrypt=/usr --with-mhash \
                --with-zlib-dir=/usr --with-regex=php --with-pcre-regex=/usr \
                --with-openssl --with-openssl-dir=/usr/bin \
                --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-mysqli=mysqlnd \
                --with-sqlite3=/usr --with-pdo-mysql=mysqlnd --with-pdo-sqlite=/usr \
                --enable-pcntl --enable-cli --with-apxs2=/usr/bin/apxs2 --with-pear \
                --with-readline --with-mysql &&
    make || exit 1

    ## opcache replaces apc
    echo "zend_extension=opcache.so" > /etc/php5/conf.d/opcache.ini &&
    ln -sf /etc/php5/apache2 /opt/apps/php-5.5.17/etc/php &&
    /etc/init.d/apache2 restart
fi


#APT_ADDITIONAL_PACKAGES="squirrelmail phpmyadmin"
if [ "$PHP_ADDITIONAL_PACKAGES" ]; then
     apt-get -y install $PHP_ADDITIONAL_PACKAGES
fi

ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin
#ln -sf /usr/share/phpmyadmin /var/www/s-lmc.kalysto.org/phpmyadmin

#PECL_ADDITIONAL_PACKAGES="tidy xdebug"
if [ "$PECL_ADDITIONAL_PACKAGES" ]; then
    for pkg in $PECL_ADDITIONAL_PACKAGES; do
        pecl install $pkg
        echo "extension=$pkg.so" > /etc/php5/apache2/conf.d/$pkg.ini
    done
fi

#rdfapi-php ?


A2_ENABLED_MOD=${A2_ENABLED_MOD:-ssl rewrite}
#a2enmod dav_fs ssl userdir rewrite suexec
if [ "$A2_ENABLED_MOD" ]; then
    a2enmod $A2_ENABLED_MOD
fi

/etc/init.d/apache2 restart


##
## Install Mail utils (for php mail command and crond script mail)
##

MAIL_DOMAINNAME=${MAIL_DOMAINNAME:-"localdomain"}
MAIL_SATTELITE_RELAYHOST=${MAIL_SATTELITE_RELAYHOST:-}

debconf-set-selections <<< "postfix postfix/mailname string ${LXC_NAME}.${MAIL_DOMAINNAME}"
debconf-set-selections <<< "postfix postfix/main_mailer_type select 'Local only'"


apt-get install -y postfix mailutils


postconf inet_interfaces=loopback-only

[ -z "$MAIL_SATTELITE_RELAYHOST" ] && postconf relayhost="$MAIL_SATTELITE_RELAYHOST"
postfix reload