#!/bin/sh

LOAD=$(ps -A -o %cpu | awk '{ s+=$1 } END { print s }' | sed 's/\..*//')
if [ "${LOAD}" -ge 350 ]; then
  # shellcheck disable=SC2312
  if [ "$(uname -o)" = "Darwin" ]; then
    PROCESS=$(ps axhc -o command,%cpu | awk '{ print $NF,$0 }' \
      | sort -rhk1 | cut -d' ' -f2 \
      | grep -v 'IntuneMdmDaemon' \
      | head -n 1)
      # ignore this process (it is always there at the top)
  else
    PROCESS=$(ps -eo cmd --sort=-%cpu --no-headers | head -n 1 | xargs basename --)
  fi
  echo "#[fg=red]${LOAD}%(${PROCESS})#[fg=default]"
else
  echo ""
fi
