# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgbase=libblockdev
pkgname=(
  libblockdev
  libblockdev-{btrfs,crypto,dm,fs,loop,lvm,mdraid,mpath,nvdimm,nvme,part,swap}
  python-libblockdev
)
pkgver=%version%
pkgrel=%release%
pkgdesc="A library for manipulating block devices"
arch=('x86_64')
url="https://github.com/storaged-project/libblockdev"
license=(LGPL-2.1-or-later)
makedepends=(
  'autoconf-archive'
  'btrfs-progs'
  'cryptsetup'
  'device-mapper'
  'dosfstools'
  'e2fsprogs'
  'exfatprogs'
  'gcc-libs'
  'glib2'
  'glibc'
  'gobject-introspection'
  'gtk-doc'
  'kmod'
  'keyutils'
  'libbytesize'
  'libnvme'
  'lvm2'
  'mdadm'
  'ndctl'
  'nss'
  'parted'
  'python'
  'systemd-libs'
  'util-linux'
  'util-linux-libs'
  'volume_key'
)
source=(
  "$url/releases/download/$pkgver-1/$pkgname-$pkgver.tar.gz"
)
sha512sums=('44628d9e68a467f8a5d5e65e12d5d6ffb4d9b953c846acd9f6da257eab5ccd79309651d6b20092fa700fb4340f1f718ceef11141203da676d088824aa9d03481')
b2sums=('b3dbdc7226524d544193b609ced8a9fee725268ba2201b639c22103fcae6d047c94fb1b848e489525300242cb235d688934193894a06279a4dc0e0b33dff6e96')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc
  )

  cd $pkgname-$pkgver
  ./configure "${configure_options[@]}"
  # prevent libtool from overlinking everything
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  cd $pkgname-$pkgver
  make check
}

package_libblockdev() {
  depends=(
    glib2 libglib-2.0.so libgio-2.0.so libgobject-2.0.so
    glibc
    kmod libkmod.so
    systemd-libs libudev.so
  )
  optdepends=(
    'libblockdev-btrfs: for BTRFS support'
    'libblockdev-crypto: for crypto support'
    'libblockdev-dm: for device-mapper support'
    'libblockdev-fs: for filesystem support'
    'libblockdev-loop: for loop device support'
    'libblockdev-lvm: for LVM support'
    'libblockdev-mdraid: for MDRAID support'
    'libblockdev-mpath: for multipath support'
    'libblockdev-nvdimm: for NVDIMM support'
    'libblockdev-nvme: for NVME support'
    'libblockdev-part: for partitioning support'
    'libblockdev-swap: for swap support'
    'python-libblockdev: for Python support'
  )
  provides=(
    libbd_utils.so
    libblockdev.so
  )
  conflicts=(libblockdev-utils)
  replaces=(libblockdev-utils)

  make DESTDIR="$pkgdir" install -C $pkgname-$pkgver

  cd "$pkgdir"

  _pick $pkgbase-btrfs usr/include/blockdev/btrfs.h
  _pick $pkgbase-btrfs usr/lib/libbd_btrfs.*

  _pick $pkgbase-crypto usr/include/blockdev/crypto.h
  _pick $pkgbase-crypto usr/lib/libbd_crypto.*

  _pick $pkgbase-dm usr/include/blockdev/dm.h
  _pick $pkgbase-dm usr/lib/libbd_dm.*

  _pick $pkgbase-fs usr/bin/vfat-resize  # the libbd_fs.so library calls this executable
  _pick $pkgbase-fs usr/include/blockdev/fs.h
  _pick $pkgbase-fs usr/include/blockdev/fs/*.h
  _pick $pkgbase-fs usr/lib/libbd_fs.*

  _pick $pkgbase-loop usr/include/blockdev/loop.h
  _pick $pkgbase-loop usr/lib/libbd_loop.*

  _pick $pkgbase-lvm etc/libblockdev/3/conf.d/10-lvm-dbus.cfg
  _pick $pkgbase-lvm usr/bin/lvm-cache-stats
  _pick $pkgbase-lvm usr/include/blockdev/lvm.h
  _pick $pkgbase-lvm usr/lib/libbd_lvm.*
  _pick $pkgbase-lvm usr/lib/libbd_lvm-dbus.*

  _pick $pkgbase-mdraid usr/include/blockdev/mdraid.h
  _pick $pkgbase-mdraid usr/lib/libbd_mdraid.*

  _pick $pkgbase-mpath usr/include/blockdev/mpath.h
  _pick $pkgbase-mpath usr/lib/libbd_mpath.*

  _pick $pkgbase-nvdimm usr/include/blockdev/nvdimm.h
  _pick $pkgbase-nvdimm usr/lib/libbd_nvdimm.*

  _pick $pkgbase-nvme usr/include/blockdev/nvme.h
  _pick $pkgbase-nvme usr/lib/libbd_nvme.*

  _pick $pkgbase-part usr/include/blockdev/part.h
  _pick $pkgbase-part usr/lib/libbd_part.*

  _pick $pkgbase-swap usr/include/blockdev/swap.h
  _pick $pkgbase-swap usr/lib/libbd_swap.*

  _pick python-$pkgbase usr/lib/python*
}

package_libblockdev-btrfs() {
  pkgdesc+=" - BTRFS support"
  depends=(
    btrfs-progs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    libbytesize
  )
  provides=(libbd_btrfs.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-crypto() {
  pkgdesc+=" - crypto support"
  depends=(
    cryptsetup libcryptsetup.so
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    keyutils libkeyutils.so
    "libblockdev=$pkgver" libbd_utils.so
    nss
    util-linux-libs libblkid.so
    volume_key
  )
  provides=(libbd_crypto.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-dm() {
  pkgdesc+=" - device mapper support"
  depends=(
    device-mapper libdevmapper.so
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
  )
  provides=(libbd_dm.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-fs() {
  pkgdesc+=" - filesystem support"
  depends=(
    e2fsprogs libext2fs.so libe2p.so
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    libbytesize
    parted
    util-linux-libs libblkid.so libmount.so libuuid.so
  )
  optdepends=(
    'btrfs-progs: for BTRFS filesystem support'
    'dosfstools: for VFAT filesystem support'
    'exfatprogs: for exFAT filesystem support'
    'f2fs-tools: for F2FS filesystem support'
    'nilfs-utils: for nilfs filesystem support'
    'ntfs-3g: for NTFS filesystem support'
    'udftools: for UDF filesystem support'
    'xfsprogs: for XFS filesystem support'
  )
  provides=(libbd_fs.so)
  conflicts=(libblockdev-tools)
  replaces=(libblockdev-tools)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-loop() {
  pkgdesc+=" - loop device support"
  depends=(
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
  )
  provides=(libbd_loop.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-lvm() {
  pkgdesc+=" - LVM support"
  depends=(
    device-mapper libdevmapper.so
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so libblockdev.so
    libbytesize
  )
  provides=(
    libbd_lvm.so
    libbd_lvm-dbus.so
  )
  conflicts=(
    libblockdev-lvm-dbus
    libblockdev-tools
  )
  replaces=(
    libblockdev-lvm-dbus
    libblockdev-tools
  )

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-mdraid() {
  pkgdesc+=" - MDRAID support"
  depends=(
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    libbytesize
    mdadm  # the libbd_mdraid.so library calls the mdadm executable
  )
  provides=(libbd_mdraid.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-mpath() {
  pkgdesc+=" - multipath device support"
  depends=(
    device-mapper libdevmapper.so
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
  )
  provides=(libbd_mpath.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-nvdimm() {
  pkgdesc+=" - NVDIMM support"
  depends=(
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    ndctl
    util-linux-libs libuuid.so
  )
  provides=(libbd_nvdimm.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-nvme() {
  pkgdesc+=" - NVME support"
  depends=(
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    libnvme
  )
  provides=(libbd_nvme.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-part() {
  pkgdesc+=" - partitioning support"
  depends=(
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    util-linux-libs libfdisk.so
  )
  provides=(libbd_part.so)

  mv -v $pkgname/* "$pkgdir"
}

package_libblockdev-swap() {
  pkgdesc+=" - swap device support"
  depends=(
    gcc-libs
    glib2 libglib-2.0.so
    glibc
    "libblockdev=$pkgver" libbd_utils.so
    util-linux-libs libblkid.so
  )
  provides=(libbd_swap.so)

  mv -v $pkgname/* "$pkgdir"
}

package_python-libblockdev() {
  pkgdesc+=" - Python support"
  depends=(
    python
    gobject-introspection-runtime
    libbytesize
  )

  mv -v $pkgname/* "$pkgdir"
}
