FROM maven:3-openjdk-17-slim AS build
RUN apt update \
  && apt upgrade -y \
  && curl -fsSL https://deb.nodesource.com/setup_14.x | bash - \
  && apt install -y git nodejs \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . .
RUN mvn clean && mvn package -T4C

FROM tomee:8.0.15-jre17-webprofile
ARG ADMIN_PASSWORD=Secr3t

COPY config/tomcat/tomee /usr/local/tomee

RUN rm -rf /usr/local/tomee/webapps/ROOT \
 && cat /usr/local/tomee/conf/webbpm.properties >> /usr/local/tomee/conf/catalina.properties \
 && sed -i -r "s/<must-be-changed>/$ADMIN_PASSWORD/g" /usr/local/tomee/conf/tomcat-users.xml

COPY --from=build /app/frontend/target/frontend*.war /usr/local/tomee/webapps/ROOT.war
COPY --from=build /app/backend/target/dashboard*.war /usr/local/tomee/webapps/dashboard.war
