ARG BUILDER_IMAGE=registry.altlinux.org/basealt/altsp:c10f1
ARG RUNTIME_IMAGE=registry.altlinux.org/basealt/altsp:c10f1

FROM $BUILDER_IMAGE AS builder

ARG MVN_FLAGS="-Pprod"

RUN apt-get update \
 && apt-get -y install git glibc-locales java-17-openjdk-devel maven node \
 && apt-get clean

WORKDIR /app

COPY . .

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

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

FROM $RUNTIME_IMAGE
ARG ADMIN_PASSWORD=Secr3t

COPY config/entrypoint.sh /entrypoint.sh

RUN apt-get update \
 && apt-get -y install glibc-locales java-17-openjdk-headless mc \
                       tomcat tomcat-admin-webapps \
 && apt-get clean \
 && chmod +x /entrypoint.sh

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 root:tomcat /var/lib/tomcat/webapps \
 && chmod g+rw /var/lib/tomcat/webapps

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

USER tomcat

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]
