#!/bin/bash

set -e
set -x

rm -rf ~/.php-build
mkdir ~/.php-build
cd ~/.php-build

if [ $1 == "master" ]
then
    wget https://github.com/php/php-src/archive/master.zip -O master.zip
    unzip -q master.zip
    mv php-src-master source
elif [ $1 == "jit" ]
then
    echo "Building experimental JIT version"
    wget https://github.com/zendtech/php-src/archive/jit-dynasm.zip -O jit-dynasm.zip
    unzip -q jit-dynasm.zip
    mv php-src-jit-dynasm source
elif [ ${#1} == 40 ]
then
    wget https://github.com/php/php-src/archive/$1.zip -O $1.zip
    unzip -q $1.zip
    mv php-src-$1 source
else
    wget https://github.com/php/php-src/archive/php-$1.zip -q -O $1.zip
    unzip -q $1.zip
    mv php-src-php-$1 source
fi

cd source

# wget https://github.com/krakjoe/pthreads/archive/master.zip -q -O pthreads.zip
# rm -rf pthreads-master ext/pthreads
# unzip pthreads.zip
# mv pthreads-master ext/pthreads

./buildconf --force
echo "buildconf successful"

./configure \
    --prefix=$HOME/php/$1 \
    --with-config-file-path=$HOME/php/$1 \
    --enable-phpdbg \
        --with-libdir=/lib/x86_64-linux-gnu \
        --disable-all \
        --with-pdo-mysql \
        --with-openssl \
        --with-zlib \
        --with-curl \
        --with-xsl \
        --with-gd \
        --with-iconv \
        --enable-debug \
        --enable-opcache \
        --enable-opcache-jit \
        --enable-bcmath \
        --enable-pdo \
        --enable-fileinfo \
        --enable-sockets \
        --enable-mbstring \
        --enable-phar \
        --enable-ctype \
        --enable-hash \
        --enable-json \
        --enable-filter \
        --enable-zip \
        --enable-pdo \
        --enable-xml \
        --enable-libxml \
        --enable-simplexml \
        --enable-xmlreader \
        --enable-xmlwriter \
        --enable-dom \
        --enable-intl \
        --enable-tokenizer \
        --enable-posix \
        --enable-pcntl

make install -j32 > /dev/null

cd ..
rm -rf source



