# ---------------------------------------------------------------------------
#
# Create Ubuntu-based ESI OpenFOAM image
#
# Build
#   apptainer build of.sif com-openfoam.def
#
# Note
#   apptainer version 1.3.1
#
# ---------------------------------------------------------------------------
Bootstrap: localimage
From: containers/basic/{{ BASE_CONTAINER }}.sif

%arguments
    OS_DISTRO=ubuntu
    OS_VERSION=24.04
    MPI_IMPLEMENTATION=openmpi
    MPI_VERSION=4.1.5
    FRAMEWORK_VERSION=2312
    FRAMEWORK_GIT_REF=default

%post -c /bin/bash
    DEBIAN_FRONTEND=noninteractive
    apt-get update
    if [[ {{ FRAMEWORK_GIT_REF }} == "default" ]]; then
        curl https://dl.openfoam.com/add-debian-repo.sh | bash
        apt-get -y install --no-install-recommends openfoam{{ FRAMEWORK_VERSION }}-dev python3
    else
        apt-get install -y --no-install-recommends \
            flex libfl-dev libreadline-dev zlib1g-dev libgmp-dev libmpfr-dev libmpc-dev \
            libfftw3-dev libscotch-dev libptscotch-dev libboost-system-dev libboost-thread-dev libcgal-dev
        mkdir -p /usr/lib/openfoam
        git clone https://develop.openfoam.com/Development/openfoam /usr/lib/openfoam/openfoam{{ FRAMEWORK_VERSION }}
        cd /usr/lib/openfoam/openfoam{{ FRAMEWORK_VERSION }}
        git checkout {{ FRAMEWORK_GIT_REF }}
        ./Allwmake -j -s -q -l
    fi
    jq --arg app openfoam --arg commit {{ FRAMEWORK_GIT_REF }} \
        --arg branch {{ FRAMEWORK_GIT_REF }} \
        '.[$app] |= if . == null then
        {
            fork: "com-openfoam",
            branch: $branch,
            commit: $commit,
            source_script: "/usr/lib/openfoam/openfoam{{ FRAMEWORK_VERSION }}/etc/bashrc",
            version: "{{ FRAMEWORK_VERSION }}"
        }
        else . +
        {
            fork: "com-openfoam",
            branch: $branch,
            commit: $commit,
            source_script: "/usr/lib/openfoam/openfoam{{ FRAMEWORK_VERSION }}/etc/bashrc",
            version: "{{ FRAMEWORK_VERSION }}"
        } end' /apps.json > /tmp/apps.json
    mv /tmp/apps.json /apps.json

%runscript
    #!/bin/bash
    if [ $# -eq 0 ]; then
        exec /bin/bash
    else
        exec "$@"
    fi

%labels
    Maintainer Mohammed Elwardi Fadeli
    Description Ubuntu-based ESI OpenFOAM image with OpenMPI
    AppsFile /apps.json
