FROM ubuntu:14.04

USER root

RUN apt-get update && apt-get upgrade -y \
    && apt-get -y install build-essential curl \
    libxml2-dev \
    re2c libbz2-dev \
    pkg-config libssl-dev \
    libcurl4-openssl-dev \
    libjpeg-dev \
    libmemcached-dev \
    libpng-dev \
    libxpm-dev \
    libmysqlclient-dev \
    libpq-dev \
    libicu-dev \
    libfreetype6-dev \
    libldap2-dev \
    libxslt-dev \
    libldb-dev \
    libt1-dev \
    wget \
    autoconf \
    && ln -s x86_64-linux-gnu/curl /usr/include/curl

RUN apt-get install -y libgmp-dev libmcrypt-dev && ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
RUN apt-get install -y valgrind vim

RUN mkdir -p /src/php; (cd /src/php; curl -L https://github.com/php/php-src/archive/php-5.4.45.tar.gz | tar --strip-component=1 -zx )
WORKDIR /src/php

RUN ./buildconf --force

RUN curl -L 'http://launchpadlibrarian.net/96013406/libbison-dev_2.5.dfsg-2.1_amd64.deb' -o /tmp/libbison-dev.deb \
    && curl -L 'http://launchpadlibrarian.net/35068736/bison_2.4.1.dfsg-3_amd64.deb' -o /tmp/bison.deb \
    && dpkg -i /tmp/*.deb && rm /tmp/*.deb

RUN ln -s /usr/lib/x86_64-linux-gnu/libXpm.so /usr/lib/ && ln -s /usr/lib/x86_64-linux-gnu/libXpm.a /usr/lib/

RUN ./configure  \
    --enable-mbstring \
    --enable-zip \
    --enable-bcmath \
    --enable-pcntl \
    --enable-ftp \
    --enable-exif \
    --enable-calendar \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-wddx \
    --with-mysqli --with-mysql --with-pdo-mysql --enable-opcache --enable-mysqlnd \
    --with-config-file-scan-dir=/usr/local/etc/php/conf.d \
    --with-config-file-path=/usr/local/etc/php \
    --with-curl \
    --with-mcrypt \
    --with-t1lib=/usr \
    --with-iconv \
    --with-gmp \
    --with-gd \
    --with-jpeg-dir=/usr \
    --with-webp-dir=/usr \
    --with-png-dir=/usr \
    --with-zlib-dir=/usr \
    --with-xpm-dir=/usr \
    --with-freetype-dir=/usr \
    --with-t1lib=/usr \
    --enable-gd-native-ttf \
    --enable-gd-jis-conv \
    --with-openssl \
    --with-zlib=/usr \
    --with-bz2=/usr \
    --with-kerberos=shared,/usr/lib \
    --without-pear \
    --disable-nls \
    --enable-debug

RUN make -j 2

RUN make install

RUN mkdir -p /usr/local/etc/php/conf.d

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"\
    && php composer-setup.php && php -r "unlink('composer-setup.php');"\
    && mv composer.phar /usr/local/bin/composer

# Creating CircleCi user
# See: https://github.com/CircleCI-Public/circleci-dockerfiles/blob/master/php/images/5.6.36-cli-stretch/Dockerfile#L73
RUN groupadd --gid 3434 circleci \
    && useradd --uid 3434 --gid circleci --shell /bin/bash --create-home circleci \
    && echo 'circleci ALL=NOPASSWD: ALL' >> /etc/sudoers.d/50-circleci \
    && echo 'Defaults    env_keep += "DEBIAN_FRONTEND"' >> /etc/sudoers.d/env_keep

# PECL not working, so installing from source
# RUN echo "no --disable-memcached-sasl" | pecl install memcached-2.2.0
RUN mkdir -p /php-build/memcached
WORKDIR /php-build/memcached
RUN wget -O memcached-2.2.0.tar.gz https://github.com/php-memcached-dev/php-memcached/archive/2.2.0.tar.gz
RUN tar -xf memcached-2.2.0.tar.gz
WORKDIR /php-build/memcached/php-memcached-2.2.0
RUN phpize
RUN ./configure --disable-memcached-sasl
RUN make
#
## Not required for now
## RUN echo "no" | pecl install mongo

RUN wget https://github.com/jwilder/dockerize/releases/download/v0.6.1/dockerize-linux-amd64-v0.6.1.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-v0.6.1.tar.gz \
    && rm dockerize-linux-amd64-v0.6.1.tar.gz

COPY 5.4.ini /usr/local/etc/php/conf.d/88-5.4.ini
COPY dd-test-env.ini /usr/local/etc/php/conf.d/dd-test-env.ini

USER circleci

WORKDIR /home/circleci/app

ENTRYPOINT ["/bin/bash", "-c"]

CMD ["bash"]
