# -*- 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 119973 2014-05-12 11:49:53Z ciserlohn@macports.org $

PortSystem          1.0

name                git-core
version             1.9.3
description         A fast version control system
long_description    Git is a fast, scalable, distributed open source version \
                    control system focusing on speed and efficiency.
maintainers         ciserlohn
categories          devel
license             GPL-2 LGPL-2.1+
platforms           darwin
homepage            http://git-scm.com/
master_sites        https://www.kernel.org/pub/software/scm/git/
distname            git-${version}
distfiles           git-${version}${extract.suffix} \
                    git-manpages-${version}${extract.suffix}

checksums           git-${version}${extract.suffix} \
                    rmd160  a0d31f4e454b83a98569ef0af3c9a206b78eea3d \
                    sha256  71bca677efa3b87a060cba1de9e3512e7ab0f09197ef95d648e5280d9a6d1357 \
                    git-manpages-${version}${extract.suffix} \
                    rmd160  8424418559cc5ba13b6712826ea6e46448755093 \
                    sha256  43bfb09c072ccfe0ed4b0ad30d04c8f34b6af4d2f52cd8dfdb48787cc9c8509d \

depends_run         port:rsync

depends_lib         port:curl \
                    port:zlib \
                    port:openssl \
                    port:expat \
                    port:libiconv

if {[variant_isset perl5_12]} {
    set perl_version 5.12
} elseif {[variant_isset perl5_14]} {
    set perl_version 5.14
} else {
    set perl_version 5.16
    default_variants +perl5_16
}


depends_run-append  port:p${perl_version}-error \
                    port:p${perl_version}-net-smtp-ssl \
                    port:p${perl_version}-term-readkey

depends_lib-append  port:perl${perl_version}

patchfiles          patch-Makefile.diff git-subtree.1.diff
patch.pre_args      -p1

extract.only        git-${version}${extract.suffix} \
                    git-manpages-${version}${extract.suffix}

use_configure       no

variant universal   {}

set CFLAGS          "-I. -Wall -O2 -I${prefix}/include [get_canonical_archflags cc]"
set LDFLAGS         "-L${prefix}/lib [get_canonical_archflags ld]"

build.args          CFLAGS="${CFLAGS}" \
                    LDFLAGS="${LDFLAGS}" \
                    CC=${configure.cc} \
                    prefix=${prefix} \
                    CURLDIR=${prefix} \
                    OPENSSLDIR=${prefix} \
                    ICONVDIR=${prefix} \
                    PERL_PATH="${prefix}/bin/perl${perl_version}" \
                    NO_FINK=1 \
                    NO_DARWIN_PORTS=1 \
                    NO_R_TO_GCC_LINKER=1 \
                    V=1

test.run            yes
test.cmd            make
test.target         test
test.dir            ${worksrcpath}
pre-test {
    eval test.args  ${build.args}
}

destroot.target     install
pre-destroot {
    eval destroot.args  ${build.args}
    xinstall -m 644 ${worksrcpath}/contrib/subtree/git-subtree.1 ${workpath}/man1
}

set docdestroot ${destroot}${prefix}/share/doc/${name}

post-destroot {
    foreach f {1 5 7} {
        xinstall -d ${destroot}${prefix}/share/man/man${f}
        foreach m [glob -directory ${workpath} man${f}/*.${f}] {
            xinstall ${m} ${destroot}${prefix}/share/man/man${f}
        }
    }
    if {![variant_isset svn]} {
        system "rm ${destroot}${prefix}/libexec/git-core/git-svn*"
    }
    xinstall -d ${docdestroot}

    fs-traverse badfile ${destroot} {
        if {[string last perllocal.pod ${badfile}] != -1} {
            ui_info "Removing ${badfile}"
            file delete ${badfile}
        }
    }

    set completions_path ${destroot}${prefix}/share/bash-completion/completions
    xinstall -d ${completions_path}
    xinstall -m 644 ${worksrcpath}/contrib/completion/git-completion.bash \
        ${completions_path}/git
   
    set share_path ${destroot}${prefix}/share/${name}
    xinstall -d ${share_path}
    xinstall -m 644 ${worksrcpath}/contrib/completion/git-prompt.sh \
        ${share_path}/git-prompt.sh

    xinstall -m 755 ${worksrcpath}/contrib/subtree/git-subtree.sh \
        ${destroot}${prefix}/libexec/git-core/git-subtree

    file delete -force ${share_path}/contrib
    copy ${worksrcpath}/contrib ${share_path}

    xinstall -m 755 -d ${destroot}${prefix}/share/emacs/site-lisp/
    eval xinstall -m 644 [glob ${worksrcpath}/contrib/emacs/*.el] \
        ${destroot}${prefix}/share/emacs/site-lisp/

}

if {![variant_isset python26] && ![variant_isset python27]} {
    default_variants    +python27
}

variant python26 conflicts python27 description {Use Python 2.6} {
    build.args-append       PYTHON_PATH="${prefix}/bin/python2.6"
    depends_lib-append      port:python26
}

variant python27 conflicts python26 description {Use Python 2.7} {
    build.args-append       PYTHON_PATH="${prefix}/bin/python2.7"
    depends_lib-append      port:python27
}

variant perl5_16 conflicts perl5_14 perl5_12 description {use perl 5.16} {}
variant perl5_14 conflicts perl5_16 perl5_12 description {use perl 5.14} {}
variant perl5_12 conflicts perl5_16 perl5_14 description {use perl 5.12} {}

variant pcre {
    build.args-append       LIBPCREDIR=${prefix} USE_LIBPCRE=1
    depends_lib-append      port:pcre
}

variant doc description {Install HTML and plaintext documentation} {
    distfiles-append        git-htmldocs-${version}${extract.suffix}
    checksums-append        git-htmldocs-${version}${extract.suffix} \
                            rmd160  a7e50ac9456c6bd9defa8cad30059abed8e44320 \
                            sha256  d23f3f3ccd421706f8805c40ee199fb1d3dda7e6720eb8b260f3dbed20f2536f

    patchfiles-append       git-subtree.html.diff

    post-extract {
        file mkdir ${workpath}/htmldocs
        system -W ${workpath}/htmldocs "${extract.cmd} ${extract.pre_args} \
            ${distpath}/git-htmldocs-${version}${extract.suffix} \
            ${extract.post_args}"
    }

    pre-destroot {
        xinstall -m 644 ${worksrcpath}/contrib/subtree/git-subtree.html ${workpath}/htmldocs
    }

    post-destroot {
        foreach f [glob ${workpath}/htmldocs/*] {
            file delete -force "${docdestroot}/[file tail ${f}]"
            file attribute ${f} -permissions ugo+r
            copy ${f} ${docdestroot}
        }
    }
}

variant gitweb description {Install gitweb.cgi} {
    build.target-append     gitweb/gitweb.cgi

    post-destroot {
        xinstall -d ${destroot}${prefix}/share/${name}/gitweb
        xinstall -W ${worksrcpath}/gitweb \
            gitweb.cgi \
            ${destroot}${prefix}/share/${name}/gitweb
        xinstall -m 444 -W ${worksrcpath}/gitweb/static \
            gitweb.css \
            gitweb.js \
            git-favicon.png \
            git-logo.png \
            ${destroot}${prefix}/share/${name}/gitweb
        xinstall -d ${docdestroot}/gitweb
        xinstall -m 444 -W ${worksrcpath}/gitweb README INSTALL \
            ${docdestroot}/gitweb
    }
}

variant svn description {Bi-directional subversion repository support} {
    depends_run-append  port:subversion \
                        port:p${perl_version}-libwww-perl \
                        port:p${perl_version}-svn-simple
}

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


variant credential_osxkeychain description {Install git credential-osxkeychain utility from contrib} {

    post-build {
        system -W "${worksrcpath}/contrib/credential/osxkeychain" "make [join ${build.args}]"
    }

    pre-destroot {
        xinstall -m 755 "${worksrcpath}/contrib/credential/osxkeychain/git-credential-osxkeychain" \
            "${destroot}${prefix}/bin/"
    }
}

platform darwin 8 {
    build.args-append   NO_APPLE_COMMON_CRYPTO=1
}

default_variants    +doc +pcre +credential_osxkeychain

livecheck.type          regexm
livecheck.regex         {<span class="version">.*?(\d\.\d\.\d).*?</span>}
