# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=("util-linux" "libutil-linux" "libutil-linux-devel")
_pkgmajor=2.40
pkgver=${_pkgmajor}.2
pkgrel=2
pkgdesc="Miscellaneous system utilities for Linux"
arch=('i686' 'x86_64')
license=('spdx:GPL-2.0-or-later')
url="https://www.kernel.org/pub/linux/utils/util-linux/"
msys2_references=(
  "anitya: 8179"
  "cpe: cpe:/a:andries_brouwer:util-linux"
  "cpe: cpe:/a:kernel:util-linux"
  "cpe: cpe:/a:linux:util-linux"
  "cpe: cpe:/a:util-linux_project:util-linux"
)
makedepends=('autotools'
             'gcc'
             'gettext-devel'
             'gtk-doc'
             'libiconv-devel'
             'libreadline-devel'
             'libsqlite-devel'
             'libxcrypt-devel'
             'ncurses-devel'
             'zlib-devel')
source=(https://www.kernel.org/pub/linux/utils/$pkgname/v${_pkgmajor}/${pkgname}-${pkgver}.tar.xz
        2.24.2-libintl.patch
        2.25.1-relocate-sbin-to-usr-bin.patch
        2.33.1-cygwin-cpuset.patch
        2.39.3-cygwin-fallocate.patch
        2.39.3-cygwin-include.patch
        2.39.3-cygwin-more.patch
        2.39.3-testsuite.patch
        2.40.2-agetty.patch
        2.40.2-col.patch
        2.40.2-libblkid-topology.patch
        2.40.2-probe.patch
        2.40.2-setsid.patch
        2.40.2-uuid_time.patch
        msys2-2.40.2-taskset.patch
        msys2-2.40.2-fallocate.patch)
sha256sums=('d78b37a66f5922d70edf3bdfb01a6b33d34ed3c3cafd6628203b2a2b67c8e8b3'
            'a678a08fa18fc2ad34bc2a39bb0ff983f76803d4df742daf9d5e7b0d1d3b76f6'
            'd6bfc7e34349a98c8d110f1e216bd16d6f1edf45c369cda8c6301fb994986d13'
            '5536be94962e4b3cbc746811b6a7c40f11adc6b6d13ac91a5396c58af1dedf80'
            '7cf5f2cd65c7d517ea61c6d36a87bfec596df5412d2e59d982b6316343f500fb'
            '224505c3624f61f085130b4bc440c6237b19536bcdda57599ea4100f8b704d42'
            '60030e9c1f7a77eb317ac415d521b0a8c6da6b761f940664874f6eeeb7a0efd0'
            'd55723ab9f563f8c614b7d920c0feb9f2f47e49403247e6e282e449160141918'
            '2ea8cc78fd90a3149760d9bedc6152fd974bba039e7ad08c0b305fa3d2b35f8f'
            '5c0e64b9b7334bee98cfd76607a39ab61ee3f14c6e79184b60a9ecc56052fd8d'
            '165523f857d04ad5b60028d94a4d09466c5042714bb0b16fbcc93934c4739465'
            'f77fcbfba6c6ed4ad9a722df562bd9b2b2675e64a11bf0f323f0868cf3ba5738'
            '3a38dec3d3dbaf012b52511368c40bfd8108616fee74287603f3fd5ae9680402'
            'f6954765c98b956d1006e14e43ee9ce14b423e91397e251d978a6d2d5947cbd0'
            '1616f09d37ce37f2d102dad6494fa111a64b396cc9308736f99a7565dcb06028'
            '923c9bec7a508e6e091bda54bcc974e93dcb578b858ae0f9ae2a4ff53dbea93d')

prepare() {
  cd ${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/2.24.2-libintl.patch
  patch -p1 -i ${srcdir}/2.25.1-relocate-sbin-to-usr-bin.patch
  patch -p2 -i ${srcdir}/2.33.1-cygwin-cpuset.patch
  patch -p2 -i ${srcdir}/2.39.3-cygwin-fallocate.patch
  patch -p2 -i ${srcdir}/2.39.3-cygwin-include.patch
  patch -p2 -i ${srcdir}/2.39.3-cygwin-more.patch
  patch -p2 -i ${srcdir}/2.39.3-testsuite.patch
  patch -p2 -i ${srcdir}/2.40.2-agetty.patch
  patch -p2 -i ${srcdir}/2.40.2-col.patch
  patch -p2 -i ${srcdir}/2.40.2-libblkid-topology.patch
  patch -p2 -i ${srcdir}/2.40.2-probe.patch
  patch -p2 -i ${srcdir}/2.40.2-setsid.patch
  patch -p2 -i ${srcdir}/2.40.2-uuid_time.patch
  # TODO: find out where Cygwin's util-linux package is getting sys/syscall.h
  # and drop these patches in favor of that
  patch -p1 -i ${srcdir}/msys2-2.40.2-taskset.patch
  patch -p1 -i ${srcdir}/msys2-2.40.2-fallocate.patch
  autoreconf -fi
}

build() {
  mkdir -p build-${CHOST} && cd build-${CHOST}

  CPPFLAGS+=" -D__USE_LINUX_IOCTL_DEFS"

  export MSYSTEM=CYGWIN
  local -a extra_config
  # 32-bit cygwin only has 32-bit time_t
  # https://github.com/msys2/MSYS2-packages/issues/4078
  #
  # fallocate only builds for Cygwin 3.5+, and i686 is stuck on 3.3
  if [[ "$CARCH" == "i686" ]]; then
    extra_config+=("--disable-year2038" "--disable-fallocate")
  fi
  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib \
    --libdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --runstatedir=/var/run \
    --enable-libuuid \
    --enable-libuuid-force-uuidd \
    --enable-libblkid \
    --enable-libfdisk \
    --disable-libmount \
    --disable-mount \
    --disable-losetup \
    --disable-fsck \
    --disable-partx \
    --enable-uuidd \
    --disable-mountpoint \
    --disable-unshare \
    --disable-nsenter \
    --disable-setpriv \
    --disable-eject \
    --enable-agetty \
    --enable-cramfs \
    --enable-bfs \
    --disable-fdformat \
    --disable-hwclock \
    --disable-wdctl \
    --disable-switch_root \
    --disable-pivot_root \
    --disable-ipcrm \
    --disable-ipcs \
    --disable-tunelp \
    --disable-kill \
    --disable-last \
    --disable-lslogins \
    --disable-utmpdump \
    --disable-line \
    --disable-mesg \
    --disable-raw \
    --enable-rename \
    --disable-vipw \
    --disable-newgrp \
    --disable-chfn-chsh \
    --disable-login \
    --enable-nologin \
    --disable-sulogin \
    --disable-su \
    --disable-runuser \
    --enable-ul \
    --enable-more \
    --enable-pg \
    --disable-setterm \
    --disable-wall \
    --disable-write \
    --disable-use-tty-group \
    --disable-makeinstall-chown \
    --disable-makeinstall-setuid \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    "${extra_config[@]}" \
    SOLIB_LDFLAGS=-no-undefined

  make
  make DESTDIR=${srcdir}/dest install
}

check() {
  cd build-${CHOST}

  # Remove || true once testcase doesn't cause failures.
  make check || true
}

package_util-linux() {
  pkgdesc="Collection of basic system utilities"
  depends=("coreutils" "libutil-linux" "libiconv" "libreadline" "libsqlite")
  provides=('getopt')
  conflicts=('getopt')
  replaces=('getopt')

  mkdir -p ${pkgdir}/usr/bin
  cp dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf dest/usr/share ${pkgdir}/usr/
}

package_libutil-linux() {
  pkgdesc="Block device ID and Universally Unique ID libraries"
  provides=("libblkid" "libuuid" "libsmartcols")
  depends=("gcc-libs" "libintl")

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libutil-linux-devel() {
  pkgdesc="Block device ID and Universally Unique ID headers and libraries."
  provides=("libblkid-devel" "libuuid-devel" "libsmartcols-devel")
  depends=("libutil-linux")
  options=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf dest/usr/lib ${pkgdir}/usr/
  cp -rf dest/usr/include ${pkgdir}/usr/
}
