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

PortSystem              1.0
PortGroup               makefile 1.0
PortGroup               compiler_blacklist_versions 1.0
PortGroup               perl5 1.0
PortGroup               texlive 1.0

name                    auto-multiple-choice
categories              x11 tex education
platforms               darwin
license                 GPL-2+
maintainers             nomaintainer

description             Auto Multiple Choice - Multiple Choice Papers Management
long_description        Utility to manage multiple choice questionnaires, \
                        with optionally mixed questions and answers. \
                        AMC provides automatic marking from papers' scans. \
                        Annotated papers are produced. Marks can be exported \
                        as an LibreOffice.org sheet file.

homepage                http://www.auto-multiple-choice.net/

perl5.branches          5.34

subport auto-multiple-choice-devel {}

if {${subport} eq ${name}} {
    # release
    set amc.version.main        1.6.0
    set amc.version.secondary   20230206142200
    set amc.version         ${amc.version.main}
    version                 ${amc.version.main}
    revision                2
    checksums               rmd160  00788859b8619b45ee1c19f702ea1295d7d3e445 \
                            sha256  acd9d266ebb8104543702fae52f383ed6170e7e7e1c90bc9c6abc4e83e3734ba \
                            size    13419428

    conflicts               auto-multiple-choice-devel

} else {
    # devel
    set amc.version.main        1.6.0
    set amc.version.secondary   20240317102301
    set amc.version         ${amc.version.main}+git${amc.version.secondary}
    version                 ${amc.version.main}-git${amc.version.secondary}
    checksums               rmd160  718b9e95615a49b07aa42a3d7ba5af387ead1b7c \
                            sha256  143a05d354d5c7a68ae9dc78e7d3344336e48d524251c5130e96bf69cc9314e2 \
                            size    15155724

    conflicts               auto-multiple-choice
}
distname                ${name}_${amc.version}_dist
worksrcdir              ${name}-${amc.version}


set opencv_ver          4
depends_lib-append      path:lib/opencv${opencv_ver}/libopencv_core.dylib:opencv${opencv_ver}
build.env-append        PKG_CONFIG_PATH=${prefix}/lib/opencv${opencv_ver}/pkgconfig

master_sites            https://download.auto-multiple-choice.net/precomp/

use_configure           no

compiler.cxx_standard   2011

# In file included from AMC-detect.cc:40:
# In file included from /opt/local/include/opencv4/opencv2/core/core.hpp:48:
# In file included from /opt/local/include/opencv4/opencv2/core.hpp:52:
# /opt/local/include/opencv4/opencv2/core/cvdef.h:738:10: fatal error: 'array' file not found
# #include <array>
#          ^
compiler.blacklist-append \
                        {clang < 900}

depends_build           port:gmake
build.cmd               ${prefix}/bin/gmake
build.args              AMCCONF=macports \
                        BASEPATH=${prefix} \
                        GCC="${configure.cxx}" \
                        GCC_PP="${configure.cxx}"

depends_run             \
                        path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                        port:libnotify \
                        path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                        port:netpbm \
                        port:p${perl5.major}-archive-zip \
                        port:p${perl5.major}-clone \
                        port:p${perl5.major}-dbd-sqlite \
                        port:p${perl5.major}-digest-md5 \
                        port:p${perl5.major}-email-mime \
                        port:p${perl5.major}-email-sender \
                        port:p${perl5.major}-file-basedir \
                        port:p${perl5.major}-file-mimeinfo \
                        port:p${perl5.major}-filesys-dfportable \
                        port:p${perl5.major}-glib-object-introspection \
                        port:p${perl5.major}-gtk3 \
                        port:p${perl5.major}-hash-merge \
                        port:p${perl5.major}-io-compress \
                        port:p${perl5.major}-locale-codes \
                        port:p${perl5.major}-locale-gettext \
                        port:p${perl5.major}-module-load-conditional \
                        port:p${perl5.major}-openoffice-oodoc \
                        port:p${perl5.major}-perlmagick \
                        port:p${perl5.major}-text-csv \
                        port:p${perl5.major}-xml-simple \
                        port:p${perl5.major}-xml-writer \
                        port:p${perl5.major}-yaml-syck \
                        path:lib/pkgconfig/poppler.pc:poppler \
                        port:qpdf \
                        port:sqlite3 \
                        port:texlive \
                        port:texlive-fonts-extra \
                        port:texlive-lang-japanese \
                        port:texlive-latex-extra \
                        port:unzip

pre-build {
    build.post_args-append PERLDIR=${perl5.lib}
    if {![variant_isset mactex]} {
        set amc.texmflocal ${texlive_texmflocal}
    }
    build.post_args-append TEXDIR="${amc.texmflocal}/tex/latex/AMC"
    build.post_args-append TEXDOCDIR="${amc.texmflocal}/doc/latex/AMC"
}

destroot.args           AMCCONF=macports \
                        BASEPATH=${prefix}

pre-destroot {
    destroot.args-append PERLDIR=${perl5.lib}
    if {![variant_isset mactex]} {
        set amc.texmflocal ${texlive_texmflocal}
    }
    destroot.args-append TEXDIR="${amc.texmflocal}/tex/latex/AMC"
    destroot.args-append TEXDOCDIR="${amc.texmflocal}/doc/latex/AMC"
}

# The mactex variant expects MacTeX to be installed
# and installs dblatex's stylefiles to MacTeX's texmf (local)
set amc.texmflocal ""
set amc.mactex_bin ""

variant mactex description {The mactex variant allow Auto Multiple Choice to use a MacTeX installation \
instead of Macports texlive tools. To install the mactex variant \
/Library/TeX/texbin must be added to binpath in macports.conf.} {
    # First, check if MacTeX actually seems to be there…
    set amc.mactex_candidates { \
       "/Library/TeX/texbin" \
       "/usr/texbin" \
    }
    foreach dir ${amc.mactex_candidates} {
        if {[file executable "${dir}/kpsewhich"]} {
            set amc.mactex_bin ${dir}
            break
        }
    }
    if { ${amc.mactex_bin} != "" } {
        set amc.texmflocal \
            [exec ${amc.mactex_bin}/kpsewhich --expand-var='\$TEXMFLOCAL']
        regsub -all {'} ${amc.texmflocal} "" amc.texmflocal
    } else {
        return -code error "Cannot find MacTeX installation; aborting"
    }

    depends_run-delete      \
                            port:texlive \
                            port:texlive-fonts-extra \
                            port:texlive-lang-japanese \
                            port:texlive-latex-extra

    notes "The mactex variant will install styles to MacTeX's texmf-local
    ${amc.texmflocal}
(which is outside macport's common directory structure).
For the mactex variant to work,
${amc.mactex_bin} must also be added to binpath in macports.conf"

    # AND, since we're installing files outside macports' normal directories
    destroot.violate_mtree  yes
}

proc amc.mktexlsr {} {
    if {![variant_isset mactex]} {
        texlive.mktexlsr
    } else {
        global amc.mactex_bin
        system "${amc.mactex_bin}/mktexlsr"
        if {[file exists "${amc.mactex_bin}/mtxrun"]} {
            system "${amc.mactex_bin}/mtxrun --generate"
        }
    }
}

post-activate {
    amc.mktexlsr
}

post-deactivate {
    amc.mktexlsr
}

livecheck.type          regex
livecheck.url           [lindex ${master_sites} 0]#
livecheck.regex         ${name}_(\[0-9.\]+\\+\[0-9.a-z-\]+)_dist.tar.gz
livecheck.version       ${amc.version}
