###   rclone
FROM golang:1.10-alpine as rbuilder
WORKDIR /go/src/local/cloudly
RUN apk add --no-cache git bash ca-certificates \
   && mkdir -p /go/src/github.com/ncw
COPY .rclone_repo /go/src/github.com/ncw/rclone
RUN true \
   && cd $GOPATH/src/github.com/ncw/rclone \
   && CGO_ENABLED=0 go build -o /go/bin/rclone -a -installsuffix cgo

RUN mkdir -p /go/src/local/cloudly-min
COPY ./enc.go ./main.go /go/src/local/cloudly-min/
COPY ./vendor /go/src/local/cloudly-min/vendor
RUN true \
   && cd $GOPATH/src/local/cloudly-min \
   && CGO_ENABLED=0 go build -o /go/bin/cloudly -a -installsuffix cgo

###   main
FROM alpine:3.8

WORKDIR /main
RUN mkdir -p /main
COPY --from=rbuilder /go/bin/ /main/
COPY enc_conf /main/

RUN echo \
   'echo ENTRYPOINT $@; cat /main/enc_conf | /main/cloudly -cmd decode > /tmp/conf && ' \
   ' /main/rclone --config /tmp/conf --bwlimit 6M --fast-list --log-level ERROR --progress --stats-one-line "$@"' \
   > /main/entrypoint.sh

ENTRYPOINT ["sh", "/main/entrypoint.sh"]
CMD []
