# -*- 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                    python31
epoch                   1
# Remember to keep py31-tkinter and py31-gdbm's versions sync'd with this
version                 3.1.5
revision                5
set major               [lindex [split $version .] 0]
set branch              [join [lrange [split ${version} .] 0 1] .]
categories              lang
license                 PSF
platforms               darwin
maintainers             nomaintainer

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               md5    20dd2b7f801dc97db948dd168df4dd52 \
                        rmd160 021362658310f9ea5f6825bf0919f160304f4833 \
                        sha256 035c4370354d901924ce0fd8df65c5452839f590ec1b9c17fae2ada3e5f3b4d7

patchfiles              patch-setup.py.diff \
                        patch-Lib-cgi.py.diff \
                        patch-Lib-distutils-dist.py.diff \
                        patch-setup.py-disabled_modules.diff \
                        patch-libedit.diff \
                        patch-Lib-site.py-omit_local_site_packages.diff \
                        patch-Include-pyport.h.diff

# http://bugs.python.org/issue21811
patchfiles-append       patch-configure_configure.ac-yosemite_configure_fixes.diff \
                        patch-Lib-distutils-util.py_Mac-BuildScript-build-installer.py-yosemite_python_fixes.diff

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

configure.args          --enable-framework=${frameworks_dir} \
                        --enable-ipv6 \
                        --with-computed-gotos
configure.ccache        no

use_parallel_build      yes

post-patch {
   reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/Lib/cgi.py

   reinplace "s|/setup.py|/setup.py --no-user-cfg|" ${worksrcpath}/Makefile.pre.in

   # replace /Applications/ with ${applications_dir}/
   reinplace "s|\\(\[^a-zA-Z0-9\]\\)/Applications/|\\1${applications_dir}/|" \
      ${worksrcpath}/Mac/Makefile.in \
      ${worksrcpath}/Mac/Tools/Doc/setup.py \
      ${worksrcpath}/Mac/PythonLauncher/Makefile.in \
      ${worksrcpath}/Mac/BuildScript/build-installer.py

   reinplace "s|#!/Library/Frameworks|#!${frameworks_dir}|" \
      ${worksrcpath}/Mac/IDLE/IDLE.app/Contents/MacOS/IDLE

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

build.target            all

# test_cmd_line fails
# see http://bugs.python.org/issue4388 and
#     http://bugs.python.org/issue4474
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}"

select.group            python
select.file             ${filespath}/python[string map {. {}} ${branch}]

notes "
To make python ${branch} the default (i.e. the version you get when you run\
'python'), please run:

sudo port select --set ${select.group} [file tail ${select.file}]
"

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/

   # Without this, LINKFORSHARED is set to
   # ... $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
   # (this becomes Python.framework/Versions/3.1/Python) which doesn't
   # quite work (see ticket #15099); instead specifically list the
   # full path to the proper Python framework file (which becomes
   # ${prefix}/Library/Frameworks/Python.framework/Versions/3.1/Python)
   reinplace {s|^\(LINKFORSHARED=.*\)$(PYTHONFRAMEWORKDIR).*$|\1 $(PYTHONFRAMEWORKINSTALLDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)|} ${destroot}${framewdir}/lib/python${branch}/config/Makefile

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

platform darwin {
   post-configure {
      # See http://trac.macports.org/ticket/18376
      system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.ed"
   }
   post-patch {
		if {![file exists /usr/lib/libSystemStubs.a]} {
			reinplace s/-lSystemStubs//g ${worksrcpath}/configure
		}
   }
}

variant universal {
   post-patch {
      set universal_arch_flags {}
      set arch_run_32bit {}
      foreach arch ${universal_archs} {
         lappend universal_arch_flags -arch ${arch}
         if {${arch} eq "i386" || ${arch} eq "ppc"} {
            lappend arch_run_32bit -${arch}
         }
      }
      reinplace \
         "s|UNIVERSAL_ARCH_FLAGS=\".*\"|UNIVERSAL_ARCH_FLAGS=\"${universal_arch_flags}\"|" \
         ${worksrcpath}/configure
      if { ${arch_run_32bit} != "" } {
         reinplace \
            "s|ARCH_RUN_32BIT=\".*\"|ARCH_RUN_32BIT=\"arch ${arch_run_32bit}\"|" \
            ${worksrcpath}/configure
      }
   }
   if {${configure.sdkroot} != ""} {
      configure.args-append   --enable-universalsdk=${configure.sdkroot}
   } else {
      configure.args-append   --enable-universalsdk=/
   }
   post-configure {
      system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.h-universal.ed"
   }
}

variant ucs4 description {Use 4-byte Unicode characters} {
   configure.args-append   --with-wide-unicode
}

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