# -*- 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 91580 2012-04-06 06:26:53Z jmr@macports.org $

PortSystem      1.0

name            perl5.8
version         5.8.9
revision        7
set branch      [join [lrange [split ${version} .] 0 1] .]
categories      lang
license         {Artistic-1 GPL}
platforms       darwin freebsd linux
maintainers     ricci openmaintainer
description     Perl 5.8.x - Practical Extraction and Report Language

long_description \
    Perl is a general-purpose programming language originally developed \
    for text manipulation and now used for a wide range of tasks including \
    system administration, web development, network programming, GUI \
    development, and more.

distname        perl-${version}
use_bzip2       yes
homepage        http://www.perl.org/
master_sites    http://www.cpan.org/src/

checksums        \
    md5     1cb52a76ce77fa300218da96577793ec \
    sha1    19843b5a7585cf35d96c07dbcd419bbdd5813617 \
    rmd160  680004238d626ec985075e1b4519b7f0b34f57e5

patchfiles  patch-perl.c.diff patch-Configure.diff

platform darwin 11 {
    # bypass stupid check that assumes fprintf must be in libc.dylib
    patchfiles-append   patch-hints_darwin.sh-usenm.diff
}

post-patch {
    reinplace "s|/opt/local|${prefix}|g" ${worksrcpath}/Configure
    # Ensure that the ld value set in configure.args is used
    reinplace "s|ld='cc';||" ${worksrcpath}/hints/darwin.sh
}

configure.ccache    no
configure.distcc    no
configure.env       LC_ALL=C
configure.cmd       /bin/sh Configure
configure.pre_args
configure.universal_args-delete --disable-dependency-tracking
configure.args \
    -des \
    -D prefix='${prefix}' \
    -D scriptdir='${prefix}/bin' \
    -D cppflags="\${CPPFLAGS}" \
    -D ccflags="\${CFLAGS}" \
    -D ldflags="\${LDFLAGS}" \
    -D vendorprefix='${prefix}' \
    -D man1ext='1pm' \
    -D man3ext='3pm' \
    -D cc=\${CC} \
    -D ld=\${CC} \
    -D man1dir='${prefix}/share/man/man1p' \
    -D man3dir='${prefix}/share/man/man3p' \
    -D siteman1dir='${prefix}/share/man/man1' \
    -D siteman3dir='${prefix}/share/man/man3' \
    -D vendorman1dir='${prefix}/share/man/man1' \
    -D vendorman3dir='${prefix}/share/man/man3' \
    -D perlpath="${prefix}/bin/perl${branch}" \
    -D startperl="#!${prefix}/bin/perl${branch}"

# Allow perl to find p5-* port installed by the last version
if { [variant_isset threads] } {
    configure.args-append "-D inc_version_list=\"5.8.8 5.8.8/${os.platform}-thread-multi-2level\""
} else {
    configure.args-append "-D inc_version_list=\"5.8.8 5.8.8/${os.platform}-2level\""
}

set exclude_libs {bind gdbm db}

post-patch {
    foreach lib ${exclude_libs} {
        configure.args-append  -U i_${lib}
        reinplace "s|\\(libswanted=.*\\)${lib} *\\(.*\\)|\\1\\2|" ${worksrcpath}/Configure
    }
}

if {[variant_isset universal]} {
    post-configure {
        system "cd ${worksrcpath} && ed - ${worksrcpath}/config.h < ${filespath}/config.h.ed"
    }
}

post-build { 
    reinplace -E {s|-arch [a-z0-9_]+||g} \
        ${worksrcpath}/lib/Config_heavy.pl 
}

test.run            yes

post-destroot {
    # avoid conflicting with other perl versions
    # perl5 creates unversioned symlinks
    delete ${destroot}${prefix}/bin/perl
    foreach binFile [glob -directory "${destroot}${prefix}/bin" *] {
        if {$binFile != "${destroot}${prefix}/bin/perl${version}"} {
            move ${binFile} ${binFile}-${branch}
        }
    }
    move ${destroot}${prefix}/bin/perl${version} ${destroot}${prefix}/bin/${name}
    ln -s ${name} ${destroot}${prefix}/bin/perl${version}
    foreach man1File [glob -directory "${destroot}${prefix}/share/man/man1p" *] {
        regexp (.*)(\.1.*) ${man1File} -> program suffix
        move ${man1File} ${program}-${branch}${suffix}
    }
    foreach man3File [glob -directory "${destroot}${prefix}/share/man/man3p" *] {
        regexp (.*)(\.3.*) ${man3File} -> program suffix
        move ${man3File} ${program}-${branch}${suffix}
    }
}

variant threads description {Build with thread support} {
    configure.args-append   -Dusethreads
}

variant multiplicity description {Build with multiplicity support} {
    configure.args-append   -Dusemultiplicity=y
}

variant shared description {Build shared perl library} {
    configure.args-append   -Duseshrplib
}

variant gdbm description {Build with gdbm support} {
    depends_lib-append port:gdbm
    set exclude_libs [ldelete ${exclude_libs} gdbm]
}

variant db description {Build with Berkeley Data Base support} {
    depends_lib-append port:db47
    set exclude_libs [ldelete ${exclude_libs} db]
    
    post-patch {
        reinplace "s|${prefix}/include|${prefix}/include ${prefix}/include/db47|g" ${worksrcpath}/Configure
        reinplace "s|${prefix}/lib|${prefix}/lib ${prefix}/lib/db47|g" ${worksrcpath}/Configure
    }
}

livecheck.type  regex
livecheck.url   http://www.cpan.org/src
livecheck.regex {perl-(5\.8\.\d)\.}
