FROM node:18.13.0-buster

RUN /bin/cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
    echo "Asia/Tokyo" > /etc/timezone

# Options for setup script
ARG INSTALL_ZSH="true"
ARG UPGRADE_PACKAGES="false"
ARG USERNAME=vscode
# 1000 はnodeで使われているためずらす
ARG USER_UID=1001
ARG USER_GID=$USER_UID

# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
COPY library-scripts/common-debian.sh /tmp/library-scripts/
RUN bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
    && apt-get install default-jre -y \
    && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts

# COPY --from=golang:1.18-buster /usr/local/go/ /usr/local/go/
ENV GO111MODULE=auto
COPY library-scripts/go-debian.sh /tmp/library-scripts/
RUN bash /tmp/library-scripts/go-debian.sh "1.18" "/usr/local/go" "${GOPATH}" "${USERNAME}" "false" \
    && apt-get clean -y && rm -rf /tmp/library-scripts
ENV PATH="/usr/local/go/bin:${PATH}"
RUN mkdir -p /tmp/gotools \
    && cd /tmp/gotools \
    && export GOPATH=/tmp/gotools \
    && export GOCACHE=/tmp/gotools/cache \
    # sql-migrate
    && go install github.com/rubenv/sql-migrate/sql-migrate@v1.1.2 \
    && mv /tmp/gotools/bin/* ${TARGET_GOPATH}/bin/ \
    && rm -rf /tmp/gotools 

# Install NestJS
RUN npm i -g @nestjs/cli

# Copy mysql conf
COPY db/conf/my.cnf /etc/mysql/conf.d/

# 以下 ユーザー権限で実施
USER $USERNAME
# copy init-script
COPY --chown=$USERNAME:$USERNAME init.sh /home/${USERNAME}/
RUN chmod +x /home/${USERNAME}/init.sh

# 初期化を行う
# node imageのデフォルトENTRYPOINTが邪魔するため上書き
ENTRYPOINT /home/vscode/init.sh
