#!/bin/sh

me="$0"
scriptdir=${0%/*}

PACKAGENAME=coreutils
VERSION=-9.7
VERSIONPATCH=

. ${scriptdir}/functions.sh

PATCHES="
patches/coreutils/coreutils-i18n.patch
patches/coreutils/coreutils-sysinfo.patch
patches/coreutils/coreutils-getaddrinfo.patch
patches/coreutils/coreutils-misc.patch
patches/coreutils/coreutils-skip-some-sort-tests-on-ppc.patch
patches/coreutils/coreutils-skip-gnulib-test-tls.patch
patches/coreutils/coreutils-tests-shorten-extreme-factor-tests.patch
patches/coreutils/coreutils-disable_tests.patch
patches/coreutils/coreutils-test_without_valgrind.patch
patches/coreutils/coreutils-mint-uptime.patch
patches/coreutils/coreutils-mint-physmem.patch
patches/coreutils/coreutils-mint-mountlist.patch
patches/coreutils/coreutils-mint-procfile.patch
patches/coreutils/coreutils-mint-thread.patch
patches/coreutils/coreutils-9.7-sort-CVE-2025-5278.patch
"
EXTRA_DIST="
patches/automake/mintelf-config.sub
"

# patches/coreutils/coreutils-dummy-man-patch
# patches/coreutils/coreutils-remove_hostname_documentation.patch
# patches/coreutils/coreutils-remove_kill_documentation.patch
# patches/coreutils/coreutils-build-timeout-as-pie.patch

BINFILES="
${TARGET_BINDIR#/}/*
bin/*
${TARGET_PREFIX#/}/share/info/*
"

unpack_archive

cd "$srcdir"

rm -f aclocal.m4

aclocal -I m4 || exit 1
autoconf || exit 1
autoheader || exit 1
automake --force --copy --add-missing || exit 1
rm -rf autom4te.cache config.h.in.orig

# autoreconf may have overwritten config.sub
cp "$BUILD_DIR/patches/automake/mintelf-config.sub" build-aux/config.sub

cd "$MINT_BUILD_DIR"

COMMON_CFLAGS="-O2 -fomit-frame-pointer ${ELF_CFLAGS}"

CONFIGURE_FLAGS="--host=${TARGET} \
	--prefix=${prefix}
	--sysconfdir=/etc
	--disable-nls
	--enable-install-program=arch,kill
	--localstatedir=/var/run
	DEFAULT_POSIX2_VERSION=200112
	alternative=199209
	--config-cache
"
STACKSIZE="-Wl,-stack,256k"
if echo '#include <time.h>' | ${TARGET}-gcc -D_TIME_BITS=64 -E - | grep -q 'typedef __time64_t'; then
  :
else
  CONFIGURE_FLAGS+=" --disable-year2038"
fi

export PKG_CONFIG_LIBDIR="$prefix/$TARGET/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_LIBDIR"

create_config_cache()
{
cat <<EOF >config.cache
ac_cv_header_pthread_h=no
gl_have_pthread_h=no
EOF
	append_gnulib_cache
}

for CPU in ${ALL_CPUS}; do
	cd "$MINT_BUILD_DIR"

	eval CPU_CFLAGS=\${CPU_CFLAGS_$CPU}
	eval multilibdir=\${CPU_LIBDIR_$CPU}
	eval multilibexecdir=\${CPU_LIBEXECDIR_$CPU}
	create_config_cache
	CFLAGS="$CPU_CFLAGS $COMMON_CFLAGS" \
	LDFLAGS="$CPU_CFLAGS $COMMON_CFLAGS ${STACKSIZE}" \
	"$srcdir/configure" ${CONFIGURE_FLAGS} \
	--libdir='${exec_prefix}/lib'$multilibdir \
	--libexecdir='${exec_prefix}/libexec/find'$multilibexecdir

	: hack_lto_cflags

	${MAKE} ${JOBS} || exit 1

	${MAKE} DESTDIR="${THISPKG_DIR}${sysroot}" install
	
	${MAKE} clean >/dev/null

	cd ${THISPKG_DIR}${sysroot}
	rm -f ${TARGET_LIBDIR#/}$multilibdir/charset.alias	

	mkdir -p ${THISPKG_DIR}${sysroot}/bin
	cd ${THISPKG_DIR}${sysroot}/bin
	for i in arch kill basename cat chgrp chmod chown cp date dd df echo \
	  false ln ls mkdir mknod mktemp mv pwd rm rmdir sleep sort stat \
	  stty sync touch true uname readlink md5sum
	do
	  rm -f $i
	  $LN_S ../usr/bin/$i $i
	done

	# don't supply chroot under MiNT
	cd ${THISPKG_DIR}${sysroot}${prefix}/bin
	rm -f chroot

	# rename hostname so it don't conflict with the hostname rpm
	test ! -x hostname || mv hostname hostname.gnu
	
	# don't ship su; it's in shadow-utils
	rm -f su

	# remove man-pages; they are generated by running the tools
	rm -rf ${THISPKG_DIR}${sysroot}${prefix}/share/man

	make_bin_archive $CPU
done

move_prefix
configured_prefix="${prefix}"

make_archives
