# -*- 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           select 1.0
PortGroup           openssl 1.0
PortGroup           deprecated 1.0

# Reached EOL on 2016-02-24
deprecated.upstream_support no

name                ruby20
version             2.0.0-p648
revision            14

categories          lang ruby
maintainers         {kimuraw @kimuraw} openmaintainer
platforms           darwin

description         Powerful and clean object-oriented scripting language
long_description    Ruby is the interpreted scripting language for quick \
                    and easy object-oriented programming. It has many \
                    features to process text files and to do system \
                    management tasks (as in Perl). It is simple, \
                    straight-forward, extensible, and portable.

homepage            http://www.ruby-lang.org/
license             {Ruby BSD}

master_sites        ruby:2.0
use_bzip2           yes
distname            ruby-${version}

checksums           md5 3544031334f4665aa2eb1414babc9345 \
                    rmd160 3b6fd5d89e6e53fb71f512f44b710148698a6e29 \
                    sha1 504be2eae6cdfe93aa7ed02ec55e35043d067ad5 \
                    sha256 087ad4dec748cfe665c856dbfbabdee5520268e94bb81a1d8565d76c3cc62166 \
                    size 10785918

depends_lib         port:readline \
                    port:zlib \
                    port:ncurses \
                    port:libyaml \
                    port:libffi \
                    port:gdbm
openssl.branch      1.0
depends_run         port:ruby_select
depends_build       port:pkgconfig
depends_skip_archcheck pkgconfig

select.group        ruby
select.file         ${filespath}/ruby20

configure.args      --enable-shared \
                    --disable-install-doc \
                    --mandir="${prefix}/share/man" \
                    --enable-pthread \
                    --without-tk \
                    --with-opt-dir="${prefix}" \
                    --with-openssl-include=[openssl::include_dir] \
                    --with-openssl-lib=[openssl::lib_dir] \
                    --program-suffix=2.0 \
                    --with-rubylibprefix=${prefix}/lib/ruby2.0

configure.pkg_config_path-prepend ${prefix}/lib/openssl-1.0/pkgconfig

# Ignore minor version for archdir, like i686-darwin9.
# Port "ruby" and "ruby19" does the same.
configure.env-append UNAME_RELEASE=${os.major}

# fix missing c-extentions at ext/*
configure.cflags-append -Wno-error=implicit-function-declaration

# ext/tk/extconf.rb: fix build error with tk variant
patchfiles          patch-ext-tk-extconf.rb.diff
# patch-configure_cxx11.diff: fix "invalid suffix on literal" with C++11
#                             from RUBY_ARCH and RUBY_PLATFORM in config.h
#                             https://trac.macports.org/ticket/58255
patchfiles-append   patch-configure_cxx11.diff
# thread.c: fix implicit declaration of rb_frame_last_func
patchfiles-append   implicit.patch
# Fix pkg-config in ext/openssl
patchfiles-append   patch-openssl_pkgconfig.diff

# replace old config.{guess,sub} with recent versions from automake
depends_build-append    port:automake
post-patch {
    set automake_dirs [glob -directory ${prefix}/share automake-*]
    set automake_dir [lindex [lsort -command vercmp $automake_dirs] end]
    copy -force ${automake_dir}/config.guess ${automake_dir}/config.sub \
        ${worksrcpath}/tool
}

# [NOTE] workaround for mismatch of sdk versions on macOS 11.x,
# such as MacOSX11.0.sdk (buildbot) <=> MacOSX11.1.sdk (user's Mac).
# (see ruby27)
post-build {
    if {${os.major} >= 20} {
        # rewrite MacOSX11.1.sdk -> MacOSX.sdk in rbconfig.rb
        file copy ${worksrcpath}/rbconfig.rb ${worksrcpath}/rbconfig.rb.orig
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?[[:blank:]])|\1\2|g} \
            ${worksrcpath}/rbconfig.rb \
            ${worksrcpath}/ruby-2.0.pc
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?)$|\1\2|g} \
            ${worksrcpath}/rbconfig.rb \
            ${worksrcpath}/ruby-2.0.pc
    }
}

# Add the architecture flag as required
if {[info exists build_arch] && ${build_arch} ne ""} {
    configure.args-append "--with-arch=${build_arch}"
}

post-destroot {
    foreach type {site vendor} {
            set libdir ${destroot}${prefix}/lib/ruby2.0/${type}_ruby/2.0.0
            xinstall -m 0755 -d ${libdir}
    }

    foreach subdir [exec find ${libdir} -type d -empty] {
            destroot.keepdirs-append ${subdir}
    }

    # install destination of commands from port:rb-*
    xinstall -m 0755 -d ${destroot}${prefix}/libexec/ruby2.0
    destroot.keepdirs-append ${destroot}${prefix}/libexec/ruby2.0
}

notes-append "
To make this the default Ruby (i.e., the version run by\
the 'ruby' or 'gem' commands), run:
    sudo port select --set ruby $name
"

variant doc description "Install rdoc indexes and C API documents" {
        configure.args-delete   --disable-install-doc
}

variant tk conflicts mactk description "Build using MacPorts Tk" {
        configure.args-delete   --without-tk
        configure.args-append   --with-tk --disable-tcltk-framework
        depends_lib-append      port:tcl \
                                port:tk
}

variant mactk conflicts tk description "Build using Mac OS X Tk Framework" {
        configure.args-delete   --without-tk
        configure.args-append   --enable-tcltk-framework
}

variant universal {
        # Disable parallel builds for Universal case
        # See: https://trac.macports.org/ticket/24240
        use_parallel_build      no

        # use ruby built-in universal mechanism.
        configure.args-append   --with-arch=[join ${universal_archs} ,]
        # clear macports' universal flags
        configure.universal_cflags
        configure.universal_cppflags
        configure.universal_cxxflags
        configure.universal_ldflags
}

variant relative description "Enable relative loading of libraries to allow for relocation of binaries." {
        #enable relative loading
        configure.args-append  --enable-load-relative
}

