# -*- 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               active_variants 1.1
PortGroup               xcode 1.0

name                    R-app
# 1.83 is the first release compatible with R 4.6 (SETLEVELS was removed from the R API),
# so it is used on all systems. Compatibility notes see in R.xcodeproj/project.pbxproj.
version                 1.83
revision                0
# `rel_r_ver` is the version of R when this version of R-app was released, the `R_RELEASE`
# variable in InfoPlist.h, used for changing version info of app bundle to match the actual
# R version. Should be updated for each R-app release, and revbumped for each R release.
set rel_r_ver           4.6.0
categories              math science aqua
maintainers             {me.com:kjell.konis @kjellpk} {i0ntempest @i0ntempest} openmaintainer
license                 GPL-2+

description             R GUI for Mac OS X
long_description        {*}${description}

homepage                http://R.research.att.com
platforms               macosx

master_sites            https://cran.r-project.org/bin/macosx/ \
                        http://cran.rstudio.com/bin/macosx/ \
                        https://cran.csie.ntu.edu.tw/bin/macosx/

distname                Mac-GUI-${version}
checksums               ${distname}${extract.suffix} \
                        rmd160  91633817ad3994094b90df25091c4c67f3d76982 \
                        sha256  1fdf93ccd6bde1c4261af2ff9c517184ff99fbb35f03d6e60d3f1e50b331e3d2 \
                        size    1402105

depends_lib             port:R

require_active_variants R aqua

# All of the following are needed for older SDKs / non-clang compilers only, but are
# guarded by MAC_OS_X_VERSION_MAX_ALLOWED (or equivalent), so they are applied everywhere.
# 0001: InfoPlist.h compares MIN_VER against wrong constants for 10.6-10.9.
# 0002: R >= 4.6 built with a C23-capable compiler (gcc 13+, i.e. our gcc16-built R) puts
#       HAVE_ENUM_BASE_TYPE into Rconfig.h, and R_ext/Boolean.h then declares Rboolean as
#       `enum : int`, which no pre-C23 compiler accepts. Undo it in the prefix header.
# 1001: stderr filter uses NSRegularExpressionSearch (10.7 SDK).
# 2002: NSTouchBar and ObjC literals (10.12.2 SDK, clang).
# 2003: @available (clang from Xcode 9) -> NSAppKitVersionNumber check.
# 2004: -[NSWindow contentLayoutRect] (10.10 SDK).
# 2005: NSTextStorageEditActions type (10.11 SDK).
patchfiles              0001-InfoPlist.h-fix-MIN_VER.patch \
                        0002-R_Prefix.pch-Rconfig-enum-base-type.patch \
                        1001-RController.m-guard-stderr-filter-by-SDK.patch \
                        2002-RController.m-guard-touchbar-by-SDK.patch \
                        2003-RController.m-replace-at-available.patch \
                        2004-RDocumentWinCtrl.m-guard-contentLayoutRect-by-SDK.patch \
                        2005-RScriptEditorTextStorage.m-NSTextStorageEditActions-typedef.patch

if {${os.platform} eq "darwin" && ${os.major} > 12} {
    # Toolbar icon sizing on recent macOS, from MacPorts.
    patchfiles-append   patch-RController-toolbar-icons.diff
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # We need to use xib files from older version, newer fail to build with Xcode 3.x.
    set legacy_ver      1.70
    set legacy_dist     Mac-GUI-${legacy_ver}${extract.suffix}
    set main_dist       ${distname}${extract.suffix}
    distfiles-append    ${legacy_dist}
    checksums-append    ${legacy_dist} \
                        rmd160  1d351ae2b736bb9f6bd26ce164c37c6926d0e171 \
                        sha256  8be56f2705c5d20ebecb36948e70fe0a89ba01a9fee214e5fe58f24c3f2eee48 \
                        size    1795622

    pre-patch {
        delete ${worksrcpath}/de.lproj
        delete ${worksrcpath}/English.lproj
        delete ${worksrcpath}/fr.lproj
        delete ${worksrcpath}/it.lproj
        delete ${worksrcpath}/ja.lproj
        delete ${worksrcpath}/nl.lproj
        # Spanish was added in 1.80; there is no 1.70 counterpart, so drop it (see post-patch).
        delete ${worksrcpath}/es.lproj

        move ${workpath}/Mac-GUI-${legacy_ver}/de.lproj ${worksrcpath}
        move ${workpath}/Mac-GUI-${legacy_ver}/English.lproj ${worksrcpath}
        move ${workpath}/Mac-GUI-${legacy_ver}/fr.lproj ${worksrcpath}
        move ${workpath}/Mac-GUI-${legacy_ver}/it.lproj ${worksrcpath}
        move ${workpath}/Mac-GUI-${legacy_ver}/ja.lproj ${worksrcpath}
        move ${workpath}/Mac-GUI-${legacy_ver}/nl.lproj ${worksrcpath}
    }

    # Restore the cached-IMP direct calls removed upstream in 1.74 (r7916); not needed for
    # compilation, kept because the 1.76+ code was found to misbehave on these systems.
    patchfiles-append   2001-Revert-remove-direct-function-calls-to-Obj-C-methods.patch

    post-patch {
        # Remove all references to es.lproj from the project (file refs, variant group
        # children and the knownRegions entry).
        reinplace {/\/\* es \*\//d} ${worksrcpath}/R.xcodeproj/project.pbxproj
        reinplace {/^[[:space:]]*es,$/d} ${worksrcpath}/R.xcodeproj/project.pbxproj

        if {${os.major} == 9} {
            reinplace "s|Xcode 3.2|Xcode 3.1|g" ${worksrcpath}/R.xcodeproj/project.pbxproj
        }
        reinplace "s|MACOSX_DEPLOYMENT_TARGET = 10.13|MACOSX_DEPLOYMENT_TARGET = ${macosx_deployment_target}|g" ${worksrcpath}/R.xcodeproj/project.pbxproj
    }
}

post-patch {
    reinplace "s|/Library/Frameworks/R.framework|${frameworks_dir}/R.framework|g" \
        ${worksrcpath}/R.xcodeproj/project.pbxproj
    set r_ver [lindex [split [exec ${prefix}/bin/R --version | head -n1] " "] 2]
    reinplace "s|${rel_r_ver}|${r_ver}|g" ${worksrcpath}/InfoPlist.h
    if {[file exists ${worksrcpath}/REVISION]} {
        set rev_file [open ${worksrcpath}/REVISION r]
        set r_app_rev [lindex [split [read $rev_file] "\n"] 0]
    } else {
        set rev_file [open ${worksrcpath}/updateSVN r]
        set r_app_rev [lindex [split [lsearch -inline [split [read $rev_file] "\n"] "SVNREV=*"] "="] 1]
    }
    close $rev_file
    reinplace "s|%SVN%|${r_app_rev}|g" ${worksrcpath}/Info.plist
}

use_configure           no

universal_variant       no

xcode.project           R.xcodeproj
xcode.target            R
xcode.configuration     Release

xcode.build.settings    FRAMEWORK_SEARCH_PATHS=${frameworks_dir}
xcode.destroot.settings FRAMEWORK_SEARCH_PATHS=${frameworks_dir}

if {[vercmp ${xcodeversion} 10.0] >= 0} {
    destroot.pre_args  -UseNewBuildSystem=NO
}

post-destroot {
    delete ${destroot}${applications_dir}/sush

    # https://trac.macports.org/ticket/67042
    if {${configure.cxx_stdlib} ne "libc++"} {
            set rexec ${applications_dir}/R.app/Contents/MacOS/R
            set rgui ${prefix}/bin/R-gui

            set  wrapper    [open "${destroot}${rgui}" w 0755]
            puts ${wrapper} "#!/bin/bash"
            puts ${wrapper} ""
            puts ${wrapper} {if [ -n "$DYLD_LIBRARY_PATH" ]; then}
            puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc:\${DYLD_LIBRARY_PATH}"
            puts ${wrapper} {else}
            puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc"
            puts ${wrapper} {fi}
            puts ${wrapper} {export DYLD_LIBRARY_PATH}
            puts ${wrapper} ""
            puts ${wrapper} "   PATH=${prefix}/bin:${prefix}/sbin:\$PATH"
            puts ${wrapper} {export PATH}
            puts ${wrapper} ""
            puts ${wrapper} "exec ${rexec} \"\$@\""
            close $wrapper
    }
}

if {${configure.cxx_stdlib} ne "libc++"} {
    notes "
    A wrapper script to launch the app has been installed into ${prefix}/bin.
    Please start the app from command line as `R-gui`.
    "
}

livecheck.name          Mac-GUI
