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

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.80
} else {
    # Version for older systems, including PPC. 1.73 still builds but freezes on quit.
    version             1.72
}
revision                0
set rel_r_ver           4.3.2
# 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.
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/

distname                Mac-GUI-${version}

if {${os.platform} eq "darwin" && ${os.major} > 12} {
    checksums           ${distname}.tar.gz \
                        rmd160  8c41086f9390e1c5f1a9b6f2a59ed3e84abad164 \
                        sha256  11a84bd2e26cfd746129bc334ffc78215468497e205d6a8f710e20ac4bead915 \
                        size    1400603
} else {
    checksums           ${distname}.tar.gz \
                        rmd160  022ddae8a3988c5a33928d520742ddfca73ac21d \
                        sha256  a30c97b37645b55aff62bc88cccaf92c307f1e84d84d3ecd5c1333afa2b78e76 \
                        size    1311627
}

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}.tar.gz
    set main_dist       ${distname}.tar.gz
    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
    if {${os.platform} eq "darwin" && ${os.major} > 12} {
        set rev_file [open ${worksrcpath}/REVISION r]
        set r_rev [lindex [split [read $rev_file] "\n"] 0]
    } else {
        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
