# =============================================================================
# Dockerfile - IBM Rational License Key Server (RLKS) 9.0
# Silent build using IBM Installation Manager + pre-prepared response file
# =============================================================================
# Build context must contain:
#   - IM.zip               (IBM Installation Manager installer ZIP)
#   - RLKS.zip             (RLKS 9.0 repository ZIP)
#   - rlks_response.xml    (prepared response file - generate interactively first)
#
# Build command (Apple Silicon / M-series Macs):
#   docker build --platform linux/amd64 \
#     --build-arg RESPONSE_FILE=rlks_response.xml \
#     -t rlks-9.0:latest .
#
# This Dockerfile:
#   1. Installs minimal runtime dependencies + redhat-lsb-core (added for LSB compatibility)
#   2. Silently installs IBM Installation Manager (using installc)
#   3. Extracts RLKS repository
#   4. Silently installs RLKS using IBMIM -silent + response file
#   5. Configures non-root user and lmgrd entrypoint
# =============================================================================

FROM rockylinux:8

LABEL maintainer="rob.little@mckesson.com" \
      description="IBM Rational License Key Server 9.0 (unofficial container)" \
      version="9.0"

# -----------------------------------------------------------------------------
# Step 1: Install runtime dependencies
# -----------------------------------------------------------------------------
# - findutils: required for dynamic path discovery (find command)
# - redhat-lsb-core: added for Linux Standard Base compatibility (required by some IBM installers)
# - No .i686 compat libs - RLKS 9.0 runs fine on pure 64-bit in containers
# -----------------------------------------------------------------------------
RUN yum update -y && \
    yum install -y epel-release findutils redhat-lsb-core && \
    yum install -y \
        iproute \
        python3 \
        python3-pip \
        hostname \
        inotify-tools \
        which \
        jq \
        unzip \
        wget \
        tar \
        libX11 libXext libXrender libXtst libXi \
        fontconfig freetype && \
    yum clean all && \
    rm -rf /var/cache/yum

# -----------------------------------------------------------------------------
# Build arguments
# -----------------------------------------------------------------------------
# RESPONSE_FILE: Name of your prepared response file (default: rlks_response.xml)
# -----------------------------------------------------------------------------
ARG RESPONSE_FILE=rlks_response.xml

# Copy all required files into the image
COPY IM.zip           /tmp/im.zip
COPY RLKS.zip         /tmp/rlks.zip
COPY ${RESPONSE_FILE} /tmp/rlks_response.xml

# -----------------------------------------------------------------------------
# Step 2: Silently install IBM Installation Manager
#   - Creates dedicated extraction dir to avoid path conflicts
#   - Dynamically finds the installc binary (handles any folder name/version)
#   - Uses installc (confirmed for your RLKS 9.0 ZIP)
# -----------------------------------------------------------------------------
RUN set -eux && \
    mkdir -p /tmp/im_extract && \
    cd /tmp && \
    unzip im.zip -d im_extract && \
    # Find the directory containing installc (RLKS 9.0 installer binary)
    INSTALLER_DIR=$(find im_extract -type f -name "installc" -not -path "*/.*" -printf '%h\n' | head -n 1) && \
    if [ -z "$INSTALLER_DIR" ]; then echo "ERROR: Could not find 'installc' binary in IM ZIP"; exit 1; fi && \
    cd "$INSTALLER_DIR" && \
    chmod +x installc && \
    ./installc \
        -acceptLicense \
        -installationDirectory /opt/IBM/InstallationManager \
        -silent \
        -dataLocation /opt/IBM/IMData && \
    rm -rf /tmp/im* && \
    echo "IBM Installation Manager installed at /opt/IBM/InstallationManager"

# -----------------------------------------------------------------------------
# Step 3: Move response file to persistent location
#   - Ensures it's available for RLKS install step
# -----------------------------------------------------------------------------
RUN mv /tmp/rlks_response.xml /opt/rlks_response.xml

# -----------------------------------------------------------------------------
# Step 4: Extract RLKS repository
#   - Safe extraction + dynamic find for LKSSERVER_LINUX folder
# -----------------------------------------------------------------------------
RUN set -eux && \
    mkdir -p /tmp/rlks_extract && \
    cd /tmp && \
    unzip rlks.zip -d rlks_extract && \
    # Find the LKSSERVER_LINUX directory (handles any nesting)
    RLKS_DIR=$(find rlks_extract -type d -name "LKSSERVER_LINUX" -print -quit) && \
    if [ -z "$RLKS_DIR" ]; then echo "ERROR: Could not find LKSSERVER_LINUX in RLKS ZIP"; exit 1; fi && \
    mv "$RLKS_DIR" /opt/rlks-repository && \
    rm -rf /tmp/rlks* && \
    echo "RLKS repository extracted to /opt/rlks-repository"

# -----------------------------------------------------------------------------
# Step 5: Silent install RLKS using IBMIM launcher
#   - Preferred method for response-file silent installs
#   - Automatically uses repository and package info from response file
#   - No need for explicit package ID
# -----------------------------------------------------------------------------
RUN set -eux && \
    /opt/IBM/InstallationManager/eclipse/IBMIM \
        -silent \
        -acceptLicense \
        -input /opt/rlks_response.xml \
        -log /tmp/rlks_install.log && \
    echo "RLKS installation completed (via IBMIM -silent). Last 20 lines of log:" && \
    tail -n 20 /tmp/rlks_install.log && \
    rm -f /opt/rlks_response.xml

# -----------------------------------------------------------------------------
# Step 6: Security - Switch to non-root user
# -----------------------------------------------------------------------------
RUN useradd -m -r rlksuser && \
    chown -R rlksuser:rlksuser \
        /opt/IBM \
        /opt/rlks-repository \
        /tmp && \
    mkdir -p /licenses && \
    chown rlksuser:rlksuser /licenses

USER rlksuser

# -----------------------------------------------------------------------------
# Runtime configuration
# -----------------------------------------------------------------------------
EXPOSE 27000 27001
VOLUME ["/licenses"]

# Start lmgrd (expects license.dat mounted at /licenses/license.dat)
ENTRYPOINT ["/opt/IBM/RationalRLKS/bin/lmgrd"]
CMD ["-c", "/licenses/license.dat", "-l", "/tmp/lmgrd.log", "-z"]

