# -*- 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               boost 1.0
PortGroup               conflicts_build 1.0

name                    hugin-app
version                 2023.0.0
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   2011

perl5.branches          5.34

# use wxWidget-3.2 on macOS Mojave (10.14) and later for dark mode support
platform darwin {
    if {${os.major} >= 18} {
        wxWidgets.use   wxWidgets-3.2
    } else {
        wxWidgets.use   wxWidgets-3.0-cxx11
    }
}

boost.version           1.78

checksums               rmd160  707dc7008718d54647a962bb028498bd3603847f \
                        sha256  04a39fccc6017e0544c639bd22309472c574d35b3455cbade1fc3870e631632b \
                        size    10336322

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

# Change finite() -> isfinite().  finite() deprecated since C99 and no
# no longer in my libraries.
patchfiles-append       patch-compiler.h.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

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-deprecated-register

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

# see http://wiki.panotools.org/Development_of_Open_Source_tools#Dependencies
depends_lib-append \
    port:exiv2 \
    port:fftw-3 \
    port:gettext \
    port:glew \
    port:imath \
    path:include/turbojpeg.h:libjpeg-turbo \
    port:openexr \
    port:libpano13 \
    port:libpng \
    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.10 3.11 3.12}

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

set any_python 0
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}]
    set python_prefix ${frameworks_dir}/Python.framework/Versions/${v}
    variant ${p} description "Build Python Scripting Interface with Python ${v}" conflicts {*}${c} "
        depends_build-append \
            port:py${s}-setuptools

        depends_lib-append \
            port:${p} \
            port:swig-python

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

        configure.args-append \
            -DPYTHON_EXECUTABLE=${python_prefix}/bin/python${v} \
            -DPYTHON_INCLUDE_DIR=${python_prefix}/Headers \
            -DPYTHON_LIBRARY=${python_prefix}/Python

        configure.args-replace \
            -DBUILD_HSI=OFF \
            -DBUILD_HSI=ON
    "
    if {[variant_isset python${s}]} {
        set any_python 1
    }
}
if {!${any_python}} {
    # keep in sync with python PG
    default_variants +python312
}

pre-fetch {
    set any_python 0
    foreach v ${pythons_versions} {
        set s [string map {. {}} ${v}]
        if {[variant_isset python${s}]} {
            set any_python 1
        }
    }
    if {!${any_python}} {
        return -code error "You must choose at least one Python variant."
    }
}

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

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