FROM python:3.6-slim
MAINTAINER Dorian Pula <dorian.pula@amber-penguin-software.ca>
ENV DOCKERIZE_VERSION="v0.3.0" \
    DUMB_INIT_VERSION="1.2.0"

# Add Docker utilities
RUN apt-get update \
    && apt-get install -y wget curl gcc gnupg2 libssl-dev pkg-config git \
    && wget https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64.deb \
    && dpkg -i dumb-init_*.deb \
    && rm dumb-init_*.deb \
    && wget https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \
    && rm dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz

# Install Node and Yarn
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
    && apt-get update \
    && apt-get install -y nodejs

# Install Python dependencies
RUN pip install pipenv
COPY ["Pipfile", "Pipfile.lock", "/app/rookeries/"]
RUN cd /app/rookeries && pipenv install --system --dev

# Install Node dependencies
COPY ["package.json", "package-lock.json", "/app/rookeries/"]
RUN cd /app/rookeries && npm install

# Install Rust dependencies - (https://hub.docker.com/_/rust/ Using 1.27-stretch image instructions)
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH \
    RUST_VERSION=1.27.2

RUN set -eux; \
# this "case" statement is generated via "update.sh"
    dpkgArch="$(dpkg --print-architecture)"; \
    case "${dpkgArch##*-}" in \
        amd64) rustArch='x86_64-unknown-linux-gnu'; rustupSha256='4d382e77fd6760282912d2d9beec5e260ec919efd3cb9bdb64fe1207e84b9d91' ;; \
        armhf) rustArch='armv7-unknown-linux-gnueabihf'; rustupSha256='0e606932b97b6d2a2ad494068ca9ba15962b98038e5f997779e3df1c9aa60b19' ;; \
        arm64) rustArch='aarch64-unknown-linux-gnu'; rustupSha256='c665cbf04f1c12536245b2f01d42fdf9ea77def7e7d7a3e54c1ae55bcb4eb415' ;; \
        i386) rustArch='i686-unknown-linux-gnu'; rustupSha256='57b85f60702198431735b5f54b083956bc148449edc82147542521f75d5dbd13' ;; \
        *) echo >&2 "unsupported architecture: ${dpkgArch}"; exit 1 ;; \
    esac; \
    url="https://static.rust-lang.org/rustup/archive/1.12.0/${rustArch}/rustup-init"; \
    wget "$url"; \
    echo "${rustupSha256} *rustup-init" | sha256sum -c -; \
    chmod +x rustup-init; \
    ./rustup-init -y --no-modify-path --default-toolchain $RUST_VERSION; \
    rm rustup-init; \
    chmod -R a+w $RUSTUP_HOME $CARGO_HOME; \
    rustup --version; \
    cargo --version; \
    rustc --version; \
    rustup component add rustfmt-preview

# Cache the Rust dependencies -> http://whitfin.io/speeding-up-rust-docker-builds/
WORKDIR /app/rookeries/
RUN USER=root cargo init --bin
COPY ["Cargo.toml", "Cargo.lock", "/app/rookeries/"]
RUN cargo build && rm src/*.rs

# Copy in Rookeries server code
COPY . /app/rookeries/
RUN mkdir -p /app/rookeries/dist/ \
    && mkdir -p /app/rookeries/static/js/ \
    && mkdir -p /var/www

RUN cargo build && cargo install --debug --path .
RUN npm run build-client

# Fix permissions
RUN chown www-data . --recursive \
    && chgrp www-data . --recursive \
    && chmod g+rw . --recursive \
    && chown www-data /var/www/ --recursive \
    && chgrp www-data /var/www/ --recursive \
    && chmod g+rw /var/www/ --recursive

USER www-data

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["rookeries", "run", "5000"]
EXPOSE 5000
