# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=n2n
pkgver=2.8
pkgrel=4
pkgdesc="A Peer-to-peer VPN software which makes it easy to create virtual networks bypassing intermediate firewalls"
arch=('x86_64')
url="https://github.com/ntop/n2n"
license=('GPL')
depends=('libcap' 'openssl')
optdepends=('libpcap: for n2n-decode')
makedepends=('cmake' 'ninja' 'libpcap')
backup=('etc/n2n/edge.conf'
        'etc/n2n/supernode.conf')
source=("https://github.com/ntop/n2n/archive/$pkgver/n2n-$pkgver.tar.gz"
        n2n-manpage-location.patch::https://github.com/ntop/n2n/pull/411.patch)
sha512sums=('24eaabe1b454fbcf47c251d93fad31bf10241fe7d79b3df8cfd758652fe60f18e4f123054a4319c6b58ae96d130faa93f3d56b1bfec6c91d9c834c8bd5ae71d4'
            'f62345d50cebef19dfdec3f116fc4b43b713b7c9f8a38658b7c2beeb4e818ffd1e5cbf05a701ff9645af14035368130ec8ccb95c880cdccf42efd556b57c4155')

prepare() {
  cd $pkgname-$pkgver
  patch -p1 -i ../n2n-manpage-location.patch
  sed -i 's/RUNTIME DESTINATION sbin/RUNTIME DESTINATION bin/' CMakeLists.txt
  sed -i 's|/usr/sbin|/usr/bin|' packages/etc/systemd/system/*.service.in

  # We need a better way to make the conf file owned by the user created in sysusers.d...
  sed -i '/User=n2n/d;/Group=n2n/d' packages/etc/systemd/system/supernode.service.in
}

build() {
  cd $pkgname-$pkgver

  cmake -GNinja -DCMAKE_INSTALL_PREFIX=/usr .
  ninja
}

package() {
  cd $pkgname-$pkgver
  DESTDIR="$pkgdir" ninja install

  install -Dm600 packages/etc/n2n/edge.conf.sample "$pkgdir"/etc/n2n/edge.conf
  install -Dm600 packages/etc/n2n/supernode.conf.sample "$pkgdir"/etc/n2n/supernode.conf
  install -Dm644 packages/etc/systemd/system/edge.service.in "$pkgdir"/usr/lib/systemd/system/edge.service
  install -Dm644 packages/etc/systemd/system/edge@.service.in "$pkgdir"/usr/lib/systemd/system/edge@.service
  install -Dm644 packages/etc/systemd/system/supernode.service.in "$pkgdir"/usr/lib/systemd/system/supernode.service
}
