#!/bin/bash

# Copyright 2017-2022  Patrick J. Volkerding, Sebeka, MN, USA
# Copyright 2017-2022  Heinz Wiesinger, Amsterdam, The Netherlands
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PKGNAM=kernel-tools
VERSION=${VERSION:-$(uname -r)}
KERNELRELEASE=${KERNELRELEASE:-$(uname -r)}
BUILD=${BUILD:-1}

# concurrent build results in errors
NUMJOBS=${NUMJOBS:--j1}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

rm -rf $PKG
mkdir -p $TMP $PKG/usr/doc/$PKGNAM-$VERSION

cd /lib/modules/$KERNELRELEASE/source/tools

# page-types - Tool for querying page flags
# page_owner_sort - User-space helper to sort the output of /sys/kernel/debug/page_owner
# Slabinfo - Tool to get reports about slabs
make -C vm V=1 page-types slabinfo page_owner_sort || exit 1
install -D -m0755 vm/page-types $PKG/usr/bin/page-types
install -D -m0755 vm/slabinfo $PKG/usr/bin/slabinfo
install -D -m0755 vm/page_owner_sort $PKG/usr/bin/page_owner_sort

# Utility to get per-pid and per-tgid delay accounting statistics
make -C accounting CFLAGS="$SLKCFLAGS -I../../usr/include" || exit 1
install -D -m0755 accounting/getdelays $PKG/usr/bin/getdelays

# bootconfig utility
make -C bootconfig CFLAGS="$SLKCFLAGS -Wall -g -I./include" || exit 1
make -C bootconfig install bindir=/usr/bin DESTDIR=$PKG || exit 1

# Simple listener of cgroup events
make -C cgroup CFLAGS="$SLKCFLAGS -Wall -Wextra" || exit 1
install -D -m0755 cgroup/cgroup_event_listener $PKG/usr/bin/cgroup_event_listener

# Interface to snoop mode driver for TI PCILynx 1394 controllers
make -C firewire CFLAGS="$SLKCFLAGS -Wall -g" || exit 1
make -C firewire install prefix=$PKG/usr || exit 1

# Parser/loader for IHEX formatted data.
make -C firmware CFLAGS="$SLKCFLAGS -Wall -Wextra -g" || exit 1
install -D -m0755 firmware/ihex2fw $PKG/usr/bin/ihex2fw

# GPIO tools
# gpio-event-mon - monitor GPIO line events from userspace
# gpio-hammer - example swiss army knife to shake GPIO lines on a system
# lsgpio - example on how to list the GPIO lines on a system
CFLAGS="$SLKCFLAGS" make -C gpio V=1 || exit 1
make -C gpio install DESTDIR=$PKG || exit 1

# Hyper-V Tools
make -C hv CFLAGS="$SLKCFLAGS" || exit 1
make -C hv install DESTDIR=$PKG || exit 1

# Industrial I/O utilities
make -C iio CFLAGS="$SLKCFLAGS" || exit 1
make -C iio install DESTDIR=$PKG || exit 1

# io_uring test programs
make -C io_uring io_uring-cp CFLAGS="$SLKCFLAGS" || exit 1
install -D -m0755 io_uring/io_uring-cp $PKG/usr/bin/io_uring-cp
#  Fails to build with:
#  io_uring-bench.c:493:28: error: ‘O_NOATIME’ undeclared (first use in this function); did you mean ‘MS_NOATIME’?
#install -D -m0755 io_uring/io_uring-bench $PKG/usr/bin/io_uring-bench

# Report KVM kernel module event counters
make -C kvm/kvm_stat || exit 1
make -C kvm/kvm_stat install INSTALL_ROOT=$PKG MANDIR=usr/man || exit 1

# Simple Disk Sleep Monitor
make -C laptop/dslm CFLAGS="$SLKCFLAGS -I../../usr/include" || exit 1
install -D -m0755 laptop/dslm/dslm $PKG/usr/bin/dslm

# Disk protection for HP/DELL machines
make -C laptop/freefall CFLAGS="$SLKCFLAGS" || exit 1
install -D -m0755 laptop/freefall/freefall $PKG/usr/bin/freefall

# LEDs tools
make -C leds CFLAGS="$SLKCFLAGS -Wall -Wextra -g -I../../include/uapi" || exit 1
install -D -m0755 leds/uledmon $PKG/usr/bin/uledmon
install -D -m0755 leds/led_hw_brightness_mon $PKG/usr/bin/led_hw_brightness_mon

# Library for tracing kernel events
make \
  -C lib/traceevent \
  CFLAGS="$SLKCFLAGS -Wall -g" \
  prefix=/usr \
  mandir=/usr/man \
  V=1 \
  DESTDIR=$PKG \
  install || exit 1
# Drop static library
rm -f $PKG/usr/lib$LIBDIRSUFFIX/libtraceevent.a

# BPF utilities
# bpf_asm - Minimal BPF assembler
# bpf_dbg - Minimal BPF debugger
# bpf_jit_disasm - Minimal BPF JIT image disassembler
# bpftool - tool for inspection and simple manipulation of eBPF programs and maps
make -C bpf bpf_asm bpf_dbg bpf_jit_disasm bpftool || exit 1
make -C bpf bpftool_install DESTDIR=$PKG prefix=/usr || exit 1
make -C bpf/bpftool doc || exit 1
make -C bpf/bpftool doc-install DESTDIR=$PKG prefix=/usr || exit 1

install -m 755 bpf/bpf_jit_disasm $PKG/usr/sbin/bpf_jit_disasm
install -m 755 bpf/bpf_dbg $PKG/usr/sbin/bpf_dbg
install -m 755 bpf/bpf_asm $PKG/usr/sbin/bpf_asm

# runqslower - Trace long process scheduling delays
# Fails to build with:
#   Couldn't find kernel BTF; set VMLINUX_BTF to specify its location.
#make -C bpf runqslower || exit 1
#install -m 755 bpf/runqslower $PKG/usr/bin/runqslower

# User-land tool to combine with CONFIG_STACK_VALIDATION
CFLAGS="$SLKCFLAGS" make -C objtool V=1 || exit 1
install -D -m0755 objtool/objtool $PKG/usr/bin/objtool
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/objtool
cp -a objtool/Documentation/*.txt $PKG/usr/doc/$PKGNAM-$VERSION/objtool/

# Performance analysis utility

make \
  -C perf \
  -f Makefile.perf \
  EXTRA_CFLAGS="$SLKCFLAGS" \
  prefix=/usr \
  mandir=/usr/man \
  tipdir=/doc/$PKGNAM-$VERSION/perf \
  NO_SDT=1 \
  NO_LIBNUMA=1 \
  NO_LIBAUDIT=1 \
  GTK2=1 \
  LIBCLANGLLVM=1 \
  PYTHON=python3 \
  V=1 \
  DESTDIR=$PKG \
  install install-man || exit 1

mkdir -p $PKG/usr/share/bash-completion/completions/
mv $PKG/etc/bash_completion.d/perf \
  $PKG/usr/share/bash-completion/completions/
rm -rf $PKG/etc/bash_completion.d

# Shows and sets processor power related values
CFLAGS="$SLKCFLAGS" \
make \
  -C power/cpupower \
  prefix=/usr \
  libdir=/usr/lib$LIBDIRSUFFIX \
  docdir=/usr/doc/$PKGNAM-$VERSION/cpupower \
  V=1 \
  DESTDIR=$PKG \
  install || exit 1
mv $PKG/etc/cpufreq-bench.conf $PKG/etc/cpufreq-bench.conf.new
mkdir -p $PKG/etc/{default,rc.d}
install -m 644 $CWD/cpupower.default $PKG/etc/default/cpupower.new
install -m 644 $CWD/rc.cpupower $PKG/etc/rc.d/rc.cpupower.new

# suspend/resume/boot timing analysis tools
sed -i "s|lib/pm-graph|lib$LIBDIRSUFFIX/pm-graph|g" power/pm-graph/Makefile
sed -i "s|/share/man|/man|g" power/pm-graph/Makefile
make -C power/pm-graph PREFIX=/usr DESTDIR=$PKG install || exit 1
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/pm-graph
cp -a power/pm-graph/README $PKG/usr/doc/$PKGNAM-$VERSION/pm-graph/

# Compile python scripts
python3 -m compileall "$PKG/usr/lib$LIBDIRSUFFIX/pm-graph"
python3 -O -m compileall "$PKG/usr/lib$LIBDIRSUFFIX/pm-graph"

# Intel Speed Select support tool
make -C power/x86/intel-speed-select || exit 1
make -C power/x86/intel-speed-select bindir=/usr/bin DESTDIR=$PKG install || exit 1

# Show CPU frequency and C-state residency on modern Intel turbo-capable processors
sed -i "s|/share/man|/man|g" power/x86/turbostat/Makefile
CFLAGS="$SLKCFLAGS" \
make \
  -C power/x86/turbostat \
  PREFIX=/usr \
  DESTDIR=$PKG \
  install || exit 1

# Set the energy versus performance policy preference bias on recent X86 processors
sed -i "s|/share/man/|/man/|" power/x86/x86_energy_perf_policy/Makefile
make -C power/x86/x86_energy_perf_policy || exit 1
make -C power/x86/x86_energy_perf_policy DESTDIR=$PKG install || exit 1

# A monitoring and testing tool for Linux kernel thermal subsystem
CFLAGS="$SLKCFLAGS" make -C thermal/tmon || exit 1
make -C thermal/tmon INSTALL_ROOT=$PKG install || exit 1
install -m0644 thermal/tmon/tmon.8 $PKG/usr/man/man8/tmon.8
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/tmon
cp -a thermal/tmon/README $PKG/usr/doc/$PKGNAM-$VERSION/tmon/

# USB device sharing over IP networks

cd usb/usbip
  ./autogen.sh

  CFLAGS="$SLKCFLAGS" \
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --docdir=/usr/doc/$PKGNAM-$VERSION \
    --disable-static \
    --build=$ARCH-slackware-linux

  make || exit 1
  make DESTDIR=$PKG install || exit 1

  install -m 644 $CWD/rc.usbipd $PKG/etc/rc.d/rc.usbipd.new

  mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/usbip
  cp -a AUTHORS COPYING INSTALL README \
    $PKG/usr/doc/$PKGNAM-$VERSION/usbip
cd ../..

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

# Remove empty directory
rmdir $PKG/var/lib $PKG/var

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

find $PKG/usr/doc/$PKGNAM-$VERSION -type f -exec chmod 0644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
