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

pkgname=("util-linux" "libutil-linux" "libutil-linux-devel")
_pkgmajor=2.35
pkgver=${_pkgmajor}.2
pkgrel=1
pkgdesc="Miscellaneous system utilities for Linux"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.kernel.org/pub/linux/utils/util-linux/"
makedepends=('gettext-devel' 'libiconv-devel' 'ncurses-devel' 'libcrypt-devel' 'libtool' 'patch' 'zlib-devel')
source=(https://www.kernel.org/pub/linux/utils/$pkgname/v${_pkgmajor}/${pkgname}-${pkgver}.tar.xz
        2.24.2-agetty.patch
        2.24.2-libblkid-topology.patch
        2.24.2-libintl.patch
        2.25.1-cygwin-include.patch
        2.33.1-cygwin-cpuset.patch
        2.33.1-cygwin-ionice.patch
        2.33.1-cygwin-taskset.patch
        2.32.1-testsuite.patch
        2.25.1-relocate-sbin-to-usr-bin.patch
        2.24.2-msysize.patch)
sha256sums=('21b7431e82f6bcd9441a01beeec3d57ed33ee948f8a5b41da577073c372eb58a'
            '0d6c812fab3deb34842dfb4313ce626e389c0961fe863a653ee1799a16614200'
            '347ff26c7750ef586dd50c3ac0be0b86604f8c4a4f148a0fc4e49d9ec55e2198'
            'a678a08fa18fc2ad34bc2a39bb0ff983f76803d4df742daf9d5e7b0d1d3b76f6'
            '82545b7762445c5fbd43481f12bc8416f26783a70b3ce6a6ff3f42b2b9a1afc6'
            '8826fba4ac3a102f89301fd146a61a8f98d6ae9faae8bdb186bbad28363370c2'
            'e8ee1a1577899d00c6e198cf81cd11af445eb6c906a602559a751c0f4878f248'
            'e355a7c05ad4abc5e6076e047e470b12104f1da1404313258470d404c7c26b38'
            'ea33be334f966df7b08b9d4d7b1f10df1c3f3cd08eade156d1f6969f913658d9'
            'd6bfc7e34349a98c8d110f1e216bd16d6f1edf45c369cda8c6301fb994986d13'
            '11563826de30d5e7e372e2901f3fd9d5b86539ae0c38c3d709c16b894b3034e5')

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

  patch -p2 -i ${srcdir}/2.24.2-agetty.patch
  patch -p2 -i ${srcdir}/2.24.2-libblkid-topology.patch
  patch -p2 -i ${srcdir}/2.24.2-libintl.patch
  patch -p2 -i ${srcdir}/2.25.1-cygwin-include.patch
  patch -p2 -i ${srcdir}/2.33.1-cygwin-cpuset.patch
  patch -p2 -i ${srcdir}/2.33.1-cygwin-ionice.patch
  patch -p2 -i ${srcdir}/2.33.1-cygwin-taskset.patch
  patch -p2 -i ${srcdir}/2.32.1-testsuite.patch
  patch -p1 -i ${srcdir}/2.24.2-msysize.patch
  patch -p1 -i ${srcdir}/2.25.1-relocate-sbin-to-usr-bin.patch
  autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${CHOST} ]] && rm -rf ${srcdir}/build-${CHOST}
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  CPPFLAGS+=" -D__USE_LINUX_IOCTL_DEFS"

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib \
    --libdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --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-fallocate \
    --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 \
    --enable-schedutils \
    --disable-wall \
    --disable-write \
    --disable-use-tty-group \
    --disable-makeinstall-chown \
    --disable-makeinstall-setuid \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    SOLIB_LDFLAGS=-no-undefined

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

check() {
  cd $srcdir/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")
  provides=('getopt')
  conflicts=('getopt')
  replaces=('getopt')

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/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 ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
}
