# Maintainer: David Runge <dvzrv@archlinux.org>

_name=link
pkgname=abletonlink
pkgver=3.0.3
pkgrel=1
pkgdesc="Synchronizes musical beat, tempo, and phase across multiple applications"
arch=('x86_64')
url="https://github.com/ableton/link"
license=('GPL2')
depends=('asio')
# TODO: remove replaces on 2020-09-22
replaces=('link')
makedepends=('catch2' 'cmake' 'portaudio' 'qt5-tools' 'qt5-quickcontrols')
optdepends=('jack: for JACK examples'
            'portaudio: for portaudio based examples'
            'qt5-quickcontrols: for Qt examples')
source=("$pkgname-$pkgver.tar.gz::https://github.com/Ableton/${_name}/archive/Link-${pkgver}.tar.gz"
        "${pkgname}-3.0.2-cmake_system_paths.patch"
        "${pkgname}-3.0.2-use_system_libs.patch")
sha512sums=('5fe4a6ecd10572b0c5167faa727a09e64d5491499f6a2425646cb8b8466c0bfb67a9ffe13b756a6f6ece93eee6caec86cf3d676524cdc9927958408ae9cff7f9'
            '8ccd3cc531b64fc91d1b5ba8c1de2a0f6aa52399e1ad5b971cf68b503aa471accb71834e8157b9c4b7a86a346d882f5408e7b99ba890a0baaf2cef21545424e9'
            '4683948b7bdc42f85674ad7ebed4abe8123a95580c0892a9c326d05e4783c2e875254e7d9c98767b5c8cc11b85023d31533f4535ffcf9654736a72abe256e6ca')
b2sums=('8e66e97476fd3f14b82b9df52f90b43f35249c0fb70cf97715bd30541dcff180a265910fdce577e5cc5b98acff108a82ee666b64c8aad74067aff8daeb6f29d9'
        'df3176dd3ab4957d087fa2174aef355d621c09171968b1f9b2c70012c583555f01e1d74b8f1675465c20dba8866fe39d35df4993170bd8f2c1cceb91478ad80c'
        '5a517778eb4251479a2c1a0ded68670c0198d436e0eeee30f780bf7e5757f35a5bed48f3f839e782a88778b174226be517efc22627f8ce40435dd20705268708')

prepare() {
  mv -v "${_name}-Link-${pkgver}" "$pkgname-$pkgver"
  cd "$pkgname-$pkgver"
  # using system libraries when integrating
  patch -Np1 -i "../${pkgname}-3.0.2-use_system_libs.patch"
  # the cmake integration is used to build tests and examples, so duplicating
  # before patching
  cp -v AbletonLinkConfig.cmake{,.local}
  patch -Np1 -i "../${pkgname}-3.0.2-cmake_system_paths.patch"
  mv -v AbletonLinkConfig.cmake{,.system}
  mv -v AbletonLinkConfig.cmake{.local,}
}

build() {
  cd "$pkgname-$pkgver"
  # fixing the catch2 include for the test binaries
  export CXXFLAGS+=" -I/usr/include/catch2"
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
        -DLINK_BUILD_JACK=ON \
        -DLINK_BUILD_QT_EXAMPLES=ON \
        -B build \
        -S .
  make -C build
}

check() {
  cd "$pkgname-$pkgver/build"
  bin/LinkCoreTest
  bin/LinkDiscoveryTest
}

package() {
  local dirs=('discovery' 'discovery/test' 'discovery/v1' 'link' 'link/v1'
  'platforms' 'platforms/asio' 'platforms/darwin' 'platforms/linux'
  'platforms/posix' 'platforms/stl' 'platforms/windows' 'test' 'test/serial_io'
  'util' 'util/test')
  cd "$pkgname-$pkgver"
  install -vDm 644 include/ableton/*.{ipp,hpp} \
    -t "${pkgdir}/usr/include/ableton"
  for dir in "${dirs[@]}"; do
    install -vDm 644 include/ableton/"${dir}"/*.hpp \
      -t "${pkgdir}/usr/include/ableton/${dir}"
  done
  # install cmake integration
  install -vDm 644 AbletonLinkConfig.cmake.system \
    "${pkgdir}/usr/lib/cmake/${pkgname}/AbletonLinkConfig.cmake"
  install -vDm 644 cmake_include/AsioStandaloneConfig.cmake \
    -t "${pkgdir}/usr/lib/cmake/${pkgname}/cmake_include"
  # examples
  install -vDm 755 build/bin/* -t "${pkgdir}/usr/bin/"
  install -vDm 644 {{README,CONTRIBUTING}.md,*.pdf} \
    -t "${pkgdir}/usr/share/doc/${pkgname}/"
}
