# -*- 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               qt5 1.0

qt5.depends_component   qttools qtwebengine

github.setup            Sigil-Ebook Sigil 2.2.1
revision                0
name                    sigil
conflicts               sigil-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  f7db20c65e2fec4a28122cfa2b244df5f922587f \
                        sha256  0f491d703424557b5829d60e08ac17747735d54c14a92c470f448515caf4bfb1 \
                        size    26131954
github.tarball_from     archive

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

patchfiles              pcre.patch
conflicts_build         libzip
compiler.cxx_standard   2011

configure.args          -DCMAKE_BUILD_WITH_INSTALL_RPATH=OFF \
                        -DSYSTEM_LIBS_REQUIRED=ON \
                        -DUSE_SYSTEM_LIBS=ON \
                        -DUSE_QT5=ON

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 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\\.8 COMPONENTS Interpreter Development|Python3 ${::python_branch} COMPONENTS Interpreter Development|g" ${worksrcpath}/CMakeLists.txt
        reinplace "s|PythonInterp 3\\.8|PythonInterp ${::python_branch}|g" ${worksrcpath}/CMakeLists.txt
        reinplace "s|PythonLibs 3\\.8|PythonLibs ${::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 python312 conflicts python39 python310 python311 description {Build with Python 3.12} {
    set ::python_branch 3.12
    python-depends ${::python_branch}
}

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

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

variant python39 conflicts python310 python311 python312 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]} {
    default_variants +python312
}
