# -*- 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.0
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  86309a3322354d59b0632016cc32b8cb2cdcc08e \
                        sha256  2ae229c548de10d92d79f61431503bae28a6df1c09d887b399973723a1e5e6f1 \
                        size    19447356

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] && ![variant_isset python39]} {
        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 python39 python310 python311 python312 description {Build with Python 3.13} {
    set ::python_branch 3.13
    python-depends ${::python_branch}
}

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

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

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

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

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