# $Id$

PortSystem 1.0
PortGroup select 1.0

name                    python24
version                 2.4.6
revision                11
set major               [lindex [split $version .] 0]
set branch              [join [lrange [split ${version} .] 0 1] .]
categories              lang
license                 PSF
platforms               darwin
maintainers             fourdigits.nl:roel 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}/ \
                        ftp://ftp.python.org/pub/python/${version}/ \
                        ftp://ftp.fastorama.com/mirrors/ftp.python.org/pub/python/${version}/ \
                        ftp://ftp.python.jp/pub/python/${version}/

distname                Python-${version}
use_bzip2               yes

checksums               md5     76083277f6c7e4d78992f36d7ad9018d \
                        sha1    cb1972a554a458f6a26d3e047b359251865d7c96 \
                        rmd160  0687989193dec2ac108142759281be7ddcf7f31e

patchfiles              patch-configure \
                        patch-Makefile.pre.in \
                        patch-Lib-cgi.py \
                        patch-Lib-site.py \
                        patch-setup.py \
                        patch-Include-pyport.h \
                        patch-Mac-OSX-Makefile.in \
                        patch-Mac-OSX-IDLE-Makefile.in \
                        patch-Mac-OSX-PythonLauncher-Makefile.in \
                        patch-fwrapv.diff

depends_lib             port:gettext path:lib/libssl.dylib:openssl
depends_run             port:python_select

configure.args          --with-cxx=${configure.cxx} \
                        --enable-shared \
                        --enable-framework=${frameworks_dir} \
                        --mandir=${prefix}/share/man \
                        --without-readline \
                        --disable-tk \
                        --enable-ipv6

use_parallel_build      no

post-patch {
    reinplace "s|__PREFIX__|${prefix}|g" ${worksrcpath}/Lib/cgi.py \
        ${worksrcpath}/Lib/site.py ${worksrcpath}/setup.py
    reinplace "s|__FRAMEWORKS_DIR__|${frameworks_dir}|g" \
        ${worksrcpath}/Mac/OSX/Makefile.in
    reinplace "s|__APPLICATIONS_DIR__|${applications_dir}|g" \
        ${worksrcpath}/Mac/OSX/IDLE/Makefile.in \
        ${worksrcpath}/Mac/OSX/Makefile.in \
        ${worksrcpath}/Mac/OSX/PythonLauncher/Makefile.in
    reinplace "s|__BUILD_ARCH__|${build_arch}|" ${worksrcpath}/configure
    reinplace "s|__UNIVERSAL_CFLAGS__|${configure.universal_cflags}|" ${worksrcpath}/configure
    reinplace "s|__UNIVERSAL_LDFLAGS__|${configure.universal_ldflags}|" ${worksrcpath}/configure
}

build.target            all

# Workaround for case-sensitive file systems
post-build {
    if { ![file exists ${worksrcpath}/python.exe] } {
        ln -s python ${worksrcpath}/python.exe
    }
}

test.run                yes
test.target             test

destroot.target         frameworkinstall maninstall

post-extract {
    copy ${filespath}/python[string map {. {}} ${branch}]-noframework ${workpath}/python[string map {. {}} ${branch}]
}
select.group            python
if {${os.subplatform} ne "macosx"} {
    select.file         ${workpath}/python[string map {. {}} ${branch}]
}

platform macosx {
    if {(![variant_isset universal] && ![string match *64* $build_arch]) || ([variant_isset universal] && ![string match *64* $universal_archs])} {
        select.file     ${filespath}/python[string map {. {}} ${branch}]
        post-destroot {
            set framewpath ${frameworks_dir}/Python.framework
            set framewdir  ${framewpath}/Versions/${branch}

            foreach dir { lib include } {
                file rename ${destroot}${framewdir}/${dir}/python${branch} ${destroot}${prefix}/${dir}
                ln -s ${prefix}/${dir}/python${branch} ${destroot}${framewdir}/${dir}/python${branch}
            }
            # Since the lib/python${branch} dir was just moved above and
            # libpython2.4.a in lib/python${branch}/config is relative, we need
            # to repoint it here
            file delete ${destroot}${prefix}/lib/python${branch}/config/libpython${branch}.a
            ln -s ${framewdir}/Python ${destroot}${prefix}/lib/python${branch}/config/libpython${branch}.a

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

            # delete symlinks without version suffix, use python_select instead to choose version
            foreach bin { python pythonw idle pydoc smtpd.py } {
                file delete ${destroot}${prefix}/bin/${bin}
            }
            foreach bin [list python${branch} pythonw${branch} idle${branch} pydoc${branch} smtpd${branch}.py] {
                file rename -force ${destroot}${framewdir}/bin/${bin} ${destroot}${prefix}/bin
                ln -s ${prefix}/bin/${bin} ${destroot}${framewdir}/bin/${bin}
            }

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

            # Without this, LINKFORSHARED is set to
            # ... $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
            # (this becomes Python.framework/Versions/2.4/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/2.4/Python)
            reinplace {s|^\(LINKFORSHARED=.*\)$(PYTHONFRAMEWORKDIR).*$|\1 $(PYTHONFRAMEWORKINSTALLDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)|} ${destroot}${prefix}/lib/python${branch}/config/Makefile
        }
    } else {
        # 64-bit, can't build mac-specific stuff
        configure.args-delete --enable-framework=${frameworks_dir}
        configure.args-append --disable-toolbox-glue
        destroot.target       install maninstall

        post-destroot {
            # delete symlinks without version suffix, use python_select instead to choose version
            file delete ${destroot}${prefix}/bin/python
            foreach bin { idle pydoc } {
                move ${destroot}${prefix}/bin/${bin} ${destroot}${prefix}/bin/${bin}${branch}
            }
            move ${destroot}${prefix}/bin/smtpd.py ${destroot}${prefix}/bin/smtpd${branch}.py
    
            file rename ${destroot}${prefix}/share/man/man1/python.1 ${destroot}${prefix}/share/man/man1/python${branch}.1
        }

        select.file         ${workpath}/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}]
"

platform darwin {
    post-configure {
        # See http://trac.macports.org/ticket/18376
        system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.ed"
    }
    if {${os.major} >= 9} {
    	configure.cppflags-append       -D__DARWIN_UNIX03
    }
    if {${os.major} >= 10} {
    	post-configure {
			reinplace "s|#define _POSIX_C_SOURCE 200112L|#undef _POSIX_C_SOURCE|g" ${worksrcpath}/pyconfig.h
			reinplace "s|#define _XOPEN_SOURCE 600|#undef _XOPEN_SOURCE|g" ${worksrcpath}/pyconfig.h  
		}
		patchfiles-append   patch-macosmodule.diff \
							patch-mactoolboxglue.diff \
							patch-pymactoolbox.diff
    }
    post-patch {
        if {![file exists /usr/lib/libSystemStubs.a]} {
            reinplace s/-lSystemStubs//g ${worksrcpath}/configure
        }
    }
    post-destroot {
        # remove -arch flags from the config
        reinplace -E {s|-arch [a-z0-9_]+||g} \
            ${destroot}${prefix}/lib/python${branch}/config/Makefile
    }
}

platform puredarwin {
    configure.args-delete   --enable-framework=${frameworks_dir}
    configure.args-append   --disable-toolbox-glue --disable-framework
    destroot.target         install maninstall

    post-destroot {
        # delete symlinks without version suffix, use python_select instead to choose version
        file delete ${destroot}${prefix}/bin/python
        foreach bin { idle pydoc } {
            move ${destroot}${prefix}/bin/${bin} ${destroot}${prefix}/bin/${bin}${branch}
        }
        move ${destroot}${prefix}/bin/smtpd.py ${destroot}${prefix}/bin/smtpd${branch}.py

        file rename ${destroot}${prefix}/share/man/man1/python.1 ${destroot}${prefix}/share/man/man1/python${branch}.1
    }
}

variant universal {
   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"
   }
}

livecheck.type          regex
livecheck.url           ${homepage}download/releases/
livecheck.regex         Python (${branch}.\[0-9\]+)