# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               github 1.0
PortGroup               cmake 1.1
PortGroup               boost 1.0
PortGroup               active_variants 1.1

github.setup            htcondor htcondor 8_8_1 V
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
version                 [strsed ${github.version} g/_/\./]
revision                3
maintainers             {aronnax @lpsinger}

categories              science parallel net
license                 apache
platforms               darwin
description \
  specialized workload management system for compute-intensive jobs
long_description \
  HTCondor is a specialized workload management system for compute-intensive \
  jobs. Like other full-featured batch systems, HTCondor provides a job \
  queueing mechanism, scheduling policy, priority scheme, resource monitoring, \
  and resource management. Users submit their serial or parallel jobs to \
  HTCondor, HTCondor places them into a queue, chooses when and where to run \
  the jobs based upon a policy, carefully monitors their progress, and \
  ultimately informs the user upon completion.
homepage                https://research.cs.wisc.edu/htcondor

default_variants        +personal +kerberos

variant personal \
  description "configure your computer as a one-node personal Condor pool" {

    add_users           condor group=condor

    startupitem.create  yes
    startupitem.name    ${name}
    startupitem.start   "${prefix}/sbin/condor_master -f"
    startupitem.stop    "${prefix}/sbin/condor_off -all -master"
}

set pythons_suffixes {27 38 39}

if { !([variant_isset python27] || [variant_isset python38] || [variant_isset python39]) } {
    default_variants +python39
}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

compiler.cxx_standard 2011

cmake.out_of_source yes
configure.args-append   -DWITH_PYTHON_BINDINGS:BOOL="0" -DWITH_KRB5:BOOL="0"

set sel_py ""
foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string range ${s} 1 end]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    set f ${frameworks_dir}/Python.framework/Versions/${v}
    if {${s} == 27} {
        set m ""
    } else {
        set m 3
    }
    variant ${p} description "Enable Python interface for Python ${v}" conflicts {*}${c} "

        depends_lib-append      port:${p}
        require_active_variants [boost::depends_portname] ${p}
        configure.args-append   -DPython_ADDITIONAL_VERSIONS:STRING=\"${v}\" -DPYTHON_EXECUTABLE:FILEPATH=\"${f}/bin/python${v}\" -DBoost_PYTHON_LIBRARY:FILEPATH=\"[boost::lib_dir]/libboost_python${m}-mt.dylib\"
        configure.args-replace  -DWITH_PYTHON_BINDINGS:BOOL=\"0\" -DWITH_PYTHON_BINDINGS:BOOL=\"1\"

        post-patch {
            reinplace \"s|@prefix@|${prefix}|g\" ${worksrcpath}/src/condor_utils/condor_config.cpp
            reinplace \"s|@c_pythonarchlib@|Library/Frameworks/Python.framework/Versions/${v}/lib/python${v}/site-packages|g\" ${worksrcpath}/build/cmake/CondorPackageConfig.cmake
        }
    "
    if {[variant_isset ${p}]} {
        set sel_py ${v}
    }
}

variant kerberos description {enable Kerberos5 support} {
    depends_lib-append port:kerberos5
    configure.args-replace -DWITH_KRB5:BOOL="0" -DWITH_KRB5:BOOL="1"
}


checksums               rmd160  53f076464a125c3574b25a3df1cb36d1bdd9bc34 \
                        sha256  abab3e8fe39bcebe68ea6efb60438f88f51bf9e2d345d8a1f061b6584440cc1d \
                        size    13135083

depends_build-append    port:fig2dev \
                        port:latex2html

depends_lib-append      port:expat \
                        path:lib/libssl.dylib:openssl \
                        port:pcre

license_noconflict      latex2html

patchfiles              patch-build-cmake-CondorPackageConfig.cmake.diff \
                        patch-external-bundles-openssl-1.0.1e-CMakeLists.txt.diff \
                        patch-src-classad-CMakeLists.txt.diff \
                        patch-src-condor_scripts-macosx_rewrite_libs.diff \
                        patch-src-condor_utils-CMakeLists.txt.diff \
                        patch-src-condor_utils-condor_config.cpp.diff \
                        patch-src-python-bindings-CMakeLists.txt.diff

post-patch {
    reinplace "s|@prefix@|${prefix}|g" ${worksrcpath}/src/condor_utils/condor_config.cpp ${worksrcpath}/src/condor_scripts/macosx_rewrite_libs ${worksrcpath}/doc/Makefile
    reinplace "s|g\+\+|${configure.cxx}|g" ${worksrcpath}/doc/makeman/Makefile

    # This .sty file seems to be available for latex2html but not pdftex,
    # and the "make just-man-pages" target seems to expect it.
    ln ${prefix}/share/texmf/tex/latex/html/html.sty ${worksrcpath}/doc/
}

# FIXME: Globus support is disabled because Globus is not available in MacPorts.
configure.args-append   -DWITH_GLOBUS:BOOL="0" -DWITH_VOMS:BOOL="0" -DBUILD_TESTING:BOOL="0"

# Man pages are not part of the default build target.
post-build {
    system -W ${worksrcpath}/doc "make just-man-pages"
}

# These are empty directories that are needed by the personal Condor pool.
destroot.keepdirs       ${destroot}${prefix}/etc/condor/config.d \
                        ${destroot}${prefix}/var/lib/condor/execute \
                        ${destroot}${prefix}/var/lib/condor/spool \
                        ${destroot}${prefix}/var/log/condor

# Condor installs some documentation into ${prefix}. Move them into
# ${prefix}/share/doc/${name}.
post-destroot {
    # Move manpages into place.
    xinstall {*}[glob ${worksrcpath}/doc/man/man1/*] \
        ${destroot}${prefix}/share/man/man1

    # Remove SysV-init and Fedora-style service scripts.
    # See https://htcondor-wiki.cs.wisc.edu/index.cgi/tktview?tn=3684
    file delete ${destroot}${prefix}/etc/init.d/condor
    file delete ${destroot}${prefix}/etc/sysconfig/condor

    # Remove packaging scripts.
    file delete ${destroot}${prefix}/sbin/condor_configure
    file delete ${destroot}${prefix}/sbin/condor_install

    # Move documentation into ${prefix}/share.
    file mkdir ${destroot}${prefix}/share/doc/${name}
    move \
      ${destroot}${prefix}/examples \
      ${destroot}${prefix}/LICENSE-2.0.txt \
      ${destroot}${prefix}/README \
      ${destroot}${prefix}/share/doc/${name}

    # Move example configuration files from ${prefix}/etc to ${prefix}/share.
    move \
      ${destroot}${prefix}/etc/examples \
      ${destroot}${prefix}/share/doc/${name}/examples/config

    # If deploying a personal Condor pool, create all necessary directories.
    if {[variant_isset personal]} {
        xinstall -d ${destroot}${prefix}/etc/condor/config.d
        xinstall ${filespath}/condor_config ${destroot}${prefix}/etc/condor
        reinplace "s|@prefix@|${prefix}|g" ${destroot}${prefix}/etc/condor/condor_config
        xinstall ${filespath}/condor_config.local ${destroot}${prefix}/etc/condor

        xinstall -d ${destroot}${prefix}/var/lib
        xinstall -d -o condor -g condor ${destroot}${prefix}/var/lib/condor/execute
        xinstall -d -o condor -g condor ${destroot}${prefix}/var/lib/condor/spool
        xinstall -d ${destroot}${prefix}/var/log
        xinstall -d -o condor -g condor ${destroot}${prefix}/var/log/condor
    }

    # Fix linkage. Appears to be a bug in the configuration as it
    # Should use the configured install area for boost.
    set sel_py_nodot [string map {. {}} ${sel_py}]
    set oLib ${prefix}/lib/libboost_python${sel_py_nodot}-mt.dylib
    set nLib [boost::lib_dir]/libboost_python${sel_py_nodot}-mt.dylib
    foreach lib [ exec find ${destroot}${prefix}/ -name "*.dylib" -or -name "*.so" ] {
        system "install_name_tool -change ${oLib} ${nLib} ${lib}"
    }
}

# The second of the three version numbers is even for a stable release.
# See http://research.cs.wisc.edu/htcondor/version_number.html
github.livecheck.regex  {(\d+_\d*[02468]_\d+)}
