# Contributor: Luke R. <g4jc@openmailbox.org>
# Maintainer: David P. <megver83@parabola.nu>

pkgbase=samhain
pkgname=(samhain-client samhain-server)
pkgver=4.3.2
pkgrel=1
arch=(armv7h i686 x86_64)
url='http://www.la-samhna.de/'
license=(GPL)
makedepends=(openssl procps-ng)
source=("https://www.la-samhna.de/archive/${pkgbase}_signed-$pkgver.tar.gz"
        samhain.service
        yule.service)
options=(!emptydirs)
validpgpkeys=('EF6CEF54701A0AFDB86AF4C31AAD26C80F571F6C') # Rainer Wichmann
sha512sums=('aaa4c9e384715fffaa55d3d5838bf137f199bd7a8da2f2005e165ead03f82c401de74806c4a2687eaa217927d50c5400417805ac37dfd36c4b0ad959c3bf2d1e'
            '736b6077c680afd2cbb25065207e671dd63d329ce9c1b8d11aa02e835550bdcbf72be6cba8fe67079c3823c805d8fdaf17e5238371c679b5ff8c7dbee56bdc9d'
            'd005c19efd164ea86e06860aae0bac18432636c3b9763c4e5584e1a4c17610d5092984c5e946450beaa515d683b5b0e9364d28c55d28fcab28c928d7bcf71031'
            'a6b25f878f0adb1dbdb0c19feea7e325d400bbb2834604a675ce3e47204089dd92dbb51c23889883b28e028fa7f1d8c0e0c7559c27497dd9a56f365d6155e9a2'
            'SKIP')

# Hack to allow having samhain-$pkgver.tar.gz in source=(),
# since it is inside samhain_signed-$pkgver.tar.gz
[ -e ${pkgbase}_signed-$pkgver.tar.gz ] || curl -fLC - --retry 3 --retry-delay 3 --silent -O ${source[0]}
[[ -e samhain-$pkgver.tar.gz{,.asc} ]] || tar -zxf ${pkgbase}_signed-$pkgver.tar.gz &> /dev/null &&
source+=("samhain-$pkgver.tar.gz"{,.asc})

build() {
  cp -r "$pkgbase-$pkgver" "$pkgbase-server-$pkgver"

  cd "$pkgbase-$pkgver"
  ./configure --prefix=/usr \
              --localstatedir=/var \
              --sysconfdir=/etc \
              --with-trusted=0 \
              --sbindir=/usr/bin

  cd "../$pkgbase-server-$pkgver"
  ./configure --prefix=/usr \
              --localstatedir=/var \
              --sysconfdir=/etc \
              --sbindir=/usr/bin \
              --enable-network=server
}

package_samhain-client() {
  pkgdesc='File integrity/intrusion detection system'
  install=samhain.install
  provides=($pkgbase)
  replaces=(${provides[@]})
  conflicts=(${provides[@]})

  cd "$pkgbase-$pkgver"

  make DESTDIR="$pkgdir" install

  chmod 755 $pkgdir/usr/bin/samhain
  chmod 644 $pkgdir/etc/samhainrc
  install -m755 -d "$pkgdir/usr/lib/systemd/system"
  install -m644 "$srcdir/samhain.service" "$pkgdir/usr/lib/systemd/system/samhain.service"

  rmdir $pkgdir/run
  rmdir $pkgdir/var/log
}

package_samhain-server() {
  pkgdesc='Server of file integrity/intrusion detection system'

  cd "$pkgbase-server-$pkgver"

  make DESTDIR="$pkgdir/" install

  for f in yule{,admin.pl,_setpwd,ctl}; do
    chmod 755 $pkgdir/usr/bin/$f
  done
  chmod 644 $pkgdir/etc/yulerc
  install -m755 -d "$pkgdir/usr/lib/systemd/system"
  install -m644 "$srcdir/yule.service" "$pkgdir/usr/lib/systemd/system/yule.service"

  rmdir $pkgdir/run
  rm -r $pkgdir/var/log
}
