# -*- 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$

PortSystem          1.0
PortGroup           python 1.0
PortGroup           bitbucket 1.0

bitbucket.setup     seanfarley mercurial 3.9.2
categories          devel python
license             GPL-2+
maintainers         sean openmaintainer
description         A fast, lightweight, distributed SCM system written in \
                    Python.
long_description    Mercurial is a fast, lightweight Source Control Management \
                    system designed for efficient handling of very large \
                    distributed projects. A distributed SCM tool is designed \
                    to support a model in which each Repository is loosely \
                    coupled to many others. Each Repository contains a \
                    complete set of metadata describing one or more projects. \
                    These repositories may be located almost anywhere. \
                    Individual developers only need access to their own \
                    repositories, not to a central one, in order to commit \
                    changes. Changes can be share via the builtin webserver, \
                    CGI, SSH or a stream of patch emails. Mercurial supports \
                    Apples' FileMerge for merges.

homepage            http://www.selenic.com/mercurial/
platforms           darwin
checksums           rmd160  7a2933a6c94fc4b513f7a539a2fc490349052e8a \
                    sha256  b8eb211e4b4da094b18e3bfd3503ddf0ec0b577a304aff11a19b2c107c7f0006

depends_build       port:py27-docutils

depends_run         path:share/curl/curl-ca-bundle.crt:curl-ca-bundle

patchfiles          patch-setup.py.diff

python.default_version 27

conflicts           mercurial-devel

build.cmd           make
build.target        all PYTHON=${python.bin}

subport mercurial-devel {
    bitbucket.setup     seanfarley mercurial 1c518d69d994
    name                mercurial-devel
    version             3.9.99
    revision            2

    conflicts           mercurial
    checksums           rmd160  ab131654dba0a6880f487c6628d57a05a51f5026 \
                        sha256  e73492f244d98509559ac7321550df22c28ce665bceb11513bf8861064ec9934
}

# chg is not installed by default yet
post-build {
    system -W ${worksrcpath}/contrib/chg "${build.cmd} CC=\"${configure.cc} \
        [get_canonical_archflags]\" CFLAGS=\"${configure.cflags}\" \
        CPPFLAGS='-DHGPATH=\\\"${prefix}/bin/hg\\\"' \
        PYTHON=${prefix}/bin/python2.7 HG=${prefix}/bin/hg \
        HGEXTDIR=${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages/hgext"
}

post-destroot {
    # configure ssl certificates
    xinstall -d -m 755 ${destroot}${prefix}/etc/mercurial
    xinstall -d -m 755 ${destroot}${prefix}/etc/mercurial/hgrc.d
    xinstall -d -m 755 ${destroot}${prefix}/share/doc/mercurial
    xinstall -m 644 ${filespath}/hgrc \
        ${destroot}${prefix}/etc/mercurial/hgrc.default
    reinplace "s|%%PREFIX%%|${prefix}|g" \
        ${destroot}${prefix}/etc/mercurial/hgrc.default

    # install html docs
    xinstall -m 644 -W ${worksrcpath}/doc hg.1.html hgrc.5.html hgignore.5.html \
            ${destroot}${prefix}/share/doc/mercurial

    # install man pages
    xinstall -m 644 -W ${worksrcpath}/doc hg.1 \
            ${destroot}${prefix}/share/man/man1
    xinstall -m 644 -W ${worksrcpath}/doc hgrc.5 hgignore.5 \
            ${destroot}${prefix}/share/man/man5

    # install contrib
    xinstall -d -m 755 ${destroot}${prefix}/share/mercurial
    file copy ${worksrcpath}/contrib ${destroot}${prefix}/share/mercurial/contrib

    if {[string first "-devel" $subport] == 0} {
        file copy ${worksrcpath}/contrib/mergetools.hgrc ${destroot}${prefix}/etc/mercurial/hgrc.d/mergetools.rc
    }

    # copy hgweb.cgi hgwebdir.cgi
    file copy ${worksrcpath}/hgweb.cgi ${destroot}${prefix}/share/mercurial/

    # copy hgk, the visual history browser
    file copy ${worksrcpath}/contrib/hgk ${destroot}${prefix}/bin/hgk

    # copy chg
    file copy ${worksrcpath}/contrib/chg/chg ${destroot}${prefix}/bin

    if {[variant_isset bash_completion]} {
        set completions_path ${prefix}/share/bash-completion/completions
        xinstall -d ${destroot}${completions_path}
        xinstall -m 644 ${worksrcpath}/contrib/bash_completion ${destroot}${completions_path}/mercurial
        ln -s ${completions_path}/mercurial ${destroot}${completions_path}/hg
    }

    if {[variant_isset zsh_completion]} {
        set site-functions ${destroot}${prefix}/share/zsh/site-functions
        xinstall -d ${site-functions}
        xinstall ${worksrcpath}/contrib/zsh_completion ${site-functions}/_mercurial
    }
}

post-activate {
    if {![file exists ${prefix}/etc/mercurial/hgrc]} {
        copy ${prefix}/etc/mercurial/hgrc.default ${prefix}/etc/mercurial/hgrc
    }
}

variant bash_completion {
    depends_run-append path:etc/bash_completion:bash-completion
}

variant zsh_completion description {Install mercurial zsh completion file} {
    depends_run-append path:${prefix}/bin/zsh:zsh
}