SUMMARY="A random collection of Linux utilities"
DESCRIPTION="Various useful Linux utilities."
HOMEPAGE="https://www.kernel.org/pub/linux/utils/util-linux/"
COPYRIGHT="Various Authors"
LICENSE="GNU GPL v2
	GNU LGPL v2
	BSD (4-clause)
	Public Domain"
REVISION="1"
SOURCE_URI="https://www.kernel.org/pub/linux/utils/util-linux/v$portVersion/util-linux-$portVersion.tar.xz"
CHECKSUM_SHA256="743f9d0c7252b6db246b659c1e1ce0bd45d8d4508b4dfa427bbb4a3e9b9f62b5"
SOURCE_DIR="util-linux-$portVersion"
PATCHES="util_linux-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

util_linuxLibs="libblkid libfdisk libsmartcols libuuid"
libblkidVersion="1.1.0"
libfdiskVersion="1.1.0"
libsmartcolsVersion="1.1.0"
libuuidVersion="1.3.0"
portVersionCompat="$portVersion compat >= 2"
for i in $util_linuxLibs; do
	eval "${i}VersionCompat=\"\$${i}Version compat >= \${${i}Version%%.*}\""
done

PROVIDES="
	util_linux$secondaryArchSuffix = $portVersionCompat
	cmd:blkid$commandSuffix = $portVersion
	cmd:col$commandSuffix = $portVersion
	cmd:colrm$commandSuffix = $portVersion
	cmd:column$commandSuffix = $portVersion
	cmd:hexdump$commandSuffix = $portVersion
	cmd:nologin$commandSuffix = $portVersion
	cmd:rename$commandSuffix = $portVersion
	cmd:ul$commandSuffix = $portVersion
	cmd:uuidgen$commandSuffix = $portVersion
	lib:libblkid$secondaryArchSuffix = $libblkidVersionCompat
	lib:libfdisk$secondaryArchSuffix = $libfdiskVersionCompat
	lib:libsmartcols$secondaryArchSuffix = $libsmartcolsVersionCompat
	lib:libuuid$secondaryArchSuffix = $libuuidVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

SUMMARY_cal="A random collection of Linux utilities (cal)"
PROVIDES_cal="
	util_linux${secondaryArchSuffix}_cal = $portVersionCompat
	cmd:cal$commandSuffix = $portVersion
	"
REQUIRES_cal="
	haiku$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix
	"
if [ -z "$commandSuffix" ]; then
CONFLICTS_cal="
	cmd:cal
	"
fi

SUMMARY_fdisk="A random collection of Linux utilities (fdisk)"
ARCHITECTURES_fdisk="!x86_gcc2 ?x86 ?x86_64"
SECONDARY_ARCHITECTURES_fdisk="?x86"
PROVIDES_fdisk="
	util_linux${secondaryArchSuffix}_fdisk = $portVersionCompat
	cmd:fdisk$commandSuffix = $portVersionCompat
	"
REQUIRES_fdisk="
	util_linux$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix
	"

PROVIDES_devel="
	util_linux${secondaryArchSuffix}_devel = $portVersionCompat
	devel:libblkid$secondaryArchSuffix = $libblkidVersionCompat
	devel:libfdisk$secondaryArchSuffix = $libfdiskVersionCompat
	devel:libsmartcols$secondaryArchSuffix = $libsmartcolsVersionCompat
	devel:libuuid$secondaryArchSuffix = $libuuidVersionCompat
	"
REQUIRES_devel="
	util_linux$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libintl$secondaryArchSuffix
	devel:libncursesw$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:autom4te
	cmd:automake
	cmd:bison
	cmd:gcc$secondaryArchSuffix
	cmd:gettext$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"

defineDebugInfoPackage util_linux$secondaryArchSuffix \
	"$commandBinDir"/blkid \
	"$commandBinDir"/col \
	"$commandBinDir"/colrm \
	"$commandBinDir"/column \
	"${commandBinDir/$portName/${portName}_cal}"/cal \
	"$commandBinDir"/hexdump \
	"$commandBinDir"/nologin \
	"$commandBinDir"/rename \
	"$commandBinDir"/ul \
	"$commandBinDir"/uuidgen \
	"$libDir"/libblkid.so.$libblkidVersion \
	"$libDir"/libfdisk.so.$libfdiskVersion \
	"$libDir"/libsmartcols.so.$libsmartcolsVersion \
	"$libDir"/libuuid.so.$libuuidVersion \

BUILD()
{
	CPPFLAGS="-D_BSD_SOURCE -DB_USE_POSITIVE_POSIX_ERRORS"
	LDFLAGS="-Wl,--as-needed -lnetwork -lbsd -lposix_error_mapper"

	CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" ./autogen.sh

	# Skip some tests in configure to avoid crashes
	config_opts="
		scanf_cv_alloc_modifier=no
		"
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		config_opts+="
			ax_cv_have_tls=no
			"
	fi

	CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" \
	runConfigure --omit-dirs "binDir sbinDir" ./configure $config_opts \
		--bindir="$commandBinDir" --sbindir="$commandBinDir" \
		--disable-all-programs \
		--disable-libmount \
		--disable-makedev \
		--enable-blkid \
		--enable-cal \
		--enable-col \
		--enable-colrm \
		--enable-column \
		--enable-fdisk \
		--enable-hexdump \
		--enable-libfdisk \
		--enable-libsmartcols \
		--enable-libuuid \
		--enable-nologin \
		--enable-rename \
		--enable-ul \
		--enable-uuidgen \
		--without-udev \

	# --disable-all-programs kills several --enable-* switches, so enable them:
	for i in \
		BLKID \
		COL COLRM COLUMN \
		FDISK \
		HEXDUMP \
		LIBBLKID \
		UUIDGEN \
		; do
		sed -i \
			-e "/BUILD_${i}_TRUE/ s/\"#\"/\"\"/;" \
			-e "/BUILD_${i}_FALSE/ s/\"\"/\"#\"/;" \
			config.status
	done
	make
}

INSTALL()
{
	make install

	# Cleanup
	rm -rf $prefix/sbin

	rm -f "$libDir"/lib*.la

	prepareInstalledDevelLibs $util_linuxLibs
	fixPkgconfig

	packageEntries cal \
		"$commandBinDir"/cal \
		"$dataDir"/bash-completion/completions/cal \
		"$manDir"/man1/cal.1

	# With the fdisk subpackage being marked as untested, haikuporter does not
	# create the subdirectory for it in $packagingBaseDir when ALLOW_UNTESTED=no
	# so we need to skip the call to "packageEntries fdisk ..." in that case.
	if [ -d "$packagingBaseDir"/util_linux${secondaryArchSuffix}_fdisk ]; then
		maybe_packageEntries_fdisk="packageEntries fdisk"
	else
		maybe_packageEntries_fdisk=rm
	fi
	$maybe_packageEntries_fdisk \
		"$commandBinDir"/fdisk \
		"$dataDir"/bash-completion/completions/fdisk \
		"$manDir"/man8/fdisk.8

	packageEntries devel \
		"$developDir" \
		"$manDir"/man3
}

TEST()
{
	# Set TS_OPTS="" otherwise "--nonroot" is passed to tests/run.sh which
	# then returns "Ignore util-linux test suite [non-root UID expected]."
	LIBRARY_PATH="$sourceDir/.libs${LIBRARY_PATH:+:$LIBRARY_PATH}" \
	make check TS_OPTS=
}
