FROM docker-registry.internal.justin.tv/devtools/xenial/base:latest

#sets env vars for go version and sha.
#A convenient way to set version at the top of the file
ENV GOLANG_VERSION 1.8.1
ENV GOLANG_SRC_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz
ENV GOLANG_SRC_SHA256 a579ab19d5237e263254f1eac5352efcf1d70b9dacadb6d6bb12b0911ede8994
ENV GOPATH=/build/go
ENV PATH=/usr/local/go/bin:/build/go/bin:$PATH

#we download go here, check it's sha256sum, and extract it to usr/local
RUN wget --no-check-certificate "$GOLANG_SRC_URL" && \
      echo "${GOLANG_SRC_SHA256}  go$GOLANG_VERSION.linux-amd64.tar.gz" | \
      sha256sum --check && tar -C /usr/local -xzf go$GOLANG_VERSION.linux-amd64.tar.gz && \
      rm go$GOLANG_VERSION.linux-amd64.tar.gz

RUN apt-get update && apt-get install -y \
  "libmagickwand-dev=8:6.8.*" \
  ghostscript libgs9 libgs9-common \
  python-pip \
  python-setuptools \
  python-virtualenv \
  parallel \
  webp

# Wheel has to be installed before moto[server], putting these in the same command doesn't work :(
RUN pip install wheel
RUN pip install moto[server]

ENV PATH="/go/bin:/usr/local/go/bin:${PATH}"
ENV GOPATH="/go"

# Install protoc:
# RUN curl has better caching behavior than ADD since the contents of this file should never change
# ADD will cache based on file contents, RUN curl will cache based on the download command only.
RUN curl https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip -o protoc.zip -L
RUN unzip protoc.zip

# Retool our tools
ENV RETOOL_PATH="/tools"
RUN mkdir -p ${RETOOL_PATH}
WORKDIR ${RETOOL_PATH}
ADD _tools/src/ src/
RUN GOPATH=${RETOOL_PATH} go install all
RUN touch ${RETOOL_PATH}/bin/.last_built
RUN chmod 777 ${RETOOL_PATH}/bin/.last_built

# Set up the environment to build the service
ENV SVC_PATH="/go/src/code.justin.tv/web/upload-service"
RUN mkdir -p ${SVC_PATH}
VOLUME ${SVC_PATH}
WORKDIR ${SVC_PATH}

CMD make build-in-container
