# -*- 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
# $Id: Portfile 107821 2013-07-06 08:26:24Z ryandesign@macports.org $

PortSystem              1.0

name                    oracle-instantclient
categories              databases
platforms               macosx
maintainers             ryandesign
homepage                http://www.oracle.com/technetwork/database/features/instant-client/
use_zip                 yes

description \
    Oracle database connection libraries

long_description \
    Oracle Instant Client allows you to run your applications without installing the standard Oracle client or having an ORACLE_HOME.

if {${os.arch} eq "powerpc"} {
    epoch               1
    version             10.1.0.3
    if {${os.major} > 8} {
        revision        1
    }
    set library_version 10.1
    supported_archs     ppc
    universal_variant   no
    
    # The distfiles mirror has been configured not to mirror this port's files.
    master_sites        http://cmsrep.cern.ch/cmssw/cms/SOURCES/external/oracle/10.2.0.2/
    
    checksums           [suffix instantclient-basic-macosx-${version}] \
                        rmd160  d1f056f8f1b308c5493f4938b29b55fcb32452cf \
                        sha256  55dafdab1b7387b05226fd7eb0d86d52225a702a5e690fc82af42073f49e7725 \
                        [suffix instantclient-sdk-macosx-${version}] \
                        rmd160  c819db7f739b8ca468c8059dfbac31a6a6965938 \
                        sha256  350052656466cc2daecbb8b1c162ff444365def151636ba704f28ae5f05ca411
    
    set my_worksrcdir_format        instantclient%s
    set my_distname_format(ppc)     instantclient-%s-macosx-${version}
    set my_library_prefix(ppc)      /b/729
    
    livecheck.type      none
} else {
    version             11.2.0.3.0
    revision            1
    set library_version 11.1
    supported_archs     x86_64 i386
    variant universal {}
    
    # MacPorts won't be able to download these files.
    master_sites        http://download.oracle.com/otn/mac/instantclient/[join [lrange [split ${version} .] 0 3] {}]/
    
    checksums           [suffix instantclient-basic-macos.x32-${version}] \
                        rmd160  0ce640465611e0f5d3b9c3d421caa8c9531e573c \
                        sha256  ee11e57f03dd2a8b92abe09f28b1796a8956cb89d19dd015c9e9cd01701d3021 \
                        [suffix instantclient-sdk-macos.x32-${version}] \
                        rmd160  632bdd9998ca0b8d56965544ac3020b28a213086 \
                        sha256  2ad01e3fc2bcb0c1c4285a9b89037058b74039f6450d4b88176e8180ef2be763 \
                        [suffix instantclient-basic-macos.x64-${version}] \
                        rmd160  68edc03040258f5b8e7ccd4aa3c0f553d639ccf1 \
                        sha256  b86be0d924ec44d5d86ab212cc25b010f0b813ae0f3f22fd0cb0716e27c2d6ea \
                        [suffix instantclient-sdk-macos.x64-${version}] \
                        rmd160  934639cbc90b29eb1649bf31c2685870ffedd498 \
                        sha256  c2ddc7c6d93cd4e0f113362b642741d36d9e9052a9a0282ac583d086c191afcf
    
    set my_worksrcdir_format        instantclient_%s
    set my_distname_format(i386)    instantclient-%s-macos.x32-${version}
    set my_distname_format(x86_64)  instantclient-%s-macos.x64-${version}
    set my_library_prefix(i386)     /ade/b/233679120
    set my_library_prefix(x86_64)   /ade/b/2649109290
    
    if {${os.platform} eq "darwin" && ${os.major} < 9} {
        pre-fetch {
            ui_error "${name} on Intel requires OS X 10.5 or greater."
            return -code error "incompatible OS X version"
        }
    }
    
    livecheck.type          regex
    livecheck.url           http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
    livecheck.regex         instantclient-basic-macos.x64-(\[0-9.\]+)${extract.suffix}
}

# merge() requires universal_archs to be set correctly. Since we use merge()
# even when not building universal we must always set universal_archs correctly.
if {![variant_exists universal] || ![variant_isset universal]} {
    configure.universal_archs ${configure.build_arch}
}

distfiles
foreach my_arch ${configure.universal_archs} {
    if {[info exists my_distname_format(${my_arch})]} {
        foreach my_distfile_type {basic sdk} {
            distfiles-append [suffix [format $my_distname_format(${my_arch}) ${my_distfile_type}]]
        }
    }
}

post-fetch {
    set bad_distfiles {}
    foreach distfile ${distfiles} {
        set distfile [strsed ${distfile} {/:.*$//}]
        if {![catch {strsed [exec [findBinary file $portutil::autoconf::file_path] ${distpath}/${distfile} --brief --mime] {s/;.*$//}} mimetype] && ${mimetype} eq "text/html"} {
            lappend bad_distfiles ${distfile}
            file delete ${distpath}/${distfile}
        }
    }
    if {[llength ${bad_distfiles}] > 0} {
        ui_error "MacPorts was not able to download the necessary distfiles for ${name}."
        ui_error "Please visit this URL:"
        ui_error ""
        ui_error "    ${homepage}"
        ui_error ""
        ui_error "and download these files manually:"
        ui_error ""
        foreach distfile ${bad_distfiles} {
            ui_error "    ${distfile}"
        }
        ui_error ""
        ui_error "and place them in this directory:"
        ui_error ""
        ui_error "    ${distpath}"
        return -code error "missing distfiles"
    }
}

extract {
    set my_build_dir ${extract.dir}/build
    xinstall -d ${my_build_dir}
    foreach my_arch ${configure.universal_archs} {
        foreach my_distfile_type {basic sdk} {
            system "${extract.cmd} ${extract.pre_args} ${extract.post_args} ${distpath}/[suffix [format $my_distname_format(${my_arch}) ${my_distfile_type}]]"
        }
        move ${extract.dir}/[format ${my_worksrcdir_format} [join [lrange [split ${version} .] 0 1] "_"]] ${my_build_dir}/${my_arch}
    }
}

use_configure           no

set lib_dir             ${prefix}/lib/oracle

build {
    foreach my_arch ${configure.universal_archs} {
        # Oracle builds the libraries with strange install_names; fix them.
        # For each dylib, change the directory of its install_name to ${lib_dir}.
        foreach lib [glob -directory ${workpath}/build/${my_arch} *.dylib*] {
            system "install_name_tool -id ${lib_dir}/[strsed ${lib} /^.*\\///] ${lib}"
            
            # Then for each dependent dylib with a strange install_name that
            # this dylib references, fix the reference to use ${lib_dir}.
            foreach dep [exec otool -L ${lib}] {
                if [string match "$my_library_prefix(${my_arch})/*" ${dep}] {
                    system "install_name_tool -change ${dep} ${lib_dir}/[strsed ${dep} /^.*\\///] ${lib}"
                }
            }
            
            # libociei is special: it's not linked to; it's dynamically loaded at
            # runtime by libclntsh. Oracle says you have to set DYLD_LIBRARY_PATH
            # to include ${lib_dir}, but adding an rpath works too—but rpath only
            # exists on Leopard and newer.
            if {${os.major} > 8 && [string match "*/libclntsh.dylib*" ${lib}]} {
                system "install_name_tool -add_rpath ${lib_dir} ${lib}"
            }
        }
    }
}

destroot {
    foreach my_arch ${configure.universal_archs} {
        set my_destroot ${workpath}/pre-dest/${my_arch}
        set my_worksrcpath ${workpath}/build/${my_arch}
        xinstall -d ${my_destroot}${lib_dir}
        eval xinstall \
            [glob -directory ${my_worksrcpath} *.dylib*] \
            [glob -directory ${my_worksrcpath} *.jar] \
            ${my_destroot}${lib_dir}
        
        # You would think the includes should go in ${prefix}/include/oracle, but
        # the "instantclient layout" dictates they must be in ${lib_dir}/sdk/include;
        # ports like php-oracle will expect them there.
        xinstall -d ${my_destroot}${lib_dir}/sdk
        copy ${my_worksrcpath}/sdk/include ${my_destroot}${lib_dir}/sdk
    }
    
    merge ${workpath}/pre-dest
    
    # php-oracle complains without a libclntsh.dylib symlink.
    ln -s libclntsh.dylib.${library_version} ${destroot}${lib_dir}/libclntsh.dylib
    
    # Add a libocci.dylib symlink too for good measure.
    ln -s libocci.dylib.${library_version} ${destroot}${lib_dir}/libocci.dylib
}

if {${os.major} > 8} {
    notes-append "
If you previously set DYLD_LIBRARY_PATH=${lib_dir} in your environment,\
you can remove it\; it is no longer needed with this version of ${name}.
"
} else {
    notes-append "
To use ${name}, add this command to your environment:

export DYLD_LIBRARY_PATH=${lib_dir}
"
}
