# Maintainer (arch:linux-tools): Sébastien Luttringer <seblu@archlinux.org>
# Maintainer: David P. <megver83@parabola.nu>
# Contributor: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
#  - changed upstream to linux-libre
#  - removed hyperV and bpf support


pkgbase=linux-libre-tools
pkgname=(
  'bootconfig'
  'cgroup_event_listener'
  'cpupower'
  'libtraceevent'
  'linux-libre-tools-meta'
  'perf'
#  'python-perf'
  'tmon'
  'usbip'
)
case "$CARCH" in
  armv7h)      pkgname+=('gpio-utils' 'iio-utils') ;;
  i686|x86_64) pkgname+=('turbostat' 'x86_energy_perf_policy') ;;
esac
pkgver=6.1.5
pkgver+=_gnu
pkgrel=1
license=('GPL2')
arch=('x86_64')
arch+=('i686' 'armv7h')
url='https://linux-libre.fsfla.org/'
options=('!strip')
makedepends=('git')
# split packages need all package dependencies set manually in makedepends
# kernel source deps
makedepends+=('asciidoc' 'xmlto')
# perf deps
makedepends+=('perl' 'python' 'slang' 'elfutils' 'libunwind' 'audit' 'zstd' 'libcap')
makedepends_x86_64=('numactl')
makedepends_i686=('numactl')
# cpupower deps
makedepends+=('pciutils')
# usbip deps
makedepends+=('glib2' 'sysfsutils' 'udev')
# tmon deps
makedepends+=('ncurses')
# turbostat deps
[[ "$CARCH" =~ 'x86_64|i686' ]] &&
makedepends+=('libcap')
groups=("$pkgbase")
source=("https://linux-libre.fsfla.org/pub/linux-libre/releases/${pkgver//_/-}/linux-libre-${pkgver//_/-}.tar.xz"{,.sign}
#        "https://linux-libre.fsfla.org/pub/linux-libre/releases/${pkgver%.*}-gnu/patch-${pkgver%.*}-gnu-${pkgver//_/-}.xz"{,.sign}
        'cpupower.default'
        'cpupower.systemd'
        'cpupower.service'
        'usbipd.service'
)
sha256sums=('1d41ec34b7d9741aeebaaf9527d97938c311debe1494950d13df8598a1e8bef6'
            'SKIP'
            '4fa509949d6863d001075fa3e8671eff2599c046d20c98bb4a70778595cd1c3f'
            'b692f4859ed3fd9831a058a450a84d8c409bf7e3e45aac1c2896a14bb83f3d7a'
            '42d2ec9f1d9cc255ee7945a27301478364ef482f5a6ddfc960189f03725ccec2'
            '2e187734d8aec58a3046d79883510d779aa93fb3ab20bd3132c1a607ebe5498f'
)
validpgpkeys=('474402C8C582DAFBE389C427BCB7CF877E7D47A7') # Alexandre Oliva

prepare() {
  cd linux-${pkgver%_*}

  # apply patch from the source array (should be a pacman feature)
  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    src="${src%.xz}"
    [[ $src = *.patch || $src = patch-* ]] || continue
    echo ":: Applying patch $src"
    patch -p1 -N -i "$srcdir/$src"
  done
}

build() {
  echo ':: libtraceevent'
  pushd linux-${pkgver%_*}/tools/lib/traceevent
  make
  popd

  echo ':: perf'
  pushd linux-${pkgver%_*}/tools/perf
  make -f Makefile.perf \
    prefix=/usr \
    lib=lib/perf \
    perfexecdir=lib/perf \
    EXTRA_CFLAGS=' -Wno-error=bad-function-cast -Wno-error=declaration-after-statement -Wno-error=switch-enum' \
    NO_SDT=1 \
    PYTHON=python \
    PYTHON_CONFIG=python-config \
    PERF_VERSION=$pkgver-$pkgrel \
    DESTDIR="$pkgdir"
  popd

  echo ':: cpupower'
  pushd linux-${pkgver%_*}/tools/power/cpupower
  make VERSION=$pkgver-$pkgrel
  popd

if [[ "$CARCH" == x86_64 ]] || [[ "$CARCH" == i686 ]]; then
  echo ':: x86_energy_perf_policy'
  pushd linux-${pkgver%_*}/tools/power/x86/x86_energy_perf_policy
  make
  popd
fi

  echo ':: usbip'
  pushd linux-${pkgver%_*}/tools/usb/usbip
  # Fix gcc compilation
  sed -i 's,-Wall -Werror -Wextra,-fcommon,' configure.ac
  ./autogen.sh
  ./configure --prefix=/usr --sbindir=/usr/bin
  make
  popd

  echo ':: tmon'
  pushd linux-${pkgver%_*}/tools/thermal/tmon
  make
  popd

  echo ':: cgroup_event_listener'
  pushd linux-${pkgver%_*}/tools/cgroup
  make
  popd

if [[ "$CARCH" == x86_64 ]] || [[ "$CARCH" == i686 ]]; then
  echo ':: turbostat'
  pushd linux-${pkgver%_*}/tools/power/x86/turbostat
  make
  popd
fi

  echo ':: hv'
  pushd linux/tools/hv
  CFLAGS+=' -DKVP_SCRIPTS_PATH=\"/usr/lib/hyperv/kvp_scripts/\"' make

if [[ "$CARCH" == armv7h ]]; then
  echo ':: gpio'
  pushd linux-${pkgver%_*}/tools/gpio
  make -j1
  popd

  echo ':: iio'
  pushd linux-${pkgver%_*}/tools/iio
  make -j1
  popd
fi

  echo ':: bootconfig'
  pushd linux/tools/bootconfig
  make
  popd
}

package_linux-libre-tools-meta() {
  pkgdesc='GNU/Linux-libre kernel tools meta package'
  groups=()
  depends=(
    'bootconfig'
    'cgroup_event_listener'
    'cpupower'
    'libtraceevent'
    'perf'
#    'python-perf'
    'tmon'
    'usbip'
  )
  conflicts=(
    'acpidump'
  )
  depends_x86_64=('turbostat' 'x86_energy_perf_policy')
  depends_i686=('turbostat' 'x86_energy_perf_policy')
  depends_armv7h=('gpio-utils' 'iio-utils')
  replaces=(linux-tools-meta)
  conflicts+=(linux-tools-meta)
  provides=(linux-tools-meta)
}

package_libtraceevent() {
  pkgdesc='GNU/Linux-libre kernel trace event library'
  depends=('glibc')

  cd linux-${pkgver%_*}/tools/lib/traceevent
  install -dm 755 "$pkgdir/usr/lib"
  install -m 644 libtraceevent.so "$pkgdir/usr/lib"
}

package_perf() {
  pkgdesc='GNU/Linux-libre kernel performance auditing tool'
  depends=('glibc' 'perl' 'python' 'slang' 'elfutils' 'libunwind' 'binutils'
           'audit' 'coreutils' 'glib2' 'xz' 'zlib' 'libelf' 'bash'
           'zstd' 'libcap')
  depends_x86_64=('numactl')
  depends_i686=('numactl')
  optdepends=('gtk2: support GTK2 browser for perf report')

  cd linux-${pkgver%_*}/tools/perf
  make -f Makefile.perf \
    prefix=/usr \
    lib=lib/perf \
    perfexecdir=lib/perf \
    EXTRA_CFLAGS=' -Wno-error=bad-function-cast -Wno-error=declaration-after-statement -Wno-error=switch-enum' \
    NO_SDT=1 \
    PYTHON=python \
    PYTHON_CONFIG=python-config \
    PERF_VERSION=$pkgver-$pkgrel \
    DESTDIR="$pkgdir" \
    install
  cd "$pkgdir"
  # add linker search path
  mkdir "$pkgdir/etc/ld.so.conf.d"
  echo '/usr/lib/perf' > "$pkgdir/etc/ld.so.conf.d/$pkgname.conf"
  # move completion in new directory
  install -Dm644 etc/bash_completion.d/perf usr/share/bash-completion/completions/perf
  rm -r etc/bash_completion.d
  # no exec on usr/share
  find usr/share -type f -exec chmod a-x {} \;
}

#package_python-perf(){
#  pkgdesc='GNU/Linux-libre kernel performance auditing tool (python bindings)'
#  depends=('python')
#
#  cd linux-${pkgver%_*}/tools/perf
#
#  make install-python_ext PYTHON=python DESTDIR="$pkgdir"
#}


package_cpupower() {
  pkgdesc='GNU/Linux-libre kernel tool to examine and tune power saving related features of your processor'
  backup=('etc/default/cpupower')
  depends=('glibc' 'bash' 'pciutils')
  conflicts=('cpufrequtils')
  replaces=('cpufrequtils')
  install=cpupower.install

  pushd linux-${pkgver%_*}/tools/power/cpupower
  make \
    DESTDIR="$pkgdir" \
    sbindir='/usr/bin' \
    libdir='/usr/lib' \
    mandir='/usr/share/man' \
    docdir='/usr/share/doc/cpupower' \
    install install-man
  popd
  # install startup scripts
  install -Dm 644 $pkgname.default "$pkgdir/etc/default/$pkgname"
  install -Dm 644 $pkgname.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -Dm 755 $pkgname.systemd "$pkgdir/usr/lib/systemd/scripts/$pkgname"
}

package_x86_energy_perf_policy() {
  pkgdesc='Read or write MSR_IA32_ENERGY_PERF_BIAS'
  depends=('glibc')

  cd linux-${pkgver%_*}/tools/power/x86/x86_energy_perf_policy
  install -Dm 755 x86_energy_perf_policy "$pkgdir/usr/bin/x86_energy_perf_policy"
  install -Dm 644 x86_energy_perf_policy.8 "$pkgdir/usr/share/man/man8/x86_energy_perf_policy.8"
}

package_usbip() {
  pkgdesc='An USB device sharing system over IP network'
  depends=('glibc' 'glib2' 'sysfsutils' 'systemd-libs')

  pushd linux-${pkgver%_*}/tools/usb/usbip
  make install DESTDIR="$pkgdir"
  popd
  # module loading
  install -Dm 644 /dev/null "$pkgdir/usr/lib/modules-load.d/$pkgname.conf"
  printf 'usbip-core\nusbip-host\n' > "$pkgdir/usr/lib/modules-load.d/$pkgname.conf"
  # systemd
  install -Dm 644 usbipd.service "$pkgdir/usr/lib/systemd/system/usbipd.service"
}

package_tmon() {
  pkgdesc='Monitoring and Testing Tool for GNU/Linux-libre kernel thermal subsystem'
  depends=('glibc' 'ncurses')

  cd linux-${pkgver%_*}/tools/thermal/tmon
  make install INSTALL_ROOT="$pkgdir"
}

package_cgroup_event_listener() {
  pkgdesc='Simple listener of cgroup events'
  depends=('glibc')

  cd linux-${pkgver%_*}/tools/cgroup
  install -Dm755 cgroup_event_listener "$pkgdir/usr/bin/cgroup_event_listener"
}

package_turbostat() {
  pkgdesc='Report processor frequency and idle statistics'
  depends=('glibc' 'libcap')

  cd linux-${pkgver%_*}/tools/power/x86/turbostat
  make install DESTDIR="$pkgdir"
}

package_gpio-utils() {
  pkgdesc='GPIO character device utilities'
  depends=('glibc')

  cd linux-${pkgver%_*}/tools/gpio
  make install DESTDIR="$pkgdir"
}

package_iio-utils() {
  pkgdesc='Industrial IO utilities'
  depends=('glibc')

  cd linux-${pkgver%_*}/tools/iio
  make install DESTDIR="$pkgdir"
}

package_bootconfig() {
  pkgdesc='Apply, delete or show boot config to initrd'
  depends=('glibc')

  cd linux/tools/bootconfig
  install -dm755 "$pkgdir/usr/bin"
  make install DESTDIR="$pkgdir"
}

