# -*- 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 88424 2012-01-02 01:29:32Z ryandesign@macports.org $

PortSystem              1.0
PortGroup               perl5 1.0

# replicate perl5.setup to create the subports
name                    p5-eperl
version                 2.2.14
revision                2
distname                eperl-${version}
license                 {Artistic GPL}
platforms               darwin
categories              www perl
maintainers             cal openmaintainer

description             Embedded Perl Language
long_description        ePerl interprets an ASCII file bristled with Perl 5 \
                        program statements by evaluating the Perl 5 code while \
                        passing through the plain ASCII data.

homepage                http://www.ossp.org/pkg/tool/eperl/

master_sites            ftp://ftp.ossp.org/pkg/tool/eperl/ \
                        ftp://freebsd.isc.org/pub/FreeBSD/ports/distfiles/ \
                        http://freebsd.isc.org/pub/FreeBSD/ports/distfiles/ 

checksums               rmd160  708c72d4a2bc494e9b3e953aa13efd0fe7933b98 \
                        sha256  a9add61ed59e3030027e0ff5c00195627d7309be48162c9b315f57d27730a295

use_configure           yes

# mostly copied from the perl5 port group
options perl5.default_brach perl5.branches
default perl5.branches {"5.8 5.10 5.12 5.14"}
default perl5.deafult_branch {[perl5_get_default_branch]}
proc perl5_get_default_branch {} {
    # use whatever ${prefix}/bin/perl5 was chosen, and if none, fall back to 5.12
    if {![catch {set val [lindex [split [exec ${prefix}/bin/perl5 -V:version] {'}] 1]}]} {
        return [join [lrange [split $val .] 0 1] .]
    } else {
        return 5.12
    }
}

proc perl5.extract_config {var {default ""}} {
    global perl5.bin

    if {[catch {set val [lindex [split [exec ${perl5.bin} -V:${var}] {'}] 1]}]} {
        set val ${default}
    }

    return $val
}

# Set some variables.
options perl5.version perl5.major perl5.arch perl5.lib perl5.archlib perl5.bin
default perl5.version {[perl5.extract_config version]}
default perl5.major {${perl5.default_branch}}
default perl5.arch {[perl5.extract_config archname ${os.platform}]}
default perl5.bin {${prefix}/bin/perl${perl5.major}}

# define installation libraries as vendor location
default perl5.lib {${prefix}/lib/perl5/vendor_perl/${perl5.version}}
default perl5.archlib {${perl5.lib}/${perl5.arch}}

## Most patches are copied from the Debian distribution, which uses patch
## files with a prefix
patch.pre_args          -p1
patchfiles              0001-copyright-updates.diff \
                        0002-eperl_2.2.14-15.2.diff \
                        0003-fix-spelling-errors.diff \
                        0004-document-double-shebang.diff \
                        0005-fix-preprocessor-comments-to-behave-as-documented.diff \
                        0006-fix-format-string-error-in-perl-stderr.diff \
                        0007-debian-2.2.14-16-modified-and-added-shtool.diff \
                        0008-wml-2.0.11-merge.diff

if {$subport != $name} {
    post-patch {
        # this file is added by 0007-*.diff, but must be executable
        file attributes "${worksrcpath}/etc/shtool" -permissions +x
    }
}

configure.args          --with-perl=${perl5.bin} \
                        --enable-debug

destroot.destdir        prefix=${destroot}${prefix} \
                        mandir=${destroot}${prefix}/share/man

test.run                yes

pre-configure {
   if ![exec ${perl5.bin} -MConfig -e "print \$Config{'useshrplib'} || 0"] {
        ui_error "${name} requires perl5 to be installed with the +shared variant."
        return -code error "perl5 must be +shared"
   }
}

# replaced_by and subport magic
if {[string match p5-* $name]} {
    set rootname        [string range $name 3 end]

    foreach v ${perl5.branches} {
        subport p${v}-${rootname} {
            depends_lib port:perl${v}
            perl5.major ${v}
        }
    }

    if {$subport == $name} {
        perl5.major
        distfiles
        supported_archs noarch
        replaced_by p[option perl5.default_branch]-${rootname}
        depends_lib port:p[option perl5.default_branch]-${rootname}
        use_configure no
        patch {}
        build {}
        destroot {
            xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
            system "echo $name is a stub port > ${destroot}${prefix}/share/doc/${name}/README"
        }
    }
} else {
    depends_lib port:perl${perl5.default_branch}
}
