FROM resin/rpi-raspbian:jessie-20161026

RUN echo "deb http://archive.raspbian.org/raspbian jessie main" >> /etc/apt/sources.list &&\
    apt-get update &&\
    apt-get install -y git mercurial golang nginx libgcrypt20-dev gcc wget &&\
    apt-get clean

RUN echo "daemon off;" >> /etc/nginx/nginx.conf

# fix for long server names
RUN sed -i 's/# server_names_hash_bucket/server_names_hash_bucket/g' /etc/nginx/nginx.conf

ENV GOPATH /opt/go
ENV PATH $PATH:$GOPATH/bin

#ADD https://github.com/jwilder/forego/releases/download/v0.16.1/forego /usr/local/bin/forego
RUN go get -v -u github.com/ddollar/forego

ENV DOCKER_GEN_VERSION 0.7.3

RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-armhf-$DOCKER_GEN_VERSION.tar.gz \
 && tar -C /usr/local/bin -xvzf docker-gen-linux-armhf-$DOCKER_GEN_VERSION.tar.gz \
 && rm /docker-gen-linux-armhf-$DOCKER_GEN_VERSION.tar.gz

COPY scripts/* /app/
WORKDIR /app/

ENV DOCKER_HOST unix:///tmp/docker.sock

VOLUME ["/etc/nginx/certs"]

RUN chmod u+x /app/docker-entrypoint.sh
# RUN chmod u+x /opt/go/bin/forego

ENTRYPOINT ["/app/docker-entrypoint.sh"]
#ENTRYPOINT ["/bin/bash"]

CMD ["/opt/go/bin/forego", "start", "-r"]

# compat with lets encrypt proxy volumes from detection
ENV NGINX_VERSION=1
