# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

_reponame=repoindex
pkgname=(repoindex{,-header,-web,-gui})
pkgver=0.2.6
pkgrel=1
arch=('i686' 'x86_64')
pkgdesc='Repository browser for Arch Linux'
license=('GPL')
depends=('qt5-websockets' 'qt5-webengine' 'karchive' 'c++utilities' 'qtutilities')
makedepends=('cmake' 'ninja' 'yuicompressor' 'qtutilities')
backup=("etc/${_reponame}.conf")
url="https://github.com/Martchus/${_reponame}"
options=(emptydirs)
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/Martchus/${_reponame}/archive/v${pkgver}.tar.gz")
sha256sums=('SKIP')

build() {
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"
  cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DWEBVIEW_PROVIDER=webengine \
    .
  ninja
}

package_repoindex() {
  pkgdesc="Repository browser for Arch Linux (provides server binary)."
  depends=('qt5-base' 'qt5-websockets' 'karchive' 'c++utilities')
  optdepends=('repoindex-header: header files')
  optdepends=('repoindex-web: files for web UI')
  optdepends=('repoindex-gui: application showing the web UI')

  # install files
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"
  DESTDIR="${pkgdir}" ninja install-binary install-service install-config install-bash-completion
  rm "$pkgdir/usr/bin/repoindex-gui"

  # make directories for cache and data
  mkdir -p "$pkgdir/var/cache/$_reponame"
  chown http:http "$pkgdir/var/cache/$_reponame"
  mkdir -p "$pkgdir/var/lib/$_reponame/sync"
  chown http:http "$pkgdir/var/lib/$_reponame/"{,sync}
  mkdir -p "$pkgdir/etc"
  cp "$pkgdir/usr/share/$_reponame/skel/repoindex.conf.js" "$pkgdir/etc/repoindex.conf"
}

package_repoindex-header() {
  arch=('any')
  pkgdesc="Repository browser for Arch Linux (provides header files)"
  depends=('repoindex')
  backup=()

  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"

  # install files
  DESTDIR="${pkgdir}" ninja install-header
}

package_repoindex-web() {
  arch=('any')
  pkgdesc="Repository browser for Arch Linux (provides assets for web interface)"
  depends=('repoindex')
  backup=()

  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"

  # install files
  DESTDIR="${pkgdir}" ninja install-web
}

package_repoindex-gui() {
  arch=('any')
  pkgdesc="Repository browser for Arch Linux (provides an desktop app showing the web UI)"
  depends=('repoindex-web' 'qt5-webengine')
  backup=()

  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"

  # install files
  DESTDIR="${pkgdir}" ninja install-binary install-desktop
  rm -r "$pkgdir/usr/lib"
  rm "$pkgdir/usr/bin/repoindex"
}
