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

PortSystem          1.0

name                squeak
version             3.9
revision            1
categories          lang
license             MIT
maintainers         nomaintainer
description         Full, portable, Smalltalk-80 system
long_description    Squeak is a full-featured implementation of the \
                    Smalltalk programming language and environment \
                    based on (and largely compatible with) the \
                    original Smalltalk-80 system.  Squeak has very \
                    powerful 2- and 3-D graphics, sound, video, MIDI, \
                    animation and other multimedia capabilities -- and \
                    one of the most impressive development \
                    environments ever created.  It also includes \
                    a customisable framework for creating dynamic HTTP \
                    servers and interactively extensible Web sites. \
                    The entire Squeak system is open source software, \
                    distributed freely with a liberal license.

# https://trac.macports.org/ticket/36369
platforms           {darwin < 12}

homepage            http://www.squeak.org

set squeak_vm_version 3.9-8
set squeak_img_version 3.9
set squeak_short_vrsn 39
set squeak_patch_no 7067
set squeak_vm_src Squeak-${squeak_vm_version}.src.tar.gz
set squeak_img_name Squeak${squeak_img_version}-final-${squeak_patch_no}
set squeak_img ${squeak_img_name}.image
set squeak_img_changes ${squeak_img_name}.changes
set squeak_img_src ${squeak_img_name}.zip

master_sites        http://www.squeakvm.org/unix/release/ \
                    ftp://ftp.squeak.org/3.8/unix-linux \
                    ftp://ftp.squeak.org/${squeak_img_version}/

distname            Squeak-${squeak_img_version}
distfiles           ${squeak_vm_src} ${squeak_img_src}

checksums           ${squeak_vm_src} md5 645ef7e321c61601c9c70d94fa9417e4 \
                    ${squeak_img_src} md5 30d991c418be1cd9c5d05fb87dea2f19

worksrcdir          Squeak-${squeak_vm_version}

extract.only        ${squeak_vm_src}
post-extract {
    file mkdir ${worksrcpath}/build
    # should do that but requires to rerun autoconf, I prefer to just patch inisqueak instead
    # reinplace "s|imgdir=`eval echo \${libdir}/squeak`|imgdir=`eval echo \${datadir}`|" ${worksrcpath}/platforms/unix/config/configure.ac
    # reinplace "s|plgdir=`eval echo \${imgdir}/\${VM_VERSION}`|plgdir=`eval echo \${libdir}/squeak/\${VM_VERSION}|" ${worksrcpath}/platforms/unix/config/configure.ac
}

configure.cmd       ../platforms/unix/config/configure
configure.dir       ${worksrcpath}/build
configure.args      --libdir=${prefix}/lib \
                    --mandir=${prefix}/share/man \
                    --without-quartz --without-x

build.dir           ${worksrcpath}/build
build.type          gnu

destroot.destdir    ROOT=${destroot}

post-destroot {
    set unzip "[binaryInPath "unzip"] -o"
    set gzip "[binaryInPath "gzip"] -f"

    set datadir ${prefix}/share/${name}

    # Have inisqueak look at the right place
    reinplace "s|MAJOR=3|MAJOR=${squeak_short_vrsn}|" ${worksrcpath}/build/inisqueak
    reinplace "s|imgdir=${prefix}/lib/squeak|imgdir=${datadir}|" ${worksrcpath}/build/inisqueak

    # Install inisqueak
    xinstall -m 755 ${worksrcpath}/build/inisqueak ${destroot}${prefix}/bin/

    # fix bad doc install path (should use --docdir or --datarootdir during configure)
    move ${destroot}${prefix}/doc/${name} ${destroot}${prefix}/share/doc/${name}

    # Recompress and install the default image
    system "cd ${worksrcpath} && ${unzip} ${distpath}/${squeak_img_src}"
    system "cd ${worksrcpath} && ${gzip} ${squeak_img_name}/${squeak_img}"
    system "cd ${worksrcpath} && ${gzip} ${squeak_img_name}/${squeak_img_changes}"
    xinstall -d ${destroot}${datadir}
    xinstall -m 644 ${worksrcpath}/${squeak_img_name}/${squeak_img}.gz \
        ${destroot}${datadir}
    xinstall -m 644 ${worksrcpath}/${squeak_img_name}/${squeak_img_changes}.gz \
        ${destroot}${datadir}
    xinstall -m 644 ${worksrcpath}/${squeak_img_name}/WelcomeSqueak${squeak_short_vrsn} \
        ${destroot}${datadir}
    xinstall -m 644 ${worksrcpath}/${squeak_img_name}/SqueakV${squeak_short_vrsn}.sources \
        ${destroot}${datadir}

    # Link compressed image and changes
    system "cd ${destroot}${datadir} && ln -s ${squeak_img}.gz squeak.image.gz"
    system "cd ${destroot}${datadir} && ln -s ${squeak_img_changes}.gz squeak.changes.gz"
}

variant quartz {
        configure.args-replace  --without-quartz \
                                --with-quartz
}

variant x11 {
        depends_lib-append      port:mesa \
                                port:xorg-libsm
        configure.args-replace  --without-x \
                                --with-x
}

if {![variant_isset quartz]} {
    default_variants            +x11
}
