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

github.setup        VCVRack Rack 1.1.6 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                vcvrack

categories          audio
license             GPL-3

set rack_fundamental_version    1.3.1

homepage            https://vcvrack.com/

description         VCV Rack is an open-source virtual modular synthesizer.

long_description    {*}${description}

maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

# Build process requires git checkout
fetch.type          git

patchfiles          patch-makefile.diff
patchfiles-append   fetch.patch

depends_fetch-append \
                    port:wget

depends_build-append \
                    path:bin/cmake:cmake \
                    port:jq

use_configure       no

compiler.thread_local_storage yes

# fatal error: 'stdatomic.h' file not found
compiler.blacklist-append {clang < 700}

build.env-append    CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CPP=${configure.cpp}

set rack_fundamental_checksum   \
    1c6ec2aca01ac912181d194ff12f4137ca6e5ddd8616ccee3497b7b4347f59ff

set rack_fundamental_url \
    "https://github.com/VCVRack/Fundamental/archive/v${rack_fundamental_version}.zip"

post-fetch {
    # Fetch Rack's Fundamental plugins
    system -W ${workpath} "wget ${rack_fundamental_url} -O ./Fundamental.zip"
}

post-checksum {
    # Checksum plugins
    system -W ${workpath} \
        "echo '${rack_fundamental_checksum}  ./Fundamental.zip' | shasum -a 256 -c"
}

post-extract {
    # Fetch dependencies
    system -W ${worksrcpath} \
        "git submodule update --init --recursive"

    # Extract plugins and move them into place for the build process
    system -W ${workpath} "unzip -q -x Fundamental.zip"
    move ${workpath}/Fundamental-${rack_fundamental_version} ${workpath}/Fundamental
    move ${workpath}/Fundamental ${worksrcpath}/plugins/
}

build {
    set build_command "${build.env} ${build.cmd} -j ${build.jobs}"

    # Build dependencies
    system -W ${worksrcpath} "${build_command} dep"

    # Build plugins
    system -W ${worksrcpath} "${build_command} plugins"

    # Collect plugins distributables
    xinstall -d ${workpath}/plugins
    xinstall -d ${workpath}/plugins/Fundamental
    copy {*}[glob   ${worksrcpath}/plugins/Fundamental/LICENSE* \
                    ${worksrcpath}/plugins/Fundamental/plugin* ] \
                    ${worksrcpath}/plugins/Fundamental/res \
                    ${worksrcpath}/plugins/Fundamental/src \
        ${workpath}/plugins/Fundamental

    # Create plugins zip file
    system -W ${workpath}/plugins/ "zip -9 -r ${worksrcpath}/Fundamental.zip ./"

    # Create VCVRack distributable
    system -W ${worksrcpath} "${build_command} dist"
}

destroot {
    copy ${worksrcpath}/dist/Rack.app ${destroot}${applications_dir}/
}
