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

github.setup            Sigil-Ebook Sigil 2.6.2
github.tarball_from     archive
revision                0
name                    [string tolower ${github.project}]
conflicts               [string tolower ${github.project}]-qt4
categories              editors
maintainers             {i0ntempest @i0ntempest} openmaintainer
license                 GPL-3+

description             Sigil, the ePub editor

long_description        Sigil is a multi-platform WYSIWYG ebook editor. \
                        It is designed to edit books in ePub format.

checksums               rmd160  296dc67c173eb3a5641b4e434522b7e5a82e2e46 \
                        sha256  e53d4ace437a6f02513246b2911a1cd0e5898a283c0ec5c097bd1fc7874efb62 \
                        size    19462877

qt6.depends_build       qttools
qt6.depends_lib         qt5compat \
                        qtwebengine

depends_build-append    path:bin/pkg-config:pkgconfig
depends_lib-append      port:hunspell \
                        port:minizip \
                        port:pcre2 \
                        port:zlib

conflicts_build         libzip
compiler.cxx_standard   2011

configure.args          -DCMAKE_BUILD_WITH_INSTALL_RPATH=OFF \
                        -DSYSTEM_LIBS_REQUIRED=ON \
                        -DUSE_SYSTEM_LIBS=ON \
                        -DUSE_QT6=1 \
                        -DCMAKE_OSX_DEPLOYMENT_TARGET=${macosx_deployment_target}

post-patch {
    system -W ${worksrcpath}/src/Resource_Files/mac "/usr/libexec/PlistBuddy -c \"Set :LSMinimumSystemVersion ${macosx_deployment_target}\" MacOSXBundleInfo.plist"
}

pre-configure {
    if {![variant_isset python313] && ![variant_isset python312] && ![variant_isset python311] && \
        ![variant_isset python310]} {
        ui_error "${name} requires Python, please use a Python variant."
        return -code error
    }
}

destroot {
    copy ${destroot.dir}/bin/Sigil.app ${destroot}${applications_dir}
}

proc python-depends {python_branch} {
    global frameworks_dir env
    post-patch {
        reinplace "s|Python3 3\\.9|Python3 ${::python_branch}|g" ${worksrcpath}/CMakeLists.txt
        reinplace "s|Python3MacFramework 3\\.9|Python3MacFramework ${::python_branch}|g" ${worksrcpath}/CMakeLists.txt
    }
    set python_version [string map {. ""} ${python_branch}]
    depends_lib-append  port:python${python_version} \
                        port:py${python_version}-chardet \
                        port:py${python_version}-cssselect \
                        port:py${python_version}-cssutils \
                        port:py${python_version}-html5lib \
                        port:py${python_version}-lxml \
                        port:py${python_version}-Pillow \
                        port:py${python_version}-regex \
                        port:py${python_version}-six
    configure.env-append \
                        PATH=${frameworks_dir}/Python.framework/Versions/${python_branch}/bin:$env(PATH)
}

variant python313 conflicts python310 python311 python312 description {Build with Python 3.13} {
    set ::python_branch 3.13
    python-depends ${::python_branch}
}

variant python312 conflicts python310 python311 python313 description {Build with Python 3.12} {
    set ::python_branch 3.12
    python-depends ${::python_branch}
}

variant python311 conflicts python310 python312 python313 description {Build with Python 3.11} {
    set ::python_branch 3.11
    python-depends ${::python_branch}
}

variant python310 conflicts python311 python312 python313 description {Build with Python 3.10} {
    set ::python_branch 3.10
    python-depends ${::python_branch}
}

if {![variant_isset python310] && ![variant_isset python311] && ![variant_isset python312]} {
    default_variants +python313
}
