# ==============================================================================
# APPLICATION TIER: RESIN APPLICATION SERVER COMPILATION BLUEPRINT (Dockerfile)
# ==============================================================================
# ARCHITECTURE INTENTION:
# This Dockerfile builds an immutable enterprise application container image running
# Resin AP Server on AlmaLinux 9. It encapsulates OS dependencies, Java runtimes,
# and system compilation wrappers away from the daily application layer.
#
# DESIGN ADVANTAGES & AUTOMATION AUTOMATION:
# 1. Zero-Code Version Upgrades: Target source folders (${RESIN_SERVER_NAME}) and 
#    JDBC dependencies are injected as decoupled build arguments. To upgrade versions,
#    developers shift targets in the external '.env' control panel without altering 
#    this structural code.
# 2. Complete Traceability: Network proxies, localization configurations, and memory 
#    limits are entirely automated during the container building lifecycle.
# ==============================================================================

FROM almalinux:9

# ------------------------------------------------------------------------------
# 1. BUILD ARGUMENTS (Injected from docker-compose.yml via .env)
# ------------------------------------------------------------------------------
ARG arg_resin_guest_port
ARG arg_resin_server_name
ARG arg_jdbc_file_name
ARG arg_jdbc_download_url
ARG arg_im_java_home
ARG arg_resin_home
ARG arg_container_jdk
ARG arg_http_proxy
ARG arg_https_proxy

# ------------------------------------------------------------------------------
# 2. RUNTIME ENVIRONMENT CONFIGURATION & PROXY TUNNELING
# ------------------------------------------------------------------------------
ENV http_proxy=$arg_http_proxy
ENV HTTP_PROXY=$arg_http_proxy
ENV https_proxy=$arg_https_proxy
ENV HTTPS_PROXY=$arg_https_proxy
ENV CONTAINER_JDK="${arg_container_jdk}"

# Defines the targeted application source directory to compile
ENV RESIN_SERVER_NAME="${arg_resin_server_name}"
# Establishes the static installation home directory for Resin inside the container
ENV RESIN_HOME="${arg_resin_home}"
RUN export RESIN_HOME=${RESIN_HOME}

# ------------------------------------------------------------------------------
# 3. SYSTEM PACKAGE PROVISIONING & LOCALIZATION
# ------------------------------------------------------------------------------
# Updates OS repositories and fetches mandatory development compilation tooling
RUN dnf update -y && \
    dnf install -y ${CONTAINER_JDK} "${CONTAINER_JDK}-devel" && \
    dnf install -y gcc glibc-devel make openssl openssl-devel which tzdata wget && \
    ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

# Dynamic resolution of the specific OpenJDK home directory path
RUN JAVA_HOME=${arg_im_java_home}/`ls /usr/lib/jvm/ | grep ${CONTAINER_JDK}` && \
    echo "Resolved JAVA_HOME to: $JAVA_HOME"
ENV PATH="$JAVA_HOME/bin:$PATH"
ENV CLASS_PATH="$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar"

# ------------------------------------------------------------------------------
# 4. SOURCE CODE BUNDLING & EXTENSION INJECTION
# ------------------------------------------------------------------------------
# Selectively bundles the designated resin-pro folder name defined in .env
COPY "./${RESIN_SERVER_NAME}/" "$RESIN_HOME"

# Automates the retrieval of the specialized PostgreSQL engine relational driver
WORKDIR $RESIN_HOME/lib
RUN wget "${arg_jdbc_download_url}/${arg_jdbc_file_name}" || \
    (echo "Warning: Failed to download JDBC driver" && exit 0)

# ------------------------------------------------------------------------------
# 5. RESIN COMPILATION & NATIVE RUNTIME INSTALLATION
# ------------------------------------------------------------------------------
WORKDIR $RESIN_HOME
RUN chmod 755 "$RESIN_HOME/configure"
RUN $RESIN_HOME/configure --prefix=$RESIN_HOME --enable-64bit && \
    make && \
    make install

# Exposes the application communication ports to the localized network switch
EXPOSE ${RESIN_GUEST_PORT}
EXPOSE 8080

# ------------------------------------------------------------------------------
# 6. APPLICATION WAR DEPLOYMENT POLICY ARCHITECTURE NOTE
# ------------------------------------------------------------------------------
# DEPLOYMENT STRATEGY NOTICE:
# Developers have two alternative operational mechanics to deploy the 'imart.war':
#
# METHOD A (DevOps Best Practice - Default CLI Dynamic Deploy):
# Keep the line below commented out. Run the container as ephemeral infrastructure, 
# and stream the war file into the running environment using the following command:
# `docker compose exec ap /ap-server/bin/resinctl deploy /war/imart.war`
# This isolates binary modifications from the static container image state.
#
# METHOD B (Immutable Static Image Bundle):
# Uncomment the line below to hard-bake 'imart.war' directly into the Docker image 
# during compilation time. Use this when shipping independent, pre-packaged builds.
#
# COPY "./war/imart.war" "$RESIN_HOME/webapps/imart.war"

# ------------------------------------------------------------------------------
# 7. SERVICE ENTRYPOINT CONSOLE EXECUTION
# ------------------------------------------------------------------------------
CMD ["./bin/resinctl", "-server", "app-0", "start-with-foreground"]