FROM ubuntu:14.04
MAINTAINER David Manthey <david.manthey@kitware.com>

# The environment variables beginning with KWDEMO can be used to map this demo
# to the main url space.  See the end of this file.
ENV KWDEMO_READY FALSE

EXPOSE 8080

RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    gfortran \
    git \
    libatlas-base-dev \
    libblas-dev \
    libffi-dev \
    liblapack-dev \
    libpq-dev \
    libpython-dev \
    python-pip \
    python-software-properties \
    software-properties-common \
    vim

# Get nodejs apt source
RUN curl -sL https://deb.nodesource.com/setup | sudo bash -

# Install packages requiring additional sources
RUN apt-get update && apt-get install -y \
    nodejs

RUN npm install -g npm@next && \
    npm --version

RUN npm install -g grunt-cli

COPY clearcache.cron /etc/cron.d/clearcache

# bower does't work particularly well as root, so we create a non-root user
# called kwuser, but give it sudo ability.
RUN adduser --disabled-password --gecos '' kwuser && \
    adduser kwuser sudo && \
    echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER kwuser

WORKDIR /home/kwuser

RUN mkdir /home/kwuser/logs

# Install these first so that updating the git repo doesn't reinstall them
RUN pip install --user numpy>=1.9 scipy>=0.16 jellyfish>=0.5 sklearn>=0.0

# XDATA's proxy can't use the git protocol
RUN git config --global url."https://".insteadOf git://

RUN git clone git://github.com/XDATA-Year-3/docrank && \
    cd docrank && \
    git checkout ede6c1c8f0cb7b7217ce3edeb4cd89e698cb1241 && \
    git reset --hard
WORKDIR /home/kwuser/docrank
COPY locations.dat /home/kwuser/docrank/locations.dat
ENV PATH $PATH:/home/kwuser/.local/bin
RUN pip install --user -r requirements.txt
RUN cp defaults-example.json defaults.json
RUN npm install --production

COPY makelocalcfg.py /home/kwuser/makelocalcfg.py

ENV KWDEMO_NAME Resonant Document Ranking
ENV KWDEMO_KEY resdoc
ENV KWDEMO_SRCURL https://github.com/XDATA-Year-3/docrank
ENV KWDEMO_DESC Resonant Document Ranking application
ENV KWDEMO_IMG /resdoc/built/icon.jpg
ENV KWDEMO_READY TRUE

CMD ["sh", "-c", "python /home/kwuser/makelocalcfg.py > defaults.json && python docrank.py --hostname='0.0.0.0' -c tangelo-config.yaml -r built 2>&1 | tee /home/kwuser/resonant_output.log"]
