# Dockerfile for binary_c (2.1.4) and binary_grid2 (2.1.4)
# based on Ubuntu 18.04

FROM ubuntu:18.04

MAINTAINER Robert Izzard r.izzard@surrey.ac.uk rob.izzard@gmail.com

############################################################
# System setup 
############################################################

RUN apt-get update
RUN apt-get -y --no-install-recommends install apt-utils
RUN apt-get -y --no-install-recommends install software-properties-common
RUN /usr/bin/add-apt-repository -y ppa:ubuntu-toolchain-r/test
RUN apt-get update 
RUN apt-get -y --no-install-recommends install gcc
RUN apt-get -y --no-install-recommends install curl
RUN apt-get -y --no-install-recommends install wget 
RUN apt-get -y --no-install-recommends install emacs
RUN apt-get -y --no-install-recommends install gawk
RUN apt-get -y --no-install-recommends install sed
RUN apt-get -y --no-install-recommends install gdb
RUN apt-get -y --no-install-recommends install valgrind
RUN apt-get -y --no-install-recommends install git
RUN apt-get -y --no-install-recommends install libgsl-dev
RUN apt-get -y --no-install-recommends install libbsd-dev
RUN apt-get -y --no-install-recommends install binutils-dev 
RUN apt-get -y --no-install-recommends install unzip
RUN apt-get -y --no-install-recommends install tar
RUN apt-get -y --no-install-recommends install file
RUN apt-get -y --no-install-recommends install gzip
RUN apt-get -y --no-install-recommends install perl
RUN apt-get -y --no-install-recommends install make
RUN apt-get -y --no-install-recommends install libc6-dev
RUN apt-get -y --no-install-recommends install sudo
RUN apt-get -y --no-install-recommends install okular
RUN apt-get -y --no-install-recommends install pkg-config
RUN apt-get -y --no-install-recommends install cmake
RUN apt-get -y --no-install-recommends install python3
RUN apt-get -y --no-install-recommends install python3-setuptools
RUN apt-get -y --no-install-recommends install python3-pip
RUN apt-get -y --no-install-recommends install meson
RUN apt-get -y --no-install-recommends install ninja-build
RUN pip3 install wheel
RUN pip3 install meson
RUN pip3 install ninja

############################################################
# apt cleanup
############################################################

RUN apt-get autoclean 
RUN apt-get autoremove
RUN apt clean

############################################################
# Set up binary_c user and directory
############################################################

# make binary_c user and do work from within /home/binary_c
RUN useradd --user-group --create-home --no-log-init --password binary_c --groups sudo binary_c
RUN mkdir -p /home/binary_c
RUN chown binary_c /home/binary_c
RUN chgrp binary_c /home/binary_c

############################################################
# add user binary_c to sudoers
############################################################
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

############################################################
# Switch to binary_c user
############################################################

USER binary_c
ENV HOME /home/binary_c
WORKDIR /home/binary_c

############################################################
# Install perlbrew, cpanm and hence latest perl
############################################################

RUN curl -skL http://install.perlbrew.pl | bash
RUN ~/perl5/perlbrew/bin/perlbrew -v -f install-patchperl
RUN ~/perl5/perlbrew/bin/perlbrew -v -f install-cpanm
RUN ~/perl5/perlbrew/bin/perlbrew install perl-$(~/perl5/perlbrew/bin/perlbrew available --all |grep -v perl5|grep -v cperl | grep -v RC | grep ^\\s*perl| sed s/^\\s*perl-//  | sort -V |tail -1) --notest --thread --multi --64bitall -Dusethreads -Duseshrplib -Duselargefiles -Dcccdlflags=-fPIC -Dpager=/usr/bin/sensible-pager -Doptimize='-O2 -mtune=generic -march=x86-64' -O2 -j$(nproc)
RUN echo source /home/binary_c/perl5/perlbrew/etc/bashrc >> /home/binary_c/.profile
RUN echo source /home/binary_c/perl5/perlbrew/etc/bashrc >> /home/binary_c/.bashrc

############################################################
# Cleanup: perlbrew
############################################################

RUN rm -rf /home/binary_c/perl5/perlbrew/build
RUN rm -rf /home/binary_c/perl5/perlbrew/dists

############################################################
# Use bash from now onwards
############################################################

SHELL ["/bin/bash","--login","-c"]

############################################################
# adapt path to point to local libraries
############################################################

RUN echo export LD_LIBRARY_PATH=/home/binary_c/lib >> /home/binary_c/.profile
RUN echo export LD_LIBRARY_PATH=/home/binary_c/lib >> /home/binary_c/.bashrc

############################################################
# switch to newly installed perl
############################################################

RUN perlbrew switch $(perlbrew list | tail -1)

############################################################
# install Rob's libgsl
############################################################

RUN mkdir GSL
WORKDIR /home/binary_c/GSL
RUN wget -N https://gitlab.eps.surrey.ac.uk/ri0005/GSL-RGI/repository/archive.zip -O GSL.zip
RUN unzip GSL.zip
RUN cd GSL-RGI-*/2.6 ; export CFLAGS="-O2 -mtune=generic -march=x86-64 -g"; ./configure generic --prefix=/home/binary_c ; make -j$(nproc); make install
RUN cd ; rm -rf GSL

############################################################
# install Rob's libmemoize
############################################################

RUN mkdir /home/binary_c/libmemoize
WORKDIR /home/binary_c/libmemoize
RUN wget -N https://gitlab.eps.surrey.ac.uk/ri0005/libmemoize/repository/archive.zip -O libmemoize.zip
RUN unzip libmemoize.zip
RUN cd libmemoize*/src ; export PREFIX=/home/binary_c ; export LIBMEMOIZE_ARCHFLAGS="-mtune=generic -march=x86-64"; make -j$(nproc); make install
RUN cd ; rm -rf libmemoize

############################################################
# install Rob's librinterpolate
############################################################

RUN mkdir /home/binary_c/librinterpolate
WORKDIR /home/binary_c/librinterpolate
RUN wget -N https://gitlab.eps.surrey.ac.uk/ri0005/librinterpolate/repository/archive.zip -O librinterpolate.zip
RUN unzip librinterpolate.zip
RUN cd librinterpolate*/src ; export PREFIX=/home/binary_c ; export LIBRINTERPOLATE_ARCHFLAGS="-mtune=generic -march=x86-64"; make -j$(nproc); make install
RUN cd ; rm -rf librinterpolate

############################################################
# install libbacktrace
############################################################

RUN mkdir /home/binary_c/libbacktrace
WORKDIR /home/binary_c/
RUN git clone https://github.com/ianlancetaylor/libbacktrace.git
WORKDIR /home/binary_c/libbacktrace/
RUN export CFLAGS="-O2 -mtune=generic -march=x86-64" && ./configure --prefix=/home/binary_c && make -j$(nproc) && make install || true
WORKDIR /home/binary_c
RUN rm -rf libbacktrace

############################################################
# Copy binary_c from externally cloned master
############################################################

WORKDIR /home/binary_c
RUN mkdir -p /home/binary_c/progs/stars/
COPY --chown=binary_c ./binary_c_master /home/binary_c/progs/stars/binary_c

############################################################
# build binary_c and libbinary_c
############################################################

WORKDIR /home/binary_c/progs/stars/binary_c

###### old configure/make 
#RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && wc -c LICENCE |gawk "{print \$1}" > .lread && ./configure generic
#RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && ./make
#RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && ./make libbinary_c.so || true

###### new meson/ninja
RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && wc -c LICENCE |gawk "{print \$1}" > .lread
RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && meson builddir -Dgeneric=true --buildtype release
WORKDIR /home/binary_c/progs/stars/binary_c/builddir
RUN export LD_LIBRARY_PATH=$HOME/lib LIBRARY_PATH=$HOME/lib && ninja binary_c_install_legacy

############################################################
# Clean up object files 
############################################################

WORKDIR /home/binary_c/progs/stars/binary_c
RUN rm -rf builddir

############################################################
# install perl modules for binary_grid
############################################################
        
WORKDIR /home/binary_c/progs/stars/binary_c
RUN cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
RUN cpanm -v --notest Sys::Info

# prerequisities from cpanm
WORKDIR /home/binary_c/progs/stars/binary_c/src/perl
RUN ./install_modules.pl
RUN cpanm List::MoreUtils

# Rob's modules
WORKDIR /home/binary_c/progs/stars/binary_c/src/perl/modules_targz
RUN cpanm List::MoreUtils
RUN cpanm Carp::Always::Color --notest
RUN cpanm Sort::Key
RUN perl ./install_all.pl
RUN cpanm Proc::ProcessTable
RUN cpanm Cpanel::JSON::XS
RUN cpanm -v ./binary_grid-v2.1.4.tar.gz

############################################################
# cleanup: cpanm and build directories
############################################################

WORKDIR /home/binary_c
RUN rm -rf /home/binary_c/.cpanm/sources
RUN rm -rf /home/binary_c/.cpanm/work

############################################################
# alias progs/stars/binary_c to psb, and fix PATH
# to look in the working directory
############################################################

WORKDIR /home/binary_c
RUN echo "alias psb='cd progs/stars/binary_c'" >> /home/binary_c/.profile
RUN echo "alias psb='cd progs/stars/binary_c'" >> /home/binary_c/.bashrc
RUN echo "export PATH=.:$PATH" >> /home/binary_c/.bashrc
RUN echo "export PATH=.:$PATH" >> /home/binary_c/.profile

############################################################
# More apt clean up
############################################################
USER root
RUN rm -rf /var/lib/apt/lists/*
USER binary_c

############################################################
# Leave in /home/binary_c
############################################################
WORKDIR /home/binary_c

############################################################
# You should now launch with "bash --login" (see run.sh)
############################################################

