# -*- 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               perl5 1.0
PortGroup               texlive 1.0

name                    auto-multiple-choice
categories              x11 tex education
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.7.0
    set amc.version.secondary   20250417073838
    set amc.version         ${amc.version.main}
    version                 ${amc.version.main}
    checksums               rmd160  8886151969ace9a3693b840cd9fcb3aa3678ba28 \
                            sha256  95c08477ae41f1b2a98881b677b0dce03f70c4a7ab5ac440311091175d9c98f6 \
                            size    15449580

    conflicts               auto-multiple-choice-devel

} else {
    # devel
    set amc.version.main        1.7.0
    set amc.version.secondary   20250704122834
    set amc.version         ${amc.version.main}+git${amc.version.secondary}
    version                 ${amc.version.main}-git${amc.version.secondary}
    checksums               rmd160  0a1888c9d6af8f5053406f3d4ee2565cd66a873a \
                            sha256  ced16989fe2246d0bbc092792c3e09f5f2a38e72e4eab91e207503c3e0092370 \
                            size    15530591

    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-address \
                        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}
