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

name                lazarus
version             4.2-0
revision            0
categories          devel
license             GPL-2 LGPL-2
maintainers         {@kamischi web.de:karl-michael.schindler} openmaintainer
description         Free Pascal IDE.
long_description    Lazarus is an open-source development system that builds \
                    on the Free Pascal compiler by adding an integrated \
                    development environment (IDE).  It includes a \
                    syntax-highlighting code editor and a visual form \
                    designer, as well as a component library that is highly \
                    compatible with Delphi's Visual Component Library \
                    (VCL).  The Lazarus Component Library (LCL) includes \
                    equivalents for many of the familiar VCL controls such \
                    as forms, buttons, text boxes and so on, which are used \
                    to create applications with a graphical user interface \
                    (GUI).

homepage            https://www.lazarus-ide.org
master_sites        sourceforge:lazarus

checksums           rmd160  0f511128b7081eb70959dca51896232ebdb6acd2 \
                    sha256  46c746d765d61f4e2ab270fc7407b844f039e96f1cc65eee7e01e9d3abfc327a \
                    size    92347735

depends_lib         port:fpc port:fpc-sources
supported_archs     x86_64 arm64 ppc

use_configure       no
worksrcdir          lazarus

patchfiles          noGUI-Printer.patch

post-patch {
# add the MacPorts directory tree. This might need a check in a new version

    set patchtarget ${worksrcpath}/ide/packages/ideconfig/include/unix/lazbaseconf.inc

    reinplace "s|1\.\.16|1\.\.17|g" ${patchtarget}
    reinplace "s|'/usr/share/fpcsrc'|'/usr/share/fpcsrc',\\
    '${prefix}/share/fpcsrc'|g"      ${patchtarget}
    reinplace "s|1\.\.8|1\.\.9|g" ${patchtarget}
    reinplace "s|'/usr/share/lazarus',|'/usr/share/lazarus',\\
    '${prefix}/share/lazarus',|g" ${patchtarget}

# change the preferences directory (actually, not yet)
#   reinplace "s|'\.lazarus'|'Library/Preferences/lazarus'|g" ${patchtarget}
    reinplace "s|'/etc/lazarus'|'${prefix}/etc/lazarus'|g"    ${patchtarget}

# adjust the default paths to MacPorts paths

    set patchtarget ${worksrcpath}/tools/install/macosx/environmentoptions.xml

    switch ${build_arch} {
        x86_64 {
            set defaultCompiler ppcx64
        }
        arm64 {
            set defaultCompiler ppca64
        }
        ppc {
            set defaultCompiler ppcppc
        }
        default {
            set defaultCompiler unsupported
        }
    }

    reinplace "s|/usr/local/bin/_PPCARCH_|${prefix}/bin/${defaultCompiler}|g" ${patchtarget}
    reinplace "s|/usr/local/share|${prefix}/share|g"              ${patchtarget}
    reinplace "s|/Developer/lazarus|${prefix}/share/lazarus|g"    ${patchtarget}

    reinplace "s|<DebuggerFilename Value=\"/usr/bin/lldb\"/>|<DebuggerFilename Value=\"/usr/bin/lldb\">\\
        <History Count=\"3\">\\
          <Item1 Value=\"/usr/bin/lldb\"/>\\
          <Item2 Value=\"/usr/bin/gdb\"/>\\
          <Item3 Value=\"${prefix}/bin/gdb\"/>\\
        </History>\\
      </DebuggerFilename>|g" ${patchtarget}

# some more fixes for nogui taking cocoa as a template
# if cocoa or nogui get updated, the files in ${filespath} need to be updated manually

    copy ${filespath}/nogui ${worksrcpath}/components/lclextensions/include/

    copy ${worksrcpath}/components/virtualtreeview/units/cocoa         ${worksrcpath}/components/virtualtreeview/units/nogui
    copy ${worksrcpath}/components/virtualtreeview/include/intf/cocoa/ ${worksrcpath}/components/virtualtreeview/include/intf/nogui

    if {[variant_isset gtk2]} {
        reinplace "s|/sw/lib;/sw/lib/pango-ft219|${prefix}|g" ${worksrcpath}/lcl/interfaces/lcl.lpk
        reinplace "s|/sw/lib|${prefix}/lib|g"                 ${worksrcpath}/lcl/interfaces/lcl.lpk
    }

# macOS 10.13 (High Sierra) and lower has no separate framework UserNotifications
    if {${os.major} <= 17} {
        reinplace "s| -k-weak_framework -kUserNotifications||g" ide/Makefile
        reinplace "s| -k-weak_framework -kUserNotifications||g" ide/Makefile.fpc
        reinplace "s| -k-weak_framework -kUserNotifications||g" components/chmhelp/lhelp/Makefile
        reinplace "s| -k-weak_framework -kUserNotifications||g" components/chmhelp/lhelp/Makefile.fpc
        reinplace "s| -weak_framework UserNotifications||g" lcl/interfaces/lcl.lpk
    }

# ld segfaults on macOS 15.4 and later, fall back to classic linker
# See https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41570
    if {${os.major} >= 24} {
        reinplace "s|_OPT+=-k-|_OPT+=-k-ld_classic -k-|" \
            ide/Makefile ide/Makefile.fpc components/chmhelp/lhelp/Makefile
    }
}

build.target       bigide

use_parallel_build no

variant cocoa conflicts gtk2 qt4 description "Build with Cocoa backend" {
    build.post_args    OPT=-gl LCL_PLATFORM=cocoa
}

variant gtk2 conflicts cocoa qt4 description "Build with GTK backend" {
    depends_lib-append path:lib/pkgconfig/gtk+-2.0.pc:gtk2
    build.post_args    OPT="-gl -Fl${prefix}/lib" LCL_PLATFORM=gtk2
}

variant qt4 conflicts cocoa gtk2 description "Build with Qt4 backend" {
    PortGroup          qt4 1.0

    depends_lib-append port:qt4pas
    build.post_args    OPT=-gl LCL_PLATFORM=qt
}

# It obviously makes no sense to use cocoa outside of macOS.
# On powerpc it is broken, as well as carbon:
# https://trac.macports.org/ticket/70815
# FIXME: gtk and qt backends build, but on old MacOS seem to work
# only in command-line: https://trac.macports.org/ticket/70817
# https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41153
if {![variant_isset cocoa] && ![variant_isset gtk2] && ![variant_isset qt4]} {
    if {${os.platform} eq "darwin" && ${os.arch} ne "powerpc"} {
        default_variants-append +cocoa
    } else {
        default_variants-append +qt4
    }
}

destroot.post_args INSTALL_PREFIX=${destroot}${prefix}

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/etc/lazarus
    xinstall -m 644 ${worksrcpath}/tools/install/macosx/environmentoptions.xml ${destroot}${prefix}/etc/lazarus

    ln -s ${prefix}/share/lazarus/lazarus.app ${destroot}${applications_dir}

# Install documentation files
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/lazarus
    xinstall -m 644 {*}[glob ${worksrcpath}/docs/*.txt] ${destroot}${prefix}/share/doc/lazarus
    xinstall -m 644 {*}[glob ${worksrcpath}/docs/*.pdf] ${destroot}${prefix}/share/doc/lazarus

}

notes "
Release Notes:
  https://wiki.freepascal.org/Lazarus_4.0_release_notes
"

livecheck.regex "lazarus-(\\d+(?:\\.\\d+)*-\\d)"
