# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0

PortGroup           github 1.0

name                audacity-legacy
categories          audio
maintainers         nomaintainer
license             GPL-2+
universal_variant   no

set aud_app_path    ${applications_dir}/Audacity.app

variant wxsystem description {Depend on wxWidgets from MacPorts} {}

# PortGroups for the main ports
PortGroup           active_variants 1.1
PortGroup           cmake 1.1

description         A Free, Cross-Platform Digital Audio Editor.
long_description    Audacity is a free, easy-to-use, multi-track audio editor and recorder \
                    for Windows, Mac OS X, GNU/Linux and other operating systems. The interface is translated \
                    into many languages. You can use Audacity to: \n\
                    Record live audio. \n\
                    Record computer playback on any Windows Vista or later machine. \n\
                    Convert tapes and records into digital recordings or CDs. \n\
                    Edit WAV, AIFF, FLAC, MP2, MP3 or Ogg Vorbis sound files. \n\
                    AC3, M4A/M4R (AAC), WMA and other formats supported using optional libraries. \n\
                    Cut, copy, splice or mix sounds together. \n\
                    Numerous effects including change the speed or pitch of a recording. \n\
                    And more!

long_description-append \
                \n This version of the port provides the last release using the legacy file format. \
                The newer v3 versions can import the legacy .aup projects but can only \
                write in a new .aup3 format that the older versions cannot import! Use this port \
                if you have a need to exchange native projects with people using Audacity versions \
                older than 3.0.0 .

conflicts       ${name}
# get the source tarball from github because it contains all required external libs
# incl. those not in MacPorts.
github.setup    audacity audacity 2.4.2 Audacity-
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision        3
checksums       rmd160  d9e04c029a144800e378a6b5464c74f3fdb2a7e2 \
                sha256  032273e2408ad51f97c6846ac8d8fe9e4a3fbc6e4c620e630ea4c1d0124d9a94 \
                size    61254313
set PPREFIX     ./
variant wx30 conflicts wx32 requires wxsystem description {build against wxWidgets-3.0} {}

variant wx32 conflicts wx30 requires wxsystem description {build against wxWidgets-3.2} {}
if {![variant_exists wxsystem] || [variant_isset wxsystem]} {
    PortGroup       wxWidgets 1.0

} else {
    depends_fetch-append \
                    bin:wget:wget
}

supported_archs     x86_64 arm64
compiler.cxx_standard 2014
build.post_args-append -k
cmake.generator     Ninja
if {${os.major} >= 17} {
    # Audacity builds "normally" from 10.7 to 10.13, but will request the 10.13 SDK
    # on 10.14 and newer to "prevent high CPU usage and slow drawing". Linking will
    # fail if MacPort lets source be compiled against the platform SDK, so we handle
    # SDK selection ourselves entirely. MP builds aren't required to support anything
    # but the OS version they were built for. On 11.x we just build as if there are
    # no OS specifics.
    if {${os.major} < 20} {
        macosx_deployment_target 10.13
        configure.sdk_version 10.13
    }
}

proc add_patchfile_ifexists {args} {
    global PPREFIX filespath
    foreach fname ${args} {
        if {[file exists ${filespath}/${fname}]} {
            patchfiles-append ${fname}
        }
    }
}

# openssl is a dependency of curl in cmake and of python3.7
license_noconflict  cmake python37

homepage            http://www.audacityteam.org/

if {[variant_isset wxsystem]} {
    if {[variant_exists wx32] && [variant_isset wx32]} {
        wxWidgets.use   wxWidgets-3.2
    } elseif {[variant_exists wx30] && [variant_isset wx30]} {
        wxWidgets.use   wxWidgets-3.0
    } else {
        # we want to use the temporary wxWidgets-3.1 "Audacity-special" port!
        wxWidgets.use   wxWidgets-3.1
    }

    require_active_variants ${wxWidgets.port} "" stdlib

    depends_lib-append  port:${wxWidgets.port}
} else {
    set wxCommit    07e7d83
    # different master_sites so we need to fetch and extract the audacity-wxWidgets sources "manually"
    post-fetch {
        if {![file exists ${distpath}/wxWidgets-${wxCommit}.tar.gz]} {
            ui_info "--->  Fetching wxWidgets-${wxCommit}.tar.gz"
            system "wget --no-check-certificate \"https://github.com/audacity/wxWidgets/tarball/${wxCommit}/wxWidgets-${wxCommit}.tar.gz\" \
                --progress=bar:force -O ${distpath}/wxWidgets-${wxCommit}.tar.gz"
        }
    }
    post-extract {
        ui_info "--->  Extracting wxWidgets-${wxCommit}.tar.gz"
        system -W ${worksrcpath}/lib-src "tar -xf ${distpath}/wxWidgets-${wxCommit}.tar.gz"
        ln -s audacity-wxWidgets-${wxCommit} ${worksrcpath}/lib-src/wxWidgets
    }
    # add the wxWidgets dependencies
    depends_lib-append \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:tiff \
                    port:libpng \
                    port:zlib \
                    port:libiconv
}

depends_build-append \
                    port:pkgconfig \
                    port:python37
set python_bin      ${prefix}/bin/python3.7

depends_lib-append  port:freetype \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:soundtouch \
                    port:soxr \
                    port:twolame \
                    port:expat \
                    path:lib/libavcodec.dylib:ffmpeg \
                    port:flac \
                    port:lame \
                    port:libogg \
                    port:libsndfile \
                    port:libvorbis
if {${os.arch} ne "arm"} {
    depends_lib-append \
                    port:libid3tag \
                    port:libmad
}
# audacity could use port:lv2 and port:lilv but would also require a port:suil for that to be possible.

patch.pre_args-append   -N
patch.pre_args-replace  -p0 -p1

# address issues in the build system:
# - build and link VSTControlOSX.mm instead of the GTk interface
# - address a visibility issue with the slider_array variable in libnyquist
# - make the AudioUnits plugin support build; on 64bit and also include AUControl.mm
platform darwin {
    depends_build-append \
                    port:nasm

    patchfiles-append \
                    portaudio-no-universal-build.diff                       \
                    buildinfo-clarify-no-gstreamer.diff           \
                    patch-aboutdlg-compiler.diff                  \
                    add_enGB_translation.diff                     \
                    patch-more-decent-font-sizes.diff             \
                    patch-python.diff                             \
                    patch-libnyquist-symbol-visibility.diff
    # use lame from MacPorts (partial revert of ae431bc5)
    # load ffmpeg libs from MacPorts (reverts of 1d40f33e)
    patchfiles-append \
                    patch-use-MPlame.diff \
                    patch-use-MPffmpeg.diff
    # work around a missing implementation in wxCocoa's wxSound class
    patchfiles-append \
                    patch-implement-progdial=beep.diff \
                    patch-wxw-303.diff
    if {[variant_isset wxsystem]} {
        # make certain we're not embedding any wxWidgets libraries
        add_patchfile_ifexists \
                    ${PPREFIX}patch-no-embedded-wxW.diff
    } else {
        # make certain we're embedding our wxWidgets libraries
        add_patchfile_ifexists \
                    ${PPREFIX}patch-embed-wxW.diff
    }
}
add_patchfile_ifexists \
                    ${PPREFIX}patch-skip-gcc-version-test.diff \
                    ${PPREFIX}patch-enable-compact-menu.diff \
                    ${PPREFIX}patch-temp-fixes.diff
if {[variant_isset wxsystem] && ${wxWidgets.use} ne "wxWidgets-3.2"} {
    add_patchfile_ifexists \
                    ${PPREFIX}patch-wxw-31x.diff
}

platform darwin {
    variant suil description {Build the GUI library for LV2 plugins\; requires Qt5} {
        PortGroup   qt5 1.0

    }
}

# among other things, we add frameworks AudioToolbox, Cocoa and Carbon because somewhere after wxW 3.1.3
# the wx build system no longer exports private libraries. Carbon could be temporary because wxW
# dropped Carbon usage somewhere around 3.1.4 but drops support for 10.9- earlier. This is not
# required when building against port:wxWidgets-3.1 but it won't hurt either.
patchfiles-append   ${PPREFIX}patch-cmakefiles.diff


# quelch a huge number of warnings
configure.cxxflags-append \
                    -Wno-inconsistent-missing-override \
                    -Wno-overloaded-virtual

set _OPT "audacity_"
configure.args-append \
                    -D${_OPT}lib_preference=system \
                    -D${_OPT}use_audio_units=on \
                    -D${_OPT}use_pa_jack=off \
                    -D${_OPT}use_ffmpeg=linked \
                    -D${_OPT}use_lame=system \
                    -D${_OPT}use_flac=system \
                    -D${_OPT}use_soxr=system \
                    -D${_OPT}use_vamp=local \
                    -D${_OPT}use_ogg=system \
                    -D${_OPT}use_vorbis=system \
                    -D${_OPT}use_lv2=local \
                    -D${_OPT}use_suil_qt5=OFF \
                    -D${_OPT}use_sbsms=local \
                    -D${_OPT}use_soundtouch=system \
                    -D${_OPT}use_twolame=system \
                    -D${_OPT}use_midi=local \
                    -D${_OPT}use_pch=on
if {${os.arch} ne "arm"} {
    configure.args-append \
                    -D${_OPT}use_mad=system \
                    -D${_OPT}use_id3tag=system
} else {
    configure.args-append \
                    -D${_OPT}use_mad=off \
                    -D${_OPT}use_id3tag=off
}
depends_lib-append  port:sqlite3
if {[variant_isset wxsystem]} {
    configure.args-append \
                    -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
                    -D${_OPT}use_wxwidgets=system
    configure.env-append \
                    WX_CONFIG=${wxWidgets.wxconfig}
} else {
    configure.args-append \
                    -D${_OPT}use_wxwidgets=local
    # use the MacPorts options for building wxWidgets, in particular
    # about using dependencies from MacPorts:
    configure.args-append \
                    -DwxUSE_LIBICONV=sys \
                    -DwxUSE_LIBJPEG=sys \
                    -DwxUSE_LIBTIFF=sys \
                    -DwxUSE_LIBPNG=sys \
                    -DwxUSE_ZLIB=sys \
                    -DwxUSE_OPENGL=ON \
                    -DwxUSE_LIBSDL=OFF \
                    -DwxUSE_AUI=ON \
                    -DwxUSE_DISPLAY=ON \
                    -DwxUSE_XRC=ON \
                    -DwxUSE_GRAPHICS_CONTEXT=ON \
                    -DwxBUILD_PRECOMP=ON \
                    -DwxBUILD_STRIPPED_RELEASE_DEFAULT=OFF
    configure.env-append \
                    WXWIN=${worksrcpath}/lib-src/wxWidgets
    build.env-append \
                    WXWIN=${worksrcpath}/lib-src/wxWidgets
}
# ensure pre-compiled headers and the possible use of ccache can co-exist
configure.env-append \
                    "CCACHE_SLOPPINESS=pch_defines,time_macros"
build.env-append    "CCACHE_SLOPPINESS=pch_defines,time_macros"

if {[variant_isset suil]} {
    configure.args-replace \
                    -D${_OPT}use_suil_qt5=OFF \
                    -D${_OPT}use_suil_qt5=ON
}
platform darwin {
    # Since version 2.2.0 we need to use the embedded (modified) Portaudio version.
    configure.args-append \
                    -Daudacity_use_portaudio=local
}

# make sure the C++ stdlib argument is added to the LDFLAGS too
if {${configure.cxx_stdlib} ne "" && [string match *clang* ${configure.cxx}]} {
    configure.ldflags-append \
                    -stdlib=${configure.cxx_stdlib}
}

pre-configure {
    configure.args-append \
                    "CPPFLAGS=\"${configure.cppflags}\""
}

if {${build.cmd} ne "ninja"} {
    build.args-append   V=1 VERBOSE=1
} else {
    build.pre_args-prepend -k 0
}

# with everything defined/declared as intended we can now
# handle the post-patch step with its reinplace statements.
post-patch {
    reinplace -W ${worksrcpath} "s|@@PYTHON@@|${python_bin}|g" \
                                lib-src/lv2/configure
    reinplace -W ${worksrcpath} "s|/usr/local|${prefix}|g" \
                                src/effects/ladspa/LadspaEffect.cpp \
                                src/effects/VST/VSTEffect.cpp \
                                src/export/ExportMP3.cpp \
                                lib-src/lv2/lilv/wscript \
                                lib-src/lv2/lilv/test/lilv_test.c
    reinplace -W ${worksrcpath} "s|/Library/Audio/Plug-Ins/Vamp|/Library/Audio/Plug-Ins/Vamp:${prefix}/lib/vamp|g" \
                                lib-src/libvamp/src/vamp-hostsdk/PluginHostAdapter.cpp
    reinplace -W ${worksrcpath} "s|/Library/Application Support/audacity/libs|${prefix}/lib|g" \
                                src/FFmpeg.h
    reinplace -W ${worksrcpath} "s| -Werror||g" \
                                lib-src/portaudio-v19/configure.in \
                                lib-src/portaudio-v19/configure
    reinplace -W ${worksrcpath} "s|@PREFIX@|/opt/local|g" \
                                cmake-proxies/lv2/CMakeLists.txt
    platform darwin {
        # audacity contains C++ files that include system SDK headers and ObjC code on OS X;
        # they must thus be built as ObjC++
        set make_objcpp { \
            {CommandManager src/commands    {src/Makefile.in src/Makefile.am}} \
            {Effect         src/effects     {src/Makefile.in src/Makefile.am}} \
            {LV2Effect      src/effects/lv2 {src/Makefile.in src/Makefile.am}} \
            {KeyboardCapture src            {src/Makefile.in src/Makefile.am}} \
        }
        foreach mm ${make_objcpp} {
            set f  [lindex ${mm} 0]
            set d  [lindex ${mm} 1]
            set mk [lindex ${mm} 2]
            # it's easiest to symlink f.mm to f.cpp; clang++ will do the rest.
            if {![file exists ${worksrcpath}/${d}/${f}.mm]} {
                ln -s ${f}.cpp ${worksrcpath}/${d}/${f}.mm
            }
        }
        if {[file exists ${worksrcpath}/src/menus/WindowMenus.cpp] && ![file exists ${worksrcpath}/src/menus/WindowMenus.mm]} {
            ln -s WindowMenus.cpp ${worksrcpath}/src/menus/WindowMenus.mm
        }
    }
}

pre-build {
    # register the exact source version in the build:
    if {[file exists ${worksrcpath}/mac/scripts/get_gitident.sh]} {
        system -W ${worksrcpath}/mac "scripts/get_gitident.sh"
        system "echo '#define GIT_DESC \"${version}\"' >> ${worksrcpath}/src/RevisionIdent.h"
    } else {
        system "echo '#define GIT_DESC \"${version}\"' > ${worksrcpath}/src/GitDesc.h"
    }
}

platform darwin {

    post-destroot {
        file rename ${destroot}${prefix}/Audacity.app ${destroot}${aud_app_path}
        file delete -force ${destroot}${prefix}/share/audacity/plug-ins
        file delete -force ${destroot}${prefix}/share/audacity/nyquist
        file delete -force ${destroot}${prefix}/share/audacity/modules
        ln -s ${aud_app_path}/Contents/plug-ins ${destroot}${prefix}/share/audacity/
        file delete -force ${destroot}/Resources
        if {[variant_isset suil]} {
            xinstall -m 755 -d ${destroot}${aud_app_path}/Contents/Frameworks
            file rename ${build.dir}/bin/MacPorts/lib/audacity/suil_qt5_in_cocoa.so \
                ${destroot}${aud_app_path}/Contents/Frameworks/suil_qt5_in_cocoa.dylib
            ln -s suil_qt5_in_cocoa.dylib ${destroot}${aud_app_path}/Contents/Frameworks/suil_qt5_in_cocoa.so
        }
        if {[variant_isset wxsystem]} {
            foreach l [glob -nocomplain ${wxWidgets.prefix}/share/locale/*] {
                set lang [file tail ${l}]
                xinstall -m 755 -d ${destroot}${aud_app_path}/Contents/Resources/${lang}.lproj
                ln -s ${l}/LC_MESSAGES/wxstd.mo ${destroot}${aud_app_path}/Contents/Resources/${lang}.lproj/
            }
        }
        # allow the application to find certain things where it expects them on OS X (= inside the app bundle)
        foreach r [glob -nocomplain ${destroot}${prefix}/share/audacity/*] {
            set f [file tail ${r}]
            if {![file exists ${destroot}${aud_app_path}/Contents/Resources/${f}]} {
                ln -s ${prefix}/share/audacity/${f} ${destroot}${aud_app_path}/Contents/Resources
            }
        }
        # install a wrapper script in ${prefix}/bin
        system "echo \"#!/bin/sh\nexec \\\"${aud_app_path}/Contents/MacOS/Wrapper\\\" \\\"\\\$\@\\\"\" > ${destroot}${prefix}/bin/audacity"
        system "chmod 755 ${destroot}${prefix}/bin/audacity"
    }
}

post-destroot {
    xinstall -m 755 ${filespath}/vocalremover.ny ${destroot}${aud_app_path}/Contents/plug-ins/
}

notes "
    In case Audacity complains about finding FFmpeg or Lame, please locate these libraries\
    under ${prefix}/lib in the Libraries section of the Preferences dialog (see\
    http://manual.audacityteam.org/man/installing_and_updating_audacity_on_mac_os_x.html#locate)
"

# kate: backspace-indents true; indent-pasted-text true; indent-width 4; keep-extra-spaces true; remove-trailing-spaces modified; replace-tabs true; replace-tabs-save true; syntax Tcl/Tk; tab-indents true; tab-width 4;
