# -*- 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                racket
version             9.2
revision            0
categories          lang scheme
license             Apache-2 MIT
maintainers         nomaintainer
description         Scheme programming environment
long_description    Interactive, integrated, graphical {*}${description}.

homepage            https://racket-lang.org

master_sites        https://download.racket-lang.org/releases/${version}/installers/ \
                    https://mirror.racket-lang.org/installers/${version}/ \
                    https://www.cs.utah.edu/plt/installers/${version}/ \
                    https://www.eecs.northwestern.edu/racket/${version}/ \
                    https://mirror.csclub.uwaterloo.ca/racket/racket-installers/${version}/ \
                    https://mirror.informatik.uni-tuebingen.de/mirror/racket/${version}/ \
                    https://racket.infogroep.be/${version}/

distname            ${name}-${version}-src
extract.suffix      .tgz

checksums           rmd160  8ae1912e6f52a383ffde1060048205028df531c7 \
                    sha256  ccf98fd81444dfb3e3d0fde86e8db0753a7e8568725d45c0189ce5c8b7c4bb19 \
                    size    34737316

extract.rename      yes

patch.dir           ${workpath}/${distname}

# https://github.com/racket/racket/issues/5025
# diff -NaurdwB -I '^;;[[:space:]]*' ./racket-orig/ ./racket-new | sed -E -e 's/\.\/racket-(orig|new)/\./g' > ~/Downloads/paths-to-openssl.diff
patchfiles-append   paths-to-openssl.diff

post-patch {
    # patch the @@PREFIX@@ in paths-to-openssl.diff
    foreach rkt {libcrypto.rkt libssl.rkt} {
        reinplace "s|@@PREFIX@@|${prefix}|g" \
            ${worksrcpath}/../collects/openssl/${rkt}
    }

    # https://github.com/racket/racket/issues/5055#issuecomment-2321445579
    # collects/racket/private/so-search.rkt
    reinplace -E \
        "s|^#hash\\((.+)\\)$|#hash(\\1 (lib-search-dirs . (\"${prefix}/lib\" \"${frameworks_dir}\" #f)))|" \
        ${worksrcpath}/../etc/config.rktd

    # use MacPorts-installed libraries
    # gettext-runtime provides libintl.8
    # jpeg-turbo provides libjpeg.8
    # libffi provides libffi.8
    # mpfr provides libmpfr.6
    # ossp-uuid provides libuuid.16
    # poppler provides libpoppler.136
    foreach rkt [list \
                    "${worksrcpath}/native-libs/install.rkt" \
                    "${worksrcpath}/../share/pkgs/draw-lib/racket/draw/unsafe/cairo-lib.rkt" \
                    "${worksrcpath}/../share/pkgs/draw-lib/racket/draw/unsafe/glib.rkt" \
                    "${worksrcpath}/../share/pkgs/draw-lib/racket/draw/unsafe/jpeg.rkt" \
        ] {
        # regex for (libffi).6, (libpangocairo-1.0.0), (libpng16).6.(.dylib), etc.
        reinplace -E "s|\"(lib\[\[:alnum:]_]+(-\[\[:alnum:]_]+)*(-\[\[:digit:]]+(\\.\[\[:digit:]]+)+)*)(\\.\[\[:digit:]]+)?(\\.dylib)*\"|\"\\1\\6\"|g" ${rkt}
    }
}

worksrcdir          ${worksrcpath}/src

depends_build-append \
                    port:libtool

depends_lib-append  path:lib/libcrypto.dylib:openssl \
                    path:lib/libssl.dylib:openssl \
                    port:expat \
                    port:fontconfig \
                    port:freetype \
                    port:libffi \
                    port:libiconv \
                    port:lz4 \
                    port:ncurses \
                    port:zlib

configure.args-append \
                    --enable-curses \
                    --enable-liblz4 \
                    --enable-libz \
                    --enable-pthread

if {${configure.build_arch} in [list ppc ppc64]} {
    # Stripping may fail on PowerPC:
    # https://github.com/racket/racket/issues/5021
    configure.args-append \
                    --disable-strip \
                    --enable-bigendian
}

set Name            [string totitle ${name}]
set Name_fw         ${Name}.framework/Versions/${version}_CS/${Name}
set racket_apps_dir "${applications_dir}/${Name} ${version}"

variant x11 \
    description {Use X11 GUI.} {}

if { [variant_isset "x11"] } {
    PortGroup       active_variants 1.1

    depends_lib-append \
                    port:gdk-pixbuf2 \
                    port:gtk2

    require_active_variants gtk2 x11

    configure.args-append \
                    --enable-xonx
} else {
    depends_build-append \
                    port:cctools \
                    port:file \
                    port:grep \
                    port:gsed \
                    path:bin/openssl:openssl

    # https://github.com/racket/libs/tree/master/draw-x86_64-macosx-3/racket/draw
    # https://github.com/racket/libs/tree/master/gui-x86_64-macosx/racket/gui
    # https://github.com/racket/libs/tree/master/math-x86_64-macosx/math
    # https://github.com/racket/libs/tree/master/racket-x86_64-macosx-4/racket
    depends_lib-append \
                    port:atk \
                    port:cairo \
                    port:fribidi \
                    port:gettext-runtime \
                    port:glib2 \
                    port:harfbuzz \
                    port:libedit \
                    port:libjpeg-turbo \
                    port:libpixman \
                    port:libpng \
                    port:MMTabBarView \
                    port:mpfr \
                    port:ossp-uuid \
                    port:pango \
                    port:poppler

    configure.args-append \
                    --disable-origtree \
                    --enable-macprefix \
                    --enable-nonatipkg \
                    "--guibindir='${racket_apps_dir}'"

    pre-destroot {
        xinstall -d ${racket_apps_dir}
    }

    post-destroot {
        # framework
        xinstall -d ${destroot}${frameworks_dir}
        move        ${destroot}${prefix}/lib/${name}/${Name}.framework \
                    ${destroot}${frameworks_dir}
        system -W ${destroot}${frameworks_dir} \
                    "install_name_tool -change '${Name_fw}' '@rpath/${Name_fw}' '${Name_fw}'"

        # GRacket.app
        system -W   ${destroot}${prefix}/lib/${name} \
                    "install_name_tool -change '${prefix}/lib/${name}/${Name_fw}' '${frameworks_dir}/${Name_fw}' GRacket.app/Contents/MacOS/GRacket"

        # binaries
        foreach binfile {mzscheme racket} {
            system -W ${destroot}${prefix}/bin \
                    "install_name_tool -change '${prefix}/lib/${name}/${Name_fw}' '${frameworks_dir}/${Name_fw}' '${binfile}'"
            if {${configure.build_arch} eq {arm64}} {
                system -W ${destroot}${prefix}/bin \
                    "codesign -f -s - '${binfile}'"
            }
        }

        # apps
        foreach app_name [list \
                    DrRacket \
                    "PLT Games" \
                    "Racket Documentation" \
                    Slideshow \
            ] {
            system -W ${destroot}${racket_apps_dir} \
                "install_name_tool -change '${prefix}/lib/${name}/${Name_fw}' '${frameworks_dir}/${Name_fw}' '${app_name}.app/Contents/MacOS/${app_name}'"
            if {${configure.build_arch} eq {arm64}} {
                system "codesign -f -s - '${destroot}${racket_apps_dir}/${app_name}.app/Contents/MacOS/${app_name}'"
            }
        }
    }
}

livecheck.type  regex
livecheck.url   https://racket-lang.org
livecheck.regex {>Racket version ((?:\d+\.?)+)<}
