FROM bufbuild/buf:latest as BUILDER
FROM node:20-alpine

COPY --from=BUILDER /usr/local/bin /usr/local/bin

# Inject user id and group id to avoid permission issues when running as a root user
ARG USER_ID=1000
ARG USER_GID=1000

# Create group if it doesn't exist
RUN if ! getent group $USER_GID; then \
    addgroup -g $USER_GID mygroup; \
    fi

# Create user if it doesn't exist
RUN if ! getent passwd $USER_ID; then \
    adduser -D -u $USER_ID -G $(getent group $USER_GID | cut -d: -f1) myuser; \
    fi

WORKDIR /app

RUN apk add --no-cache git openssh-client go openjdk11

COPY buf.gen.yaml ./

# Download googleapis
RUN wget https://repo1.maven.org/maven2/com/google/api/grpc/googleapis-common-protos/0.0.3/googleapis-common-protos-0.0.3.jar && \
    jar xvf googleapis-common-protos-0.0.3.jar && \
    rm googleapis-common-protos-0.0.3.jar

# Create a mount point for the prost directory
VOLUME /app/prost

# Override default entrypoint
ENTRYPOINT []

# Use shell form of CMD
CMD ["/bin/sh", "-c", "git clone https://github.com/tronprotocol/protocol.git && \
    cp -rf protocol/* ./ && \
    rm -rf protocol && \
    buf generate --include-imports && \
    rm -rf prost/_* && \
    rm -rf prost/google*"]