# -*- 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 104041 2013-03-14 00:27:36Z jmr@macports.org $

PortSystem          1.0

name                docbook-xml
revision            1
set revision-docbook-xml 2
set docbook-xml-versions {
    4.1.2
    {
        rmd160  89af805434b21f12f7be7ef58cf52edeb458e0c9 \
        sha256  30f0644064e0ea71751438251940b1431f46acada814a062870f486c772e7772
    }
    4.2
    {
        rmd160  3ba17bf96ab2ab8fe6dc9ff82da8e22aaf9ce5e0 \
        sha256  acc4601e4f97a196076b7e64b368d9248b07c7abf26b34a02cca40eeebe60fa2
    }
    4.3
    {
        rmd160  deeb29ed336bcce6ed199c516f01bb1542ea16ee \
        sha256  23068a94ea6fd484b004c5a73ec36a66aa47ea8f0d6b62cc1695931f5c143464
    }
    4.4
    {
        rmd160  691a9230442be14d019af9d07e94f269824d85cc \
        sha256  02f159eb88c4254d95e831c51c144b1863b216d909b5ff45743a1ce6f5273090
    }
    4.5
    {
        rmd160  723347ef17950c5c8e81a7c9d44812261dcadc5d \
        sha256  4e4e037a2b83c98c6c94818390d4bdd3f6e10f6ec62dd79188594e26190dc7b4
    }
    5.0
    {
        rmd160  3fc1b6a1c6f94b77223ad9139676d5b8d8f36786 \
        sha256  3dcd65e1f5d9c0c891b3be204fa2bb418ce485d32310e1ca052e81d36623208e
    }
}
version             [lindex ${docbook-xml-versions} end-1]
categories          textproc
platforms           darwin
license             BSD
maintainers         fourpalms.org:lockhart openmaintainer
supported_archs     noarch
description         Install all the versioned docbook-xml-* ports.
long_description    ${description}

homepage            http://www.docbook.org

foreach {docbook-xml-version docbook-xml-checksums} ${docbook-xml-versions} {

    subport docbook-xml-${docbook-xml-version} {

    	if {[vercmp ${docbook-xml-version} 4.1.2] == 0} {

            distname            docbkx412
            extract.mkdir       yes

            depends_lib-append  port:docbook-xml-4.2

            post-extract {

                copy ${prefix}/share/xml/docbook/4.2/catalog.xml \
                    ${worksrcpath}/
            }
            post-patch {

                reinplace "s,V4.2 ..,V4.1.2 ,g" \
                    ${worksrcpath}/catalog.xml
                reinplace "s,V4.2,V4.1.2,g" \
                    ${worksrcpath}/catalog.xml
            }
        } elseif {[vercmp ${docbook-xml-version} 5.0] >= 0} {

            distname            docbook-${docbook-xml-version}
        } else {

            distname            docbook-xml-${docbook-xml-version}
            extract.mkdir       yes
        }

        foreach part [string trim ${docbook-xml-checksums}] {

            checksums-append    $part
        }

        description         Version ${docbook-xml-version} of the DocBook XML DTDs
        long_description    Version ${docbook-xml-version} of the DocBook XML DTDs, which provide a \
                            structured, semantic markup format for books and documentation, \
                            especially for computer hardware and software.
        homepage            http://www.docbook.org/xml/${docbook-xml-version}
        master_sites        ${homepage}

        depends_run         port:xmlcatmgr

        use_zip             yes
        use_configure       no
        build               {}

        set install_dir    ${prefix}/share/xml/docbook/${docbook-xml-version}

        # xmlcatmgr as installed by MacPorts defaults to using
        # ${prefix}/etc/xml/catalog (for XML) and ${prefix}/etc/sgml/catalog (for
        # SGML) if no catalog is specified, but we'll specify the path just in case
        # users have another installation of xmlcatmgr and happen to have it before
        # ${prefix}/bin in their PATH.
        set catalog.xml    ${prefix}/etc/xml/catalog
        set xml.args       "-c ${catalog.xml}"
        set catalog.sgml   ${prefix}/etc/sgml/catalog
        set sgml.args      "-s -c ${catalog.sgml}"

        destroot {

            set destroot_dir    ${destroot}${install_dir}
            set destroot_files  [readdir ${worksrcpath}]
            xinstall -m 755 -d ${destroot_dir}
            foreach file ${destroot_files} {

                copy ${worksrcpath}/${file} ${destroot_dir}
            }
        }

        post-activate {

            # XML catalog
            # Make the directory if it doesn't exist
            if {![file exists ${prefix}/etc/xml]} {

                xinstall -m 755 -d ${prefix}/etc/xml
            }

            # Create the catalog file if it doesn't exist
            if {![file exists ${catalog.xml}]} {

                system "xmlcatmgr ${xml.args} create"
            }

            # Add the nextCatalog entry to the catalog if it doesn't exist
            if {[catch {exec xmlcatmgr ${xml.args} lookup ${install_dir}/catalog.xml}]} {

                system "xmlcatmgr ${xml.args} add nextCatalog ${install_dir}/catalog.xml"
            }

            # SGML catalog
            # SGML catalog support is only in the 4.x transitional versions...
            if {[file exists ${install_dir}/docbook.cat]} {
                # Make the directory if it doesn't exist
                if {![file exists ${prefix}/etc/sgml]} {

                    xinstall -m 755 -d ${prefix}/etc/sgml
                }

                # Create the catalog file if it doesn't exist
                if {![file exists ${catalog.sgml}]} {

                    system "xmlcatmgr ${sgml.args} create"
                }

                # Add the CATALOG entry to the catalog if it doesn't exist
                if {[catch {exec xmlcatmgr ${sgml.args} lookup ${install_dir}/docbook.cat}]} {

                    system "xmlcatmgr ${sgml.args} add CATALOG ${install_dir}/docbook.cat"
                }
            }
        }

        # This will remove the catalog entry for this port.
        post-deactivate {

            system "xmlcatmgr ${xml.args} remove nextCatalog ${install_dir}/catalog.xml"
            # Remove the CATALOG entry from the catalog
            # SGML catalog support is only in the 4.x transitional versions...
            system "xmlcatmgr ${sgml.args} remove CATALOG ${install_dir}/docbook.cat"
        }
    }
}

if {${name} == ${subport}} {

    revision            ${revision-docbook-xml}
    use_configure       no
    build               {}
    distfiles
    foreach {docbook-xml-version docbook-xml-checksums} ${docbook-xml-versions} {

        depends_lib-append  port:docbook-xml-${docbook-xml-version}
    }
    destroot {

        set docdir ${prefix}/share/doc/${name}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 ${filespath}/README ${destroot}${docdir}
    }
}

livecheck.type      regex
livecheck.url       http://www.docbook.org/xml/
livecheck.regex     {>(\d+(?:\.\d+)*)/<}
