# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>

pkgname=tribler
pkgver=7.3.0
pkgrel=1
pkgdesc='Privacy enhanced BitTorrent client with P2P content discovery'
url='https://www.tribler.org'
arch=('any')
license=('LGPL3')
depends=('python2-cryptography' 'python2-cherrypy' 'python2-lz4'
         'python2-pillow' 'python2-pyqt5' 'qt5-svg' 'phonon-qt5-vlc' 'python2-chardet'
         'python2-psutil' 'python2-meliae' 'python2-decorator' 'python2-netifaces'
         'python2-twisted' 'libsodium' 'libtorrent-rasterbar' 'python2-configobj'
         'python2-matplotlib' 'python2-service-identity' 'python2-pony'
         'python2-libnacl' 'python2-contextlib2' 'python2-zc.lockfile' 'python2-networkx'
         'python2-m2crypto' 'python2-apsw' 'python2-plyvel' 'python2-feedparser')
makedepends=('python2-setuptools' 'git')
source=("https://github.com/Tribler/tribler/releases/download/v$pkgver/Tribler-v$pkgver.tar.xz")
sha512sums=('3c9cb138f80e5ce8cd6273f9bc3c4f08ca8ec7b187dc9ef128aaed1343e859048e58361ee223da085056affd35ce0af2641408b683e74281d184def62a979c8a')

prepare() {
  cd $pkgname

  # Fix tribler path
  sed -i 's|/opt/tribler|/usr/share/tribler|g' systemd/anontunnel_helper@.service
  sed -i 's|/opt/tribler|/usr/share/tribler|g' systemd/tribler.service

  # Fix version info
  sed -e "s|version_id =.*|version_id = \"${pkgver%_*}-arch\"|g" \
      -e "s|build_date =.*|build_date = \"$(date)\"|g" \
        -i Tribler/Core/version.py
}

build () {
  cd $pkgname

  python2 setup.py build
}

package() {
  cd $pkgname

  # Install python modules
  python2 setup.py install --root="$pkgdir" --optimize=1

  # Install binary files/assets
  install -dm 755 "$pkgdir"/usr/{bin,share/tribler}
  cp -dr --no-preserve=ownership Tribler "$pkgdir"/usr/share/tribler
  cp -dr --no-preserve=ownership TriblerGUI "$pkgdir"/usr/share/tribler
  ln -s Tribler/Core/CacheDB/schema_sdb_v*.sql "$pkgdir"/usr/share/tribler/Tribler

  install -dm 755 "$pkgdir"/usr/share/{applications,pixmaps}
  install -Dm 644 Tribler/Main/Build/Ubuntu/tribler.desktop "$pkgdir"/usr/share/applications
  install -Dm 644 Tribler/Main/Build/Ubuntu/tribler.xpm "$pkgdir"/usr/share/pixmaps
  install -Dm 644 Tribler/Main/Build/Ubuntu/tribler_big.xpm "$pkgdir"/usr/share/pixmaps
  install -Dm 755 debian/bin/tribler "$pkgdir"/usr/bin
  install -Dm 644 logger.conf "$pkgdir"/usr/share/tribler/
  install -Dm 644 run_tribler.py "$pkgdir"/usr/share/tribler/
  install -Dm 644 check_os.py "$pkgdir"/usr/share/tribler/

  cp -dr --no-preserve=ownership twisted "$pkgdir"/usr/share/tribler

  # Remove test folders
  find "$pkgdir" -type d -name "test" -name "tests" -exec rm -rf {} \;

  # Install systemd files
  install -Dm 644 systemd/anontunnel_helper@.service "$pkgdir"/usr/lib/systemd/system/anontunnel_helper@.service
  install -Dm 644 systemd/tribler.service "$pkgdir"/usr/lib/systemd/system/tribler.service
}

