#!/usr/bin/env modbuild
set -e

pbuild::add_to_group 'EM'


pbuild::prep() {
    : ${CUDA_VERSION:?No CUDA_VERSION set}
    local cuda="${CUDA_VERSION%.*}"
    local filename="imod_${V_PKG}_RHEL7-64_CUDA${cuda}.sh"
    if [[ $V_RELEASE == *beta* ]]; then
        local url="https://bio3d.colorado.edu/ftp/latestIMOD/RHEL7-64_CUDA${cuda}/$filename"
    else
        local url="https://bio3d.colorado.edu/imod/AMD64-RHEL5/$filename"
    fi
    local dst="$PMODULES_DISTFILESDIR/${filename}"
    if [ -f "$dst" ]; then
        echo "Found cached $dst" >&2
    else
        ${curl:-curl} \
            --location \
            --fail \
            --output "$dst" \
            "$url"
    fi
    }

pbuild::configure() {
    :
}

pbuild::compile() {
    :
}

pbuild::install() {
    : ${CUDA_VERSION:?No CUDA_VERSION set}
    local cuda="${CUDA_VERSION%.*}"
    local filename="imod_${V_PKG}_RHEL7-64_CUDA${cuda}.sh"
    local dst="$PMODULES_DISTFILESDIR/${filename}"

    mkdir -p "$PREFIX/profile.d"
    sh "$dst" \
        -yes \
        -dir "$PREFIX" \
        -script "$PREFIX/profile.d"

}

