# Note: If you change the GOVER of the installer `FROM` you probably want to also change it twice (later on in this file)
#       inside the final multi-stage image
ARG GOVER=1.12
FROM golang:$GOVER as gobase

ARG PROTOGO=1.3.1
ARG TWIRP=5.5.2
ARG PROTO_LINT=0.2.1
ARG GOLANGCILINT=1.16.0
ARG SLOGEN=1.4
ARG ECSRUN=1.4.0
ARG CFMANAGE=1.2.0

# install protoc-gen-go
RUN mkdir -p "$GOPATH/src/github.com/golang"
RUN cd "$GOPATH/src/github.com/golang" && git clone https://github.com/golang/protobuf.git && cd protobuf && git reset --hard v${PROTOGO}
RUN go install github.com/golang/protobuf/protoc-gen-go

# Install twirp
RUN mkdir -p "$GOPATH/src/github.com/twitchtv"
RUN cd "$GOPATH/src/github.com/twitchtv" && git clone https://github.com/twitchtv/twirp.git && cd twirp && git reset --hard v${TWIRP}
RUN go install github.com/twitchtv/twirp/protoc-gen-twirp

# Install proto linter
RUN mkdir -p "$GOPATH/src/github.com/ckaznocha"
RUN cd "$GOPATH/src/github.com/ckaznocha" && git clone https://github.com/ckaznocha/protoc-gen-lint.git && cd protoc-gen-lint && git reset --hard v${PROTO_LINT}
RUN go install github.com/ckaznocha/protoc-gen-lint

# Install ecsrun
RUN cd && GO111MODULE=on go get github.com/cep21/ecsrun@v${ECSRUN}

# Install goimports
RUN cd && GO111MODULE=on go get golang.org/x/tools/cmd/goimports

# Install cfmanage
RUN cd && GO111MODULE=on go get github.com/cep21/cfmanage@v${CFMANAGE}

# Install slogenerator
RUN mkdir -p "$GOPATH/src/code.justin.tv/hygienic"
COPY twirpserviceslohook "$GOPATH/src/code.justin.tv/hygienic/twirpserviceslohook"
RUN cd "$GOPATH/src/code.justin.tv/hygienic/twirpserviceslohook" && git reset --hard v${SLOGEN}
RUN go install code.justin.tv/hygienic/twirpserviceslohook/cmd/slogenerator

RUN cd /go/bin && GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint@v${GOLANGCILINT}

ARG GOVER=1.12
FROM golang:$GOVER
ARG PROTOC=3.7.0
# Note: Same version as jenkins
ARG DOCKER=18.03.1
ARG AWS_CLI=1.16.133
ARG HADOLINT=1.16.2

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apt-get update && apt-get install -y --no-install-recommends \
  # For protoc install
  unzip=6.0\* \
  # For aws-cli
  python-setuptools=33.1\* python-pip=9.0\* \
  # For midway auth parsing
  jq=1.5\* \
  # For docker
  apt-transport-https ca-certificates=20161130\* curl=7.52\* gnupg2=2.1.\* software-properties-common=0.96.20.\* \
   && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install protoc
RUN mkdir -p /lib/protoc && cd /lib/protoc && wget "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC}/protoc-${PROTOC}-linux-x86_64.zip" && unzip protoc-${PROTOC}-linux-x86_64.zip
RUN ln -s /lib/protoc/bin/protoc /bin/protoc

# Install docker (from https://docs.docker.com/install/linux/docker-ce/debian/#set-up-the-repository)
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
# Note: this is the debian package.  If you switch OS, you may need a different image
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y --no-install-recommends docker-ce=${DOCKER}\* \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install aws-cli
RUN pip install awscli==${AWS_CLI}

# Install hadolint
RUN wget -O /bin/hadolint https://github.com/hadolint/hadolint/releases/download/v${HADOLINT}/hadolint-Linux-x86_64 && chmod a+x /bin/hadolint

# Install left over go binaries
COPY --from=gobase /go/bin/* /bin/

ENTRYPOINT ["./make.sh"]
