# -*- 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
PortGroup           app 1.1
PortGroup           github 1.0
PortGroup           java 1.0

github.setup        AppleCommander AppleCommander 1.9.0
revision            0
set gradle_version  7.3.1
java.version        11+

categories          emulators
platforms           {darwin any}
maintainers         {ryandesign @ryandesign} openmaintainer
license             GPL-2+
supported_archs     arm64 x86_64

description         GUI and CLI for manipulating Apple II disk images

long_description    ${name} is a GUI and command line for manipulating \
                    Apple DOS 3.3, ProDOS, and other formats of Apple \
                    II disk images. ${name} can read and display \
                    information about disk images, export files of \
                    various types, create disk images, import files \
                    into disk images, and has a rudimentary compiler. \
                    ${name} incorporates support for cc65, an improved \
                    command-line tool, and even an Ant interface.

homepage            https://applecommander.github.io
github.tarball_from archive
set main_distfile   ${distfiles}
set gradle_distfile gradle-${gradle_version}-bin.zip
extract.only        ${main_distfile}
master_sites-append https://services.gradle.org/distributions/:gradle
distfiles-append    ${gradle_distfile}:gradle

checksums           ${main_distfile} \
                    rmd160  836569f8f8d835fa2a6b5dd473ca9bbf98bdff5e \
                    sha256  9047e79af4de04f65e958ee6db46ca6134ae29a30857525214a15f68362a0640 \
                    size    6744978 \
                    ${gradle_distfile} \
                    rmd160  e32e691e3eec2132e2b4f2b2ba19f9f8b7f2b14f \
                    sha256  9afb3ca688fc12c761a0e9e4321e4d24e977a4a8916c8a768b1fe05ddb4d6b66 \
                    size    115766550

proc percent_encode {str} {
    return [subst [regsub -all {[^a-zA-Z0-9/.~_-]} $str {%[format %02x [scan {&} %c]]}]]
}

set gradle_home     ${workpath}/gradle
set swt_arch        [string map [list arm64 aarch64] ${configure.build_arch}]
set dirs            [list cli-ac cli-acx gui-swt-macosx-${swt_arch}]
set jars            [list ac acx macosx-${swt_arch}]
set guiwrapper      gui
set cliwrappers     [list ac acx]
set wrappers        [concat ${cliwrappers} ${guiwrapper}]

post-extract {
    # Upstream documentation shows how to create shell wrappers for the
    # jars but the build system doesn't make them so we do it ourselves.
    foreach f ${wrappers} {
        copy ${filespath}/${f}.in ${workpath}/${f}
    }
}

patchfiles          gradle-wrapper.properties.patch

post-patch {
    reinplace -W ${workpath} \
        "s|@SWT_ARCH@|${swt_arch}|g;s|@VERSION@|${version}|g" \
        {*}${wrappers}
    reinplace "s|@DISTPATH@|[percent_encode ${distpath}]|g" \
        ${build.dir}/gradle/wrapper/gradle-wrapper.properties
}

use_configure       no

build.env           "GRADLE_USER_HOME=${gradle_home}"

# This uses a local copy of a specific version of gradle. Ideally we
# should use the MacPorts gradle port but this build system is currently
# incompatible with gradle 8 or later:
# https://github.com/AppleCommander/AppleCommander/pull/105
build.cmd           ./gradlew

# Build only the modules we want (i.e. not the Linux and Windows GUIs).
build.target        {*}[lmap dir ${dirs} {expr {":app:${dir}:build"}}]

# The developer instructions show what Java command to run to start the
# GUI and the distribution includes a macOS icns file and other evidence
# that an application bundle could at one time be generated but that
# part of the build system has been neglected (was not converted from
# ant to gradle) and does not appear to be usable at this time.
# https://github.com/AppleCommander/AppleCommander/issues/112
app.executable      ${workpath}/${guiwrapper}
app.icon            ${build.dir}/mac/AppleCommander.icns
app.identifier      io.github.${name}

set appdir          ${applications_dir}/${app.name}.app
set contentsdir     ${appdir}/Contents
set javadir         ${contentsdir}/Java
set macosdir        ${contentsdir}/MacOS

destroot {
    # Upstream suggests installing the CLI programs as "ac" and "acx".
    # There is already an "ac" program included in macOS:
    # https://github.com/AppleCommander/AppleCommander/issues/57
    # so up to 1.7.0 Homebrew used to install it as "applecommander"
    # instead and MacPorts did as well. 1.7.0 introduced a second CLI
    # program "acx" and at that time the Homebrew formula changed the
    # old CLI program to "ac" and repurposed "applecommander" to launch
    # the GUI (which had previously been "applecommander-gui").
    foreach f ${cliwrappers} {
        xinstall ${workpath}/${f} ${destroot}${macosdir}
        ln -s ${macosdir}/${f} ${destroot}${prefix}/bin
    }
    # Retain the old "applecommander" name as an alias.
    ln -s ac ${destroot}${prefix}/bin/applecommander
    xinstall -d ${destroot}${javadir}
    xinstall -W ${destroot.dir}/app \
        {*}[lmap dir ${dirs} jar ${jars} {expr {"${dir}/build/libs/${name}-${jar}-${version}.jar"}}] \
        ${destroot}${javadir}
}
