# -*- 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               cmake 1.1
PortGroup               perl5 1.0
PortGroup               wxWidgets 1.0
PortGroup               active_variants 1.1
PortGroup               linear_algebra 1.0
PortGroup               conflicts_build 1.0

name                    hugin-app
version                 2025.0.1
revision                0

categories              graphics
license                 GPL-2
maintainers             {mcalhoun @MarcusCalhoun-Lopez} {mascguy @mascguy} openmaintainer
description             Panorama photo stitcher
long_description        An easy to use cross-platform panoramic imaging \
                        toolchain based on Panorama Tools. With hugin you \
                        can assemble a mosaic of photographs into a complete \
                        immersive panorama, stitch any series of overlapping \
                        pictures and much more.
homepage                http://hugin.sourceforge.io

set branch              [join [lrange [split ${version} .] 0 1] .]
master_sites            sourceforge:project/hugin/hugin/hugin-${branch}
distname                hugin-${version}
use_bzip2               yes

compiler.cxx_standard   2017

perl5.branches          5.34

platform darwin {
    wxWidgets.use   wxWidgets-3.2
}

checksums               rmd160  08bd10f46079071f21e67d6e06bba73d812a2d76 \
                        sha256  7cf8eb33a6a8848cc7f816faf4bc88389228883d5513136dccb5cb243912ab79 \
                        size    10229525

linalg.setup            noveclibfort

# Since there are multiple apps, group together in separate folder
set applications_dir    ${applications_dir}/Hugin

# Remove deployment target. Make detecting MacPorts more robust through flag to
# support non-standard install location.
patchfiles-append       patch-CMakeLists.txt.diff

# Fix icons which were not getting installed for PTBatcherGUI.app and
# HuginStitchProject.app.  No icon supplied for calibrate_lens_gui.app.
patchfiles-append       patch-icns.diff

# Corrects path for launching PTBatcherGUI.app from Hugin.app by removing
# MAC_SELF_CONTAINED_BUNDLE from #if condition.  Existing code seems to work.
patchfiles-append       patch-PanoPanel.cpp.diff

# Update search path in Hugin.app (i.e. for cpfind, etc.).
patchfiles-append       patch-huginApp.cpp.diff

# create usable version number for vigra
patchfiles-append       patch-FindVIGRA.cmake.diff

post-extract {
    # fix file perms; tarball contents deny world read
    system "find ${worksrcpath} -type d -print0 | xargs -0 chmod a+rx"
    system "find ${worksrcpath} -type f -print0 | xargs -0 chmod a+r"

    # icon files have strange permissions
    system "chmod 0644 ${worksrcpath}/src/hugin1/hugin/*.icns"

    # icon files are not found
    ln -s ../hugin/Hugin.icns        ${worksrcpath}/src/hugin1/stitch_project
    ln -s ../hugin/PTBatcherGui.icns ${worksrcpath}/src/hugin1/ptbatcher

    # use the cmake version of the following packages
    foreach cmakePackage "LAPACK GLUT GLEW JPEG PNG PackageHandleStandardArgs PkgConfig TIFF ZLIB OpenMP" {
        delete ${worksrcpath}/CMakeModules/Find${cmakePackage}.cmake
    }
}

post-patch {
    # In RunStichPanel.cpp adjust exe path. Solves problem with PTBatcherGUI.app
    # finding nona.  Otherwise path is set to bundle exe dir which only contains
    # PTBatcherGUI exe.
    reinplace \
        "s|exePath(wxStandardPaths::Get().GetExecutablePath())|exePath(\"${prefix}/bin/\")|" \
        ${worksrcpath}/src/hugin1/base_wx/RunStitchPanel.cpp

    # In hugin_executor.cpp adjust exe path. I am not sure what this affects
    # (didn't solve PTBatcherGUI nona problem) but similar to above patch.  Might
    # affect HuginStitchProject.app which I haven't used.
    reinplace \
        "s|exePath(wxStandardPaths::Get().GetExecutablePath())|exePath(\"${prefix}/bin/\")|" \
        ${worksrcpath}/src/hugin1/executor/hugin_executor.cpp

    # Adjust search path above if non-standard MacPorts install location.
    reinplace \
        "s|@@MACPORTS_BIN_PATH@@|${prefix}/bin|" \
        ${worksrcpath}/src/hugin1/hugin/huginApp.cpp

    # Adjust Applications path
    reinplace \
        "s|@@MACPORTS_APP_PATH@@|${applications_dir}|" \
        ${worksrcpath}/CMakeLists.txt
}

# do not find freeglut if installed
configure.pre_args-replace \
    -DCMAKE_FIND_FRAMEWORK=LAST \
    -DCMAKE_FIND_FRAMEWORK=FIRST
conflicts_build-append \
    freeglut

# see https://trac.macports.org/ticket/58484
configure.args-append \
    -DTIFF_INCLUDE_DIR=${prefix}/include

# Affects CMakeList.txt application bundle path.
configure.args-append \
    -DMACPORTS=1

# Affects huginApp.cpp adding code to setup environment path.
configure.cppflags-append \
    -DMACPORTS

# Reduce compilation warnings
configure.cppflags-append \
    -Wno-deprecated-declarations \
    -Wno-register

# support exiv 0.27 with c++17, when 0.28 is in MacPorts this
# can be dropped
configure.cppflags-append \
    -D_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR

variant openmp description {Use OpenMP for multi-threaded image processing} {
    compiler.openmp_version 2.5
}
default_variants-append +openmp

depends_build-append \
    path:bin/pkg-config:pkgconfig \
    port:gettext

# see http://wiki.panotools.org/Development_of_Open_Source_tools#Dependencies
depends_lib-append \
    port:exiv2 \
    port:fftw-3 \
    port:glew \
    port:imath \
    port:openexr \
    port:libpano13 \
    port:tiff \
    port:vigra \
    port:${wxWidgets.port} \
    port:zlib

require_active_variants glew "" x11

depends_lib-append \
    port:flann \
    port:lcms2 \
    port:sqlite3

# Note: 'lz4' needed for 'flann':
#   Undefined symbols for architecture x86_64:
#     "_LZ4_compress_HC_continue", referenced from:
#         flann::serialization::SaveArchive::flushBlock() in PanoDetectorLogic.cpp.o
#     "_LZ4_resetStreamHC", referenced from:
#         flann::serialization::SaveArchive::initBlock() in PanoDetectorLogic.cpp.o
depends_lib-append \
    port:lz4
configure.ldflags-append \
    -llz4

depends_run-append \
    port:enblend \
    port:p${perl5.major}-image-exiftool

configure.args-append \
    -DINSTALL_OSX_BUNDLE_DIR=${applications_dir} \
    -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
    -DBUILD_HSI=OFF \
    -DENABLE_LAPACK=ON

pre-configure {
    configure.args-append ${cmake_linalglib}
}

set pythons_versions {3.14}

set pythons_ports {}
foreach v ${pythons_versions} {
    lappend pythons_ports python[string map {. {}} ${v}]
}

# The variants only select a Python version, so their bodies stay empty and
# the configuration below runs at the top level with ordinary variables.
foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    set p python${s}
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build Python Scripting Interface with Python ${v}" conflicts {*}${c} ""
}

set python_version ""
foreach v ${pythons_versions} {
    if {[variant_isset python[string map {. {}} ${v}]]} {
        set python_version ${v}
    }
}
if {${python_version} eq ""} {
    # keep in sync with python PG
    default_variants-append +python314
}

# resolve again, now that the default variant has been applied
set python_version ""
foreach v ${pythons_versions} {
    if {[variant_isset python[string map {. {}} ${v}]]} {
        set python_version ${v}
    }
}

pre-fetch {
    if {${python_version} eq ""} {
        return -code error "You must choose at least one Python variant."
    }
}

if {${python_version} ne ""} {
    set python_suffix [string map {. {}} ${python_version}]
    set python_prefix ${frameworks_dir}/Python.framework/Versions/${python_version}

    depends_build-append \
        port:py${python_suffix}-setuptools \
        port:swig-python

    depends_lib-append \
        port:python${python_suffix}

    configure.python ${python_prefix}/bin/python${python_version}

    configure.args-append \
        -DPython3_EXECUTABLE=${python_prefix}/bin/python${python_version} \
        -DPython3_ROOT_DIR=${python_prefix}

    configure.args-replace \
        -DBUILD_HSI=OFF \
        -DBUILD_HSI=ON
}

notes-append \
    "Hugin applications are now installed in folder: ${applications_dir}"

livecheck.type          regex
livecheck.url           ${homepage}
livecheck.regex         "Hugin\[ -\](\[.0-9\]+) released"
