# -*- 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                mars
version             4.4
set releasedate     Aug2013
set fileversion     [join [split ${version} .] _]
revision            0
categories          java devel
platforms           {darwin any}
supported_archs     noarch
license             MIT
maintainers         {gmx.de:Torsten.Maehne @maehne} openmaintainer

description         MARS (MIPS Assembler and Runtime Simulator)

long_description    MARS is a lightweight interactive development environment \
                    (IDE) for programming in MIPS assembly language, intended \
                    for educational-level use with Patterson and Hennessy's \
                    Computer Organization and Design.

homepage            http://courses.missouristate.edu/kenvollmar/mars/
master_sites        ${homepage}MARS_${fileversion}_${releasedate}/
distname            Mars${fileversion}
use_zip             yes
extract.suffix      .jar

checksums           sha1    54544a2f212cc5cc96868efe7ea8973be3f778fb \
                    rmd160  15480082329e9b9cc8d7ea81dc84cd3922dfbefc

depends_lib         bin:java:kaffe

use_configure       no

build               {}

# The only reason we extract the zip file is to get the image for the
# OS X app icon (and patch the sources for the OS-X-specific jedit bug),
# so we don't need to extract it on other platforms.
if {${os.platform} ne "darwin"} {
    extract.only
    destroot {
        # Install Mars JAR
        xinstall -m 755 -d ${destroot}${prefix}/share/java
        xinstall -m 644 -W ${distpath} ${distname}.jar ${destroot}${prefix}/share/java

        # Install and configure mars command line wrapper
        xinstall -m 755 ${filespath}/mars.in ${destroot}${prefix}/bin/mars
        reinplace "s|@MARSJARPATH@|${prefix}/share/java/${distname}.jar|g" ${destroot}${prefix}/bin/mars
    }
}

platform darwin {
    depends_build-append        port:makeicns

    extract.mkdir               yes
# As long as the OS-X-specific jedit bug is present in the editor of Mars,
# we have to unpack the whole JAR.
#    pre-extract {
#        extract.post_args-append    images/MarsThumbnail.gif
#    }

    # The jedit-based editor of MARS requires a patch on OS X to enable
    # entering characters requiring the ALT key.
    patchfiles          patch-mars-venus-editors-jeditsyntax-DefaultInputHandler.java
    build {
        # Recompile patched source file
        # mars/venus/editors/jeditsyntax/DefaultInputHandler.java
        system "cd ${worksrcpath} && javac ${worksrcpath}/mars/venus/editors/jeditsyntax/DefaultInputHandler.java"
        # Create new JAR archive
        system "cd ${worksrcpath} && sh ${worksrcpath}/CreateMarsJar.bat"
    }

    destroot {
        # Install Mars JAR
        xinstall -m 755 -d ${destroot}${prefix}/share/java
        xinstall -m 644 ${worksrcpath}/Mars.jar ${destroot}${prefix}/share/java/${distname}.jar

        # Install and configure mars command line wrapper
        xinstall -m 755 ${filespath}/mars.in ${destroot}${prefix}/bin/mars
        reinplace "s|@MARSJARPATH@|${prefix}/share/java/${distname}.jar|g" ${destroot}${prefix}/bin/mars
    }

    # Build a nice .app
    post-destroot {
        set MarsAppPath ${destroot}${applications_dir}/MARS.app
        # Build paths
        xinstall -m 755 -d ${MarsAppPath}/Contents/Resources/Java \
                           ${MarsAppPath}/Contents/MacOS
        # Basic package information
        xinstall -m 644 ${filespath}/PkgInfo ${MarsAppPath}/Contents/PkgInfo
        xinstall -m 644 ${filespath}/Info.plist.in ${MarsAppPath}/Contents/Info.plist
        # Set the version in the Info.plist
        reinplace "s|@VERSION@|${version}|g" ${MarsAppPath}/Contents/Info.plist
        # Set the JAR file name
        reinplace "s|@MARSJARFILE@|${distname}.jar|g" ${MarsAppPath}/Contents/Info.plist
        # Generate the icon
        system "${prefix}/bin/makeicns -in ${worksrcpath}/images/MarsThumbnail.gif -out ${MarsAppPath}/Contents/Resources/Mars.icns"
        # Link to the JAR file
        ln -s ${prefix}/share/java/${distname}.jar \
                        ${MarsAppPath}/Contents/Resources/Java/${distname}.jar
        # Copy the stub app
        xinstall -m 755 "/System/Library/Frameworks/JavaVM.framework/Versions/A/Resources/MacOS/JavaApplicationStub" \
                        ${MarsAppPath}/Contents/MacOS
    }
}
