ARG BUILDER_IMAGE=registry-dev.pgs.rtlabs.ru/ervu/micord-deps:0.0.1-sha288d758c
ARG RUNTIME_IMAGE=registry-dev.pgs.rtlabs.ru/basealt/altsp:c10f1

FROM $BUILDER_IMAGE AS builder

ARG MVN_FLAGS="-Pprod -DexecuteNpmInstall=false"

RUN rm -f /etc/apt/sources.list.d/* \
 && echo "rpm http://nexus-dev.pgs.rtlabs.ru repository/alt-c10f1-classic/x86_64 classic" > /etc/apt/sources.list \ 
 && echo "rpm http://nexus-dev.pgs.rtlabs.ru repository/alt-c10f1-classic/noarch classic" >> /etc/apt/sources.list \ 
 && apt-get update \
 && apt-get -y install git glibc-locales java-17-openjdk-devel maven node \
 && apt-get clean

ENV JAVA_HOME=/usr/lib/jvm/java
ENV LANG ru_RU.UTF-8
ENV LANGUAGE ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8

WORKDIR /app
COPY . .
#RUN mkdir -p $HOME/.m2 \
# && cp -f config/pgs-settings.xml $HOME/.m2/settings.xml \
# && cp -f config/pgs-npmrc frontend/.npmrc \
# && mvn clean package -T4C ${MVN_FLAGS}

#RUN ln -s /root/.npm /app/frontend/node_modules

RUN mvn clean \
 && mvn package -T4C ${MVN_FLAGS}


FROM $RUNTIME_IMAGE
ARG ADMIN_PASSWORD=Secr3t

COPY --from=builder /usr/lib/locale/ru_RU /usr/lib/locale/ru_RU
COPY --from=builder /usr/lib/locale/ru_RU.utf8 /usr/lib/locale/ru_RU.utf8
COPY --from=builder /usr/share/locale/ru_RU /usr/share/locale/ru_RU

COPY config/entrypoint.sh /entrypoint.sh

RUN rm -f /etc/apt/sources.list.d/* \
 && echo "rpm http://nexus-dev.pgs.rtlabs.ru repository/alt-c10f1-classic/x86_64 classic" > /etc/apt/sources.list \ 
 && echo "rpm http://nexus-dev.pgs.rtlabs.ru repository/alt-c10f1-classic/noarch classic" >> /etc/apt/sources.list \ 
 && apt-get update \
 && apt-get -y install java-17-openjdk-headless tomcat tomcat-webapps tomcat-admin-webapps \
 && apt-get clean \
 && rm -f /var/cache/apt/*.bin \
 && rm -f /var/lib/apt/lists/update* \
 && chmod +x /entrypoint.sh

ENV JAVA_HOME=/usr/lib/jvm/java
ENV LANG=ru_RU.UTF-8
ENV LANGUAGE=ru_RU.UTF-8
ENV LC_ALL=ru_RU.UTF-8

COPY config/tomcat /

RUN cat /etc/tomcat/webbpm.properties >> /etc/tomcat/catalina.properties \
 && sed -i -r "s/<must-be-changed>/$ADMIN_PASSWORD/g" /etc/tomcat/tomcat-users.xml \
 && chown -R tomcat:tomcat /var/lib/tomcat/webapps

COPY --from=builder /app/backend/target/dashboard*.war /var/lib/tomcat/webapps/dashboard.war

USER tomcat

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]
