export OLDARCH="$ARCH"
unset ARCH

abinfo "Unsetting all flags to fix build ..."
unset CPPFLAGS CXXFLAGS CFLAGS LDFLAGS

if ab_match_archgroup 32bit ; then
	abinfo "Enabling Y2038 mitigation for 32-bit targets ..."
	# NOTE: The build system do not use CPPFLAGS.
	export EXTRA_CFLAGS="-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64"
	export KBUILD_HOSTCFLAGS="-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64"
fi
if ab_match_archgroup mainline ; then
	abinfo "Building bpf ..."
	cd "$SRCDIR"/tools/bpf
	make bpftool
	abinfo "Installing bpf ..."
	make DESTDIR="$PKGDIR" prefix="/usr" bpftool_install
fi

abinfo "Building cpupower ..."
cd "$SRCDIR"/tools/power/cpupower
make VERSION="$PKGVER"
abinfo "Installing cpupower ..."
make \
    DESTDIR="$PKGDIR" \
    sbindir="/usr/bin" \
    libdir="/usr/lib" \
    mandir="/usr/share/man" \
    docdir="/usr/share/doc/cpupower" \
    install install-man

if ab_match_arch amd64; then
    abinfo "Building x86-energy-perf-policy ..."
    cd "$SRCDIR"/tools/power/x86/x86_energy_perf_policy
    make

    abinfo "Installing x86-energy-perf-policy ..."
    install -Dvm755 ./x86_energy_perf_policy \
        "$PKGDIR"/usr/bin/x86_energy_perf_policy
    install -Dvm644 ./x86_energy_perf_policy.8 \
        "$PKGDIR"/usr/share/man/man8/x86_energy_perf_policy.8
fi

abinfo "Building ihex2fw ..."
cd "$SRCDIR"/tools/firmware
make
abinfo "Installing ihex2fw ..."
install -Dvm755 ./ihex2fw "$PKGDIR"/usr/bin/ihex2fw

abinfo "Building kvm_stat ..."
cd "$SRCDIR"/tools/kvm/kvm_stat
make
abinfo "Installing kvm_stat ..."
make install INSTALL_ROOT="$PKGDIR"

for i in dslm freefall; do
    abinfo "Building $i ..."
    cd "${SRCDIR}"/tools/laptop/$i
    make

    abinfo "Installing $i ..."
    install -Dvm755 ./$i \
        "$PKGDIR"/usr/bin/$i
done

abinfo "Building usbip ..."
cd "$SRCDIR"/tools/usb/usbip
./autogen.sh
./configure \
    --prefix=/usr \
    --sbindir=/usr/bin
make
abinfo "Installing usbip ..."
make install \
    DESTDIR="$PKGDIR"

abinfo "Building tmon ..."
cd "$SRCDIR"/tools/thermal/tmon
make
abinfo "Installing tmon ..."
make install INSTALL_ROOT="$PKGDIR"

abinfo "Building IIO (Industrial I/O) tools ..."
cd "$SRCDIR"/tools/iio
make
abinfo "Installing IIO (Industrial I/O) tools ..."
make install \
    INSTALL_ROOT="$PKGDIR"

# FIXME: Fails to build on loongson3, riscv64 due to missing
# __NR_sched_getattr.
if ! ab_match_arch loongarch64 && \
   ! ab_match_arch loongarch64_nosimd && \
   ! ab_match_arch loongson3 && \
   ! ab_match_arch riscv64; then
    abinfo "Building tracing tools ..."
    cd "$SRCDIR"/tools/tracing
    make
    abinfo "Installing tracing tools ..."
    make install \
        PREFIX=/usr \
        sbindir="/usr/bin" \
        MANDIR="/usr/share/man" \
        DESTDIR="$PKGDIR"
fi

abinfo "Building GPIO tools ..."
cd "$SRCDIR"/tools/gpio
# FIXME: Build fails with parallelism.
make -j1
abinfo "Installing GPIO tools ..."
make install \
    DESTDIR="$PKGDIR" \
    -j1

if ab_match_arch amd64; then
    abinfo "Building turbostat ..."
    cd "$SRCDIR"/tools/power/x86/turbostat
    make
    abinfo "Installing turbostat ..."
    make install \
        DESTDIR="$PKGDIR"
fi

if ab_match_arch amd64 || \
   ab_match_arch arm64; then
    abinfo "Building Hyper-V tools ..."
    cd "$SRCDIR"/tools/hv
    CFLAGS+=' -DKVP_SCRIPTS_PATH=\"/usr/lib/hyperv/kvp_scripts/\"' \
        make
    abinfo "Installing Hyper-V tools ..."
    for i in hv_kvp_daemon hv_vss_daemon; do
        install -Dvm755 ./$i \
            "$PKGDIR"/usr/bin/$i
    done
    # Note: hv_fcopy_uio_daemon is only available for x86.
    if ab_match_arch amd64; then
        install -Dvm755 hv_fcopy_uio_daemon \
            "$PKGDIR"/usr/bin/hv_fcopy_uio_daemon
    fi
    install -dvm755 "$PKGDIR"/usr/lib/hyperv/kvp_scripts
fi

# Perf uses inline asm blocks that is unsupported on armv4.
if ! ab_match_arch armv4 ; then
    abinfo "Building perf ..."
    __PERF_MAKE_OPTIONS=(
        prefix=/usr
        lib=lib/perf
        perfexecdir=lib/perf
        NO_SDT=1
        PYTHON=python3
        PYTHON_CONFIG=python3-config
        PERF_VERSION="$PKGVER"
        DESTDIR="$PKGDIR"
        WERROR=0
        V=1
    )
    cd "$SRCDIR"/tools/perf
    # FIXME: Build fails with parallelism.
    make all man \
        -f ./Makefile.perf \
        "${__PERF_MAKE_OPTIONS[@]}" \
        -j1
    abinfo "Installing perf ..."
    make install install-man \
        -f ./Makefile.perf \
        "${__PERF_MAKE_OPTIONS[@]}" \
        -j1
    mkdir -vp "${PKGDIR}/usr/share/bash-completion/completions"
    mv -v "${PKGDIR}/etc/bash_completion.d/perf" "${PKGDIR}/usr/share/bash-completion/completions"
    rmdir -v "${PKGDIR}/etc/bash_completion.d"

    cd "$SRCDIR"
fi

abinfo "Fixing up permissions ..."
find "$PKGDIR"/usr/share -type f -exec chmod a-x {} \;

export ARCH="$OLDARCH"
unset OLDARCH
