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

name                kicad
version             7.0.11
description         KiCad is an electronic design automation software suite
long_description    KiCad is an EDA software suite for the creation of professional schematics \
                    and printed circuit boards up to 32 copper layers with additional technical layers.
categories          science
license             GPL-3+
maintainers         {ra1nb0w @ra1nb0w} openmaintainer
homepage            https://www.kicad.org/

# Cannot update to python 3.12 because there is no py312-wxpython-4.0 port
set python_version  3.11
set py_ver_no_dot   [join [split ${python_version} "."] ""]
set python_framework_dir ${frameworks_dir}/Python.framework/Versions/${python_version}

if {${name} eq ${subport}} {
    PortGroup           boost 1.0
    PortGroup           conflicts_build 1.0
    PortGroup           legacysupport 1.1

    # Ignore stealth update of 7.0.11.
    # https://trac.macports.org/ticket/69836
    master_sites        macports_distfiles

    gitlab.setup        kicad/code kicad ${version}
    revision            2
    checksums           rmd160  05936277e09b18f03f773d7e8ffdd7fbb54e3376 \
                        sha256  24feb0747ee8d767c903fac5254c314a5012f4aee7016e961da845765764cf73 \
                        size    44083805

    patchfiles-append \
        macports_prefix.patch \
        opencascade-7.8.patch \
        0001-cmake-adding-KICAD_MACOSX_APP_BUNDLE-option.patch \
        0002-disable-codesigning.patch

    post-patch {
        reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
            common/gestfich.cpp \
            eeschema/sim/ngspice.cpp
    }

    boost.version       1.81
    conflicts_build     boost

    compiler.cxx_standard 2017

    # It requires c++17 <filesystem> and abs
    legacysupport.newest_darwin_requires_legacy 19
    legacysupport.use_mp_libcxx yes

    depends_build-append \
        path:bin/doxygen:doxygen \
        path:bin/pkg-config:pkgconfig \
        port:swig \
        port:swig-python

    depends_lib-append \
        port:python${py_ver_no_dot} \
        port:glew \
        port:glm \
        port:curl \
        port:ngspice \
        port:opencascade \
        port:py${py_ver_no_dot}-wxpython-4.0 \
        port:zlib \
        path:lib/pkgconfig/cairo.pc:cairo \
        port:py${py_ver_no_dot}-pybind11 \
        path:lib/pkgconfig/pixman-1.pc:libpixman \
        port:unixODBC \
        path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
        port:kicad-docs \
        port:kicad-symbols \
        port:kicad-footprints \
        port:kicad-packages3D \
        port:kicad-templates

    cmake.build_type Release

    configure.args-append \
        -DKICAD_MACOSX_APP_BUNDLE=OFF \
        -DKICAD_USE_OCC=ON \
        -DwxWidgets_CONFIG_EXECUTABLE=${python_framework_dir}/bin/wx-config \
        -DKICAD_BUILD_QA_TESTS=OFF \
        -DKICAD_SCRIPTING_MODULES=ON \
        -DKICAD_SCRIPTING=ON \
        -DKICAD_SCRIPTING_WXPYTHON=ON \
        -DPYTHON_SITE_PACKAGE_PATH=${python_framework_dir}/lib/python${python_version}/site-packages \
        -DPYTHON_EXECUTABLE=${python_framework_dir}/bin/python${python_version} \
        -DPYTHON_INCLUDE_DIR=${python_framework_dir}/Headers \
        -DPYTHON_LIBRARY=${python_framework_dir}/Python \
        -DDOXYGEN_DOT_EXECUTABLE=${prefix}/bin/dot \
        -DDOXYGEN_EXECUTABLE=${prefix}/bin/doxygen \
        -DNGSPICE_LIB_NAME=libngspice.dylib \
        -DOCC_INCLUDE_DIR=${prefix}/libexec/opencascade/include/opencascade \
        -DOCC_LIBRARY_DIR=${prefix}/libexec/opencascade/lib \
        -Wno-dev

    post-destroot {
        foreach app_name {bitmap2component eeschema gerbview kicad pcb_calculator pcbnew} {
            set app_dir ${destroot}${applications_dir}/KiCad/${app_name}.app/Contents
            xinstall -d ${app_dir}/MacOS \
                ${app_dir}/Resources
            xinstall -m 0644 ${worksrcpath}/${app_name}/${app_name}.icns \
                ${app_dir}/Resources/${app_name}.icns
            if {[file exists ${worksrcpath}/${app_name}/${app_name}_doc.icns]} {
                xinstall -m 0644 ${worksrcpath}/${app_name}/${app_name}_doc.icns \
                    ${app_dir}/Resources/${app_name}_doc.icns
            }
            xinstall -m 0644 ${build.dir}/${app_name}/Info.plist \
                ${app_dir}/Info.plist

            ln -s ${prefix}/bin/${app_name} ${app_dir}/MacOS/${app_name}
            foreach kiface [glob ${destroot}${prefix}/bin/*.kiface] {
                set filename  [file tail $kiface]
                ln -s ${prefix}/bin/$filename ${app_dir}/MacOS/${filename}
            }
        }
    }

    # Exclude x.99.y development versions
    gitlab.livecheck.regex {([0-9]+\.([1-8]?[0-9]|9[0-8])\.[0-9]+)}
} else {
    supported_archs     noarch
    platforms           any

    if {${subport} ne "kicad-docs"} {
        patchfiles-append ${subport}.patch
    }

    livecheck.type      none
}

subport kicad-docs {
    revision            2
    checksums           rmd160  9b5c9570129336d2cf603e91cbcc29f7668da69c \
                        sha256  0ecf3c1b748a1a2d07cc3f8e74e3ffcca948ee997b1eed2ef371ae4d7992ab9a \
                        size    602194906

    description         KiCad documentation
    long_description    {*}${description}

    # we use a pre-compiled binary since it takes a very long time to compile
    # and requires many big dependencies
    master_sites        https://kicad-downloads.s3.cern.ch/docs
    distname            ${name}-doc-${version}

    use_configure       no

    build {}

    destroot {
        xinstall -d ${destroot}${prefix}/share/doc
        file copy ${worksrcpath}/share/doc/kicad ${destroot}${prefix}/share/doc
    }
}

subport kicad-symbols {
    gitlab.setup        kicad/libraries kicad-symbols ${version}
    revision            2
    checksums           rmd160  dd69fdc2cf17e0574508663285024e6369348d43 \
                        sha256  666acf1ef282430721e64b651672d5a0b6896b3db7d8854f6035de767bf9b21b \
                        size    3369887

    description         Kicad symbols
    long_description    {*}${description}
}

subport kicad-footprints {
    gitlab.setup        kicad/libraries kicad-footprints ${version}
    revision            2
    checksums           rmd160  e9f5b60f98f393c0d78b20e82bef74bb36fecc2f \
                        sha256  c07a5886023793efc4e697bd49b07c7e13d5ec6eb18db8bf62b849add219b48f \
                        size    22722611

    description         Kicad footprints
    long_description    {*}${description}
}

subport kicad-packages3D {
    gitlab.setup        kicad/libraries kicad-packages3D ${version}
    revision            2
    checksums           rmd160  36f02b35321c7e203fb6308d8d9912bfe28d343f \
                        sha256  11361137f3604d11b12a1f5c132e939f6fed806bb2675d9990a985c967fae2f9 \
                        size    774735333

    description         Kicad package 3D
    long_description    {*}${description}
}

subport kicad-templates {
    gitlab.setup        kicad/libraries kicad-templates ${version}
    revision            2
    checksums           rmd160  10be23bfa01d8b1e617197774b1e2768797a6e12 \
                        sha256  df708e2ac2a81cbe1fb8b655b0d29694dc8c530616d244330f4cd70a9e463fd2 \
                        size    1348043

    description         Kicad templates
    long_description    {*}${description}
}
