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

name                    R-app

# Update for newer systems, keep pegged for older. Compatibility see in R.xcodeproj/project.pbxproj
if {${os.platform} eq "darwin" && ${os.major} > 12} {
    version             1.82
    set rel_r_ver       4.5.1
    revision            2
    # `rel_r_ver` is the version of R when this version of R-app was released, 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.

    distname            Mac-GUI-${version}
    checksums           ${distname}${extract.suffix} \
                        rmd160  08245af666c5df968022b9f91434037e1da0acd7 \
                        sha256  1a1b7d755b2e245acb7e3ced568ed424a70dae0a9b3d9c642bf6149b484350c1 \
                        size    1400968
} else {
    # Version for older systems, including PPC. 1.73 still builds but freezes on quit.
    version             1.72
    set rel_r_ver       4.0.2
    revision            9

    distname            Mac-GUI-${version}
    checksums           ${distname}${extract.suffix} \
                        rmd160  022ddae8a3988c5a33928d520742ddfca73ac21d \
                        sha256  a30c97b37645b55aff62bc88cccaf92c307f1e84d84d3ecd5c1333afa2b78e76 \
                        size    1311627
}
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            http://cran.rstudio.com/bin/macosx/ \
                        http://cran.us.r-project.org/bin/macosx/

require_active_variants R aqua

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

        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}
    }

    post-patch {
        # These are intended for 1.72 (see above). Other versions set different compatibility.
        if {${os.major} == 9} {
            reinplace "s|Xcode 3.2|Xcode 3.1|g" ${worksrcpath}/R.xcodeproj/project.pbxproj
        }
        reinplace "s|MACOSX_DEPLOYMENT_TARGET = 10.11|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
    set rev_file [open ${worksrcpath}/updateSVN r]
    set r_rev [lindex [split [lsearch -inline [split [read $rev_file] "\n"] "SVNREV=*"] "="] 1]
    close $rev_file
    reinplace "s|%SVN%|${r_rev}|g" ${worksrcpath}/Info.plist
}

use_configure           no

depends_lib             port:R

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
}

livecheck.name          Mac-GUI
