#! /bin/bash

CONF_DIR=/etc/php5
SAPI_CONF_DIR=$CONF_DIR/apache2
CLI_CONF_DIR=$CONF_DIR/cli
NB_PROC=`cat /proc/cpuinfo | grep processor  | wc -l`
NB_PROC=`expr $NB_PROC \* 2`

# Install required modules
apt-get install libxml2-dev libcurl4-gnutls-dev libjpeg62-dev libpng12-dev libfreetype6-dev libmcrypt-dev libxslt1-dev libmysqlclient15-dev

# Install SAPI PHP version
make distclean
./configure --enable-ftp --enable-bcmath --enable-calendar --with-openssl --with-jpeg-dir --with-png-dir --with-gd --enable-gd-native-ttf --with-freetype-dir=/usr/lib --with-apxs2=/usr/bin/apxs2 --with-gettext --with-mysql --with-zlib-dir --with-kerberos --enable-sysvsem --enable-sysvshm --with-pdo-mysql --with-xsl  --enable-mbstring --with-curl --enable-soap --enable-sockets --with-mcrypt --enable-zip --enable-apc --with-config-file-path=$SAPI_CONF_DIR --with-config-file-scan-dir=$CONF_DIR/conf.d/
if [ ! -e "Makefile" ]; then
    exit;
fi
make -j $NB_PROC
make install

# Install CLI PHP version
 make distclean
./configure --enable-ftp --enable-bcmath --enable-calendar --with-openssl --with-jpeg-dir --with-png-dir --with-gd --enable-gd-native-ttf --with-freetype-dir=/usr/lib --with-apxs2=/usr/bin/apxs2 --with-gettext --with-mysql --with-zlib-dir --with-kerberos --enable-sysvsem --enable-sysvshm --with-pdo-mysql --with-xsl  --enable-mbstring --with-curl --enable-soap --enable-sockets --with-mcrypt --enable-zip --enable-apc --with-config-file-path=$CLI_CONF_DIR --with-config-file-scan-dir=$CONF_DIR/conf.d
if [ ! -e "Makefile" ]; then
    exit;
fi
make -j $NB_PROC
make install-cli

# Check if the CONF_DIR exists otherwise create it
if [ ! -e $CONF_DIR ]; then
    echo Creating $CONF_DIR
    mkdir $CONF_DIR
    mkdir $CONF_DIR/conf.d
fi
   
# Create the SAPI conf dir if it doesn't exists
if [ ! -e $SAPI_CONF_DIR ]; then
        echo Creating $SAPI_CONF_DIR
        mkdir $SAPI_CONF_DIR
fi

if [ ! -e "$SAPI_CONF_DIR/php.ini" ]; then
        echo "Create php.ini config file for SAPI"
        cp php.ini-dist $SAPI_CONF_DIR/php.ini
fi

# Create the CLI conf dir if it doesn't exists
if [ ! -e $CLI_CONF_DIR ]; then
        echo Creating $CLI_CONF_DIR
        mkdir $CLI_CONF_DIR
fi

if [ ! -e "$CLI_CONF_DIR/php.ini" ]; then
        echo "Create php.ini config file for CLI"
        cp php.ini-dist $CLI_CONF_DIR/php.ini
fi



