# -*- 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           select 1.0

name                python27
epoch               2
# Remember to keep py27-tkinter and py27-gdbm's versions sync'd with this
version             2.7.12
revision            2

set major           [lindex [split $version .] 0]
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          lang
license             PSF
platforms           darwin

maintainers         jwa openmaintainer

description         An interpreted, object-oriented programming language
long_description    Python is an interpreted, interactive, object-oriented \
                    programming language.

homepage            http://www.python.org/
master_sites        ${homepage}ftp/python/${version}/

distname            Python-${version}
use_xz              yes

checksums           rmd160  c330f6ac08ed67f307de0e726a288bab16c832d5 \
                    sha256  d7837121dd5652a05fef807c361909d255d173280c4e1a4ded94d73d80a1f978

patchfiles          patch-Makefile.pre.in.diff \
                    patch-setup.py.diff \
                    patch-setup.py-disabled_modules.diff \
                    patch-Lib-cgi.py.diff \
                    patch-Lib-ctypes-macholib-dyld.py.diff \
                    patch-configure.diff \
                    patch-libedit.diff \
                    patch-Include-pyport.h.diff \
                    omit-local-site-packages.patch \
                    improve-QuickTime.h-check.patch \
                    enable-loadable-sqlite-extensions.patch

depends_lib         port:gettext \
                    port:zlib \
                    path:lib/libssl.dylib:openssl \
                    port:sqlite3 \
                    port:db48 \
                    port:ncurses \
                    port:bzip2 \
                    port:libedit
depends_run         port:python_select \
                    port:python2_select

# disable-ipv6 for now, getaddrinfo-function missing
configure.args      --enable-framework=${frameworks_dir} \
                    --enable-ipv6

configure.cppflags-append -I${prefix}/include/db48
configure.ldflags-append -L${prefix}/lib/db48

configure.ccache    no

# signed integer overflow is normally undefined as per the C spec, but python
# needs it to be defined as twos-complement
configure.cflags-append -fwrapv

post-patch {
    reinplace "s|@@PREFIX@@|${prefix}|g" \
        ${worksrcpath}/Lib/cgi.py \
        ${worksrcpath}/Lib/ctypes/macholib/dyld.py
    reinplace "s|@@APPLICATIONS_DIR@@|${applications_dir}|" \
        ${worksrcpath}/configure

    # See https://trac.macports.org/changeset/37861
    reinplace "s|xargs -0 rm -r|/usr/bin/xargs -0 /bin/rm -r|g" \
        ${worksrcpath}/Mac/PythonLauncher/Makefile.in
}

post-configure {
    reinplace "s;/* #undef PY_FORMAT_LONG_LONG */;#define PY_FORMAT_LONG_LONG \"ll\";" pyconfig.h
}

build.target        all

test.run            yes
test.target         test

destroot.target     frameworkinstall maninstall

# ensure that correct compiler is used
build.args-append   MAKE="${build.cmd} CC=${configure.cc}"
destroot.args-append MAKE="${destroot.cmd} CC=${configure.cc}"

set pythonNoDot python[string map {. {}} $branch]
select.entries      [list python python-$pythonNoDot $pythonNoDot] \
                    [list python2 python2-$pythonNoDot $pythonNoDot]

notes "
To make this the default Python or Python 2 (i.e., the version run by\
the 'python' or 'python2' commands), run one or both of:

    sudo port select --set python $pythonNoDot
    sudo port select --set python2 $pythonNoDot

##############################################################
# IF YOU ARE USING PYTHON FROM THE TERMINAL, PLEASE INSTALL:
#   py[string map {. {}} $branch]-readline
# TO AVOID A LIBEDIT / PYTHON INTERACTION ISSUE.
# REF: https://trac.macports.org/ticket/48807
##############################################################
"

post-destroot {
    set framewpath ${frameworks_dir}/Python.framework
    set framewdir  ${framewpath}/Versions/${branch}

    foreach dir { Headers Resources Python Versions/Current } {
        file delete ${destroot}${framewpath}/${dir}
    }

    ln -s ${framewdir}/share/man/man1/python${branch}.1 ${destroot}${prefix}/share/man/man1/
    ln -s ${framewdir}/Python ${destroot}${prefix}/lib/libpython${branch}.dylib

    foreach unversionedFile {2to3 2to3-2 idle idle2 pydoc pydoc2 python python2 python-config python2-config pythonw pythonw2 smtpd.py smtpd2.py} {
        delete ${destroot}${prefix}/bin/${unversionedFile}
    }

    # remove -arch flags from the config
    reinplace -E {s|-arch [a-z0-9_]+||g} \
        ${destroot}${framewdir}/lib/python${branch}/config/Makefile \
        ${destroot}${framewdir}/lib/python${branch}/_sysconfigdata.py
}

platform darwin {
    post-patch {
        if {![file exists /usr/lib/libSystemStubs.a]} {
            reinplace s/-lSystemStubs//g ${worksrcpath}/configure
        }
    }
}

variant readline description {Use readline instead of libedit} {
    patchfiles-delete       patch-libedit.diff
    depends_lib-append      port:readline
    depends_lib-delete      port:libedit
}

variant universal {
    # This patch will need updating when 2.7.6 is released, as ARCH_RUN_32BIT
    # will once again be used for something. See ticket #39270.
    patchfiles-append patch-configure-universal.diff
    post-patch {
        reinplace \
            "s|__UNIVERSAL_ARCHFLAGS__|${configure.universal_cflags}|" \
            ${worksrcpath}/configure
    }
    if {${configure.sdkroot} != ""} {
        configure.args-append   --enable-universalsdk=${configure.sdkroot}
    } else {
        configure.args-append   --enable-universalsdk=/
    }
}

variant ucs4 description {Enable support for UCS4} {
    configure.args-append   --enable-unicode=ucs4
}

livecheck.type          regex
#livecheck.url           ${homepage}download/releases/
livecheck.url           ${homepage}downloads/
livecheck.regex         Python (${branch}(?:\\.\\d+)*)