# -*- 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.5.1
set releasedate     Oct2024
set fileversion     [join [lrange [split ${version} .] 0 1] _]
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            https://dpetersanderson.github.io/
master_sites        https://github.com/dpetersanderson/MARS/releases/download/v.${version}:mars \
                    https://github.com/tofi86/universalJavaApplicationStub/raw/refs/tags/v3.3.0/src:jappstub

distname            Mars${fileversion}
distfiles           Mars${fileversion}.jar:mars \
                    universalJavaApplicationStub:jappstub

use_zip             yes
extract.only        ${distname}.jar
extract.suffix      .jar

checksums           Mars4_5.jar \
                    rmd160  1d87e3fbe0c06af5917f56c4e282db89223b1b49 \
                    sha256  ac340b676ba2b62246b9df77e62f81ad4447bcfd329ab539716bcd09950b7096 \
                    size    4169142 \
                    universalJavaApplicationStub \
                    rmd160  45726412bc0c8c67fd8dd4282316dce871fe6c09 \
                    sha256  49bd31b90d41b1afd2d0e47636fea2461f5d16c2a55e0147fd82d77305514151 \
                    size    37008

depends_lib         bin:java:kaffe

use_configure       no

build               {}

# The only reason we extract the zip file is to get the image for the
# macOS app icon, 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
    pre-extract {
        extract.post_args-append    images/MarsThumbnail.gif
    }

    # 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 ${distpath}/universalJavaApplicationStub \
                        ${MarsAppPath}/Contents/MacOS
    }
}
