# -*- 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
# $Id: Portfile 113984 2013-11-27 01:19:53Z aronnax@macports.org $

PortSystem              1.0
PortGroup               github 1.0
PortGroup               cmake 1.0
PortGroup               active_variants 1.1
PortGroup               conflicts_build 1.0

github.setup            htcondor htcondor 8_1_1 V
version                 [strsed ${github.version} g/_/\./]
maintainers             aronnax
revision                3

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                http://research.cs.wisc.edu/htcondor

default_variants        +personal +python27

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 {25 26 27 31 32 33}

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

conflicts_build     ${name}

configure.args-append   -DWITH_PYTHON_BINDINGS:BOOL="0"

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    set d ${frameworks_dir}/Python.framework/Versions/${v}/lib/python${v}
    eval [subst {
        variant ${p} description "Enable Python interface for Python ${v}" conflicts ${c} {

            depends_lib-append      port:${p}
            require_active_variants boost ${p}
            configure.args-append   -DC_PYTHONARCHLIB:PATH="${d}/site-packages"
            configure.args-strsed   s/-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@|${d}/site-packages|g" ${worksrcpath}/build/cmake/CondorPackageConfig.cmake
            }

        }
    }]
}

checksums               rmd160  87af615b5a2a5b347f281ddc06e508be94681766 \
                        sha256  6b299e52368de505e3391d72ef5482550103bc36f7f526da33d52303d8a9233e

depends_build-append    port:latex2html

depends_lib-append      port:boost \
                        port:expat \
                        port:kerberos5 \
                        port:openssl \
                        port:pcre

license_noconflict      latex2html

# FIXME: For some reason, Condor uses install_name_tool to have dynamic links
# point to @executable_path/../lib/condor/libexpat.1.dylib, etc, instead of
# @executable_path/../lib/libexpat.1.dylib, etc. Why?
patchfiles              patch-src-condor_scripts-macosx_rewrite_libs.diff \
                        patch-src-condor_utils-condor_config.cpp.diff \
                        patch-src-python-bindings-CMakeLists.txt.diff \
                        patch-build-cmake-CondorPackageConfig.cmake.diff

# Fix error on Lion:
# htcondor-8_1_1/src/classad/util.cpp:433:9: error: use of undeclared identifier 'finite'
patchfiles-append       patch-src-classad-util.cpp.diff

# makeman.C:182:26: error: no member named '_Ios_Fmtflags' in namespace 'std'
patchfiles-append       patch-doc-makeman-makeman.C.diff

# ar: -lc++: No such file or directory
if {${os.major} >= 11} {
    patchfiles-append   patch-build-cmake-CondorConfigure.cmake.diff
}

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

# FIXME: Globus support is disabled because Globus is not available in MacPorts.
configure.args-append   -DWITH_GLOBUS: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.
    eval 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
    }
}

# Override default GitHub livecheck because the HTCondor git repository has
# thousands of tags, and GitHub has to paginate the tags.
livecheck.type          regexm
livecheck.url           ${homepage}/downloads/
livecheck.version       ${version}
livecheck.regex         {Current Developer Release</td>\s*<td [^>]*>([0-9]+(\.[0-9]+)+)}
