# -*- 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 2015-02-23
deprecated.upstream_support no

name                ruby19
version             1.9.3-p551
revision            17

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. \
                    Version 1.9 contains a new VM called YARV, is faster \
                    and slightly incompatible from version 1.8.

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

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

checksums           md5 0d8b272b05c3449dc848bb7570f65bfe \
                    rmd160 59cfcaf4e02957f53bf83557962b0d428156bc01 \
                    sha1 35600f4e2ac98653fa8a634104cb6ab3d47a1535 \
                    sha256 b0c5e37e3431d58613a160504b39542ec687d473de1d4da983dabcf3c5de771e \
                    size 10049332

depends_lib         port:libiconv \
                    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}/ruby19

default_variants    +doc

# lib-rubygems-specification.rb: fix 'Invalid gemspec in [... json-1.6.1.gemspec]: invalid date
#     format in specification: "2011-09-18 00:00:00.000000000Z"'
#     https://github.com/rubygems/rubygems/pull/196
patchfiles          patch-lib-rubygems-specification.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

# fix compile error against port:openssl10 @1.0.2u
patchfiles-append   patch-ext-openssl-openssl_missing.diff \
                    patch-ext_openssl_ossl_x509ext.c.diff

# fix extensions failing to configure due to not including the right headers
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
}

# ruby-1.9.3 supports Xcode-4.2 clang and gcc-4.2.
# ruby built with llvm-gcc will be broken.
# http://svn.ruby-lang.org/repos/ruby/tags/v1_9_3_0/NEWS
# http://redmine.ruby-lang.org/projects/ruby-trunk/wiki/SupportedPlatforms
compiler.blacklist-append llvm-gcc-4.2 macports-llvm-gcc-4.2

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=1.9 \
                    --with-rubylibprefix=${prefix}/lib/ruby1.9

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

# [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-1.9.pc
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?)$|\1\2|g} \
            ${worksrcpath}/rbconfig.rb \
            ${worksrcpath}/ruby-1.9.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/ruby1.9/${type}_ruby/1.9.1
            xinstall -m 0755 -d ${libdir}
            xinstall -m 0644 ${filespath}/${type}-specific.rb ${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/ruby1.9
    destroot.keepdirs-append ${destroot}${prefix}/libexec/ruby1.9

    # error at activate, for these case sensitive files
    foreach rdoc {Exception2MessageMapper/fail-i.yaml \
                  REXML/Parsers/XPathParser/predicate-i.yaml} {
        if {[file exists ${destroot}${prefix}/share/ri1.9/1.9.1/system/${rdoc}]} {
            delete ${destroot}${prefix}/share/ri1.9/1.9.1/system/${rdoc}
        }
    }
}

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
"

pre-activate {
    # Bug #3805 [ruby-core:32165]
    # http://redmine.ruby-lang.org/issues/show/3805
    #
    # the two gems of "rake", contained in ruby-1.9.2 and installed via gem
    # command, might be installed.
    # `port activate' fails when the two rake are installed.
    set rake_dup_gemspec ${prefix}/lib/ruby1.9/gems/1.9.1/specifications/rake-0.8.7.gemspec
    if {[file exists ${rake_dup_gemspec}]} {
        delete ${rake_dup_gemspec}
    }
}

variant c_api_docs description "Generate documentation for Ruby C API" {
    depends_build-append port:doxygen

    post-build {
        file mkdir ${workpath}/c_api
        set doxygen_conf ${name}-doxygen.conf
        file copy -force ${filespath}/${doxygen_conf} ${workpath}
        set doxygen_conf_path ${workpath}/${name}-doxygen.conf
        reinplace "s|%WORKPATH%|${workpath}|" ${doxygen_conf_path}
        reinplace "s|%WORKSRCPATH%|${worksrcpath}|" ${doxygen_conf_path}
        system "doxygen ${doxygen_conf_path}"
    }

    post-destroot {
        set api_docs ${destroot}${prefix}/share/doc/${name}-${version}/html/c_api
        xinstall -m 755 -d ${api_docs}
        xinstall -m 640 {*}[glob ${workpath}/c_api/html/*] ${api_docs}
    }
}

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
}

