FROM tomcat:9.0.89-jdk17

# Define arguments for the group and user IDs
ARG USER_ID
ARG GROUP_ID
ARG USERNAME
ARG GROUPNAME

# Create group and user with IDs passed from Docker Compose
RUN groupadd -g ${GROUP_ID} ${GROUPNAME} && \
    useradd --no-log-init -u ${USER_ID} -g ${GROUP_ID} -m ${USERNAME} || true

# Install necessary packages and clean up
RUN apt-get update && \
    apt-get install -y gosu libreoffice && \
    apt-get clean && \
	rm -rf /var/lib/apt/lists/*

# CONFIG
COPY ./conf/catalina.properties /usr/local/tomcat/conf/catalina.properties
COPY ./conf/context.xml /usr/local/tomcat/conf/context.xml
COPY ./conf/server.xml /usr/local/tomcat/conf/server.xml

# JDBC
COPY ./libs/ /usr/local/tomcat/lib/

# Set permissions on Tomcat directories
RUN chown -R ${USERNAME}:${GROUPNAME} /usr/local/tomcat

# Ensure the required dconf cache directory exists and has the correct permissions (per libreoffice)
RUN mkdir -p /home/${USERNAME}/.cache/dconf && \
    chown -R ${USERNAME}:${GROUPNAME} /home/${USERNAME}/.cache/dconf

# Copy entrypoint script and make it executable
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

USER ${USERNAME}

CMD ["catalina.sh", "run"]