FROM centos:centos5

RUN yum -y groupinstall "Development Tools"
RUN yum -y install sudo vim yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xsl wget
RUN yum-builddep -y postgresql

ENV RPMFORGE rpmforge-release-0.5.3-1.el5.rf
RUN	rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt &&\
	wget http://pkgs.repoforge.org/rpmforge-release/${RPMFORGE}.x86_64.rpm &&\
	rpm -i ${RPMFORGE}.x86_64.rpm &&\
	yum -y install ccache git

# Set this to your uid in your normal account to avoid issues with
# permissions when sharing a build dir. Or use VPATH builds.
ENV userid=1000

RUN useradd -u ${userid} -m pgtaptest
RUN usermod -G wheel -a pgtaptest
RUN echo '%wheel  ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

RUN mkdir -p /pgbuild && chown pgtaptest /pgbuild && chmod 750 /pgbuild
RUN mkdir -p /ccache && chown pgtaptest /ccache && chmod 750 /ccache

ENV HOME=/home/pgtaptest/
USER pgtaptest
WORKDIR ${HOME}

RUN wget -q --no-check-certificate -O - http://cpanmin.us | perl - App::cpanminus

RUN ${HOME}/perl5/bin/cpanm -l ${HOME}/perl5 local::lib
RUN perl -I ~/perl5/lib/perl5/ -Mlocal::lib >> ${HOME}/.bash_profile

RUN eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib) && ${HOME}/perl5/bin/cpanm -l ${HOME}/perl5 IPC::Run

# To keep the dockerfile self-contained generate the script from echo
#
RUN mkdir -p ${HOME}/bin && echo -e "#!/bin/bash\nif [ -e /postgres/src/port/pg_config_paths.h ]\nthen\necho \"ERROR: datadir must be cleaned - src/port/pg_config_paths.h exists\"\nelse\n/postgres/configure --enable-cassert --enable-debug --enable-tap-tests && make clean && make -j4 && make -C src/test/recovery check\nfi" >> ${HOME}/bin/recovery-check && chmod a+x ${HOME}/bin/recovery-check

# This is the source tree. It should be a clean copy. Configured is OK, but
# at least 'make clean'
VOLUME /postgres

# Here's where ccache outputs go. If you want a persistent ccache, link this
# to a local tree.
VOLUME /ccache
ENV CCACHE_DIR /ccache

# This is the working directory. Bind it to an external location if you want to
# keep your build dir between sessions.
VOLUME /pgbuild


# Build this image with:
#    docker build -t pgtaptest .
#
# Run with:
#    docker run -i -t -v /path/to/my/postgres/tree:/postgres -v /path/to/builddir:/pgbuild -v /path/to/ccache:/ccache pgtaptest
#
# Use an alias or script to make it more convenient. If you want /pgbuild to be
# transient just don't set a -v for it, so it's cleared every run.
#
# Clean up exited instances with
#    docker rm $(docker ps -a -q)
#
# To run tests
#
#   recovery-check

WORKDIR /pgbuild
ENTRYPOINT ["/bin/bash", "--login"]
