#!/bin/sh

# check-kernel 0.6.3

verwendung() {
  >&2 echo 'check-kernel checks if the installed kernel is currently running'
  >&2 echo ''
  >&2 echo 'Usage: check-kernel [OPTIONS]'
  >&2 echo '  -r,--reboot     reboot system if installed kernel is not yet running'
  >&2 echo \
'  --help          display this help and exit
  --version       display version and exit'
  exit 1
}

eval set -- "$(
  getopt -o r \
    --long reboot \
    --long help \
    --long version \
    -n "$(basename "$0")" -- "$@" \
    || echo verwendung
)"

reboot=false
while true; do
  case "$1" in
    -r|--reboot)
      reboot=true
    ;;
    --help)
      verwendung 0
      ;;
    --version)
      echo '0.6.3'
      exit 0
      ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo "FEHLER: Verstehe Option \"$1\" doch nicht! Ich beende."
      verwendung
    ;;
  esac
  shift
done

if [ -f '/etc/check-kernel.conf' ]; then
  if ! . '/etc/check-kernel.conf'; then
    >&2 printf 'Sourcing "/etc/check-kernel.conf" failed.\n'
    exit 1
  fi
fi

unset installed
if command -v pkgrm >/dev/null \
&& command -v pkgadd >/dev/null \
&& [ -d /etc/ports ]; then
  running=$(
    uname -r
  )
  installed=$(
    ls /boot | \
      sed -n '
        s/^vmlinuz-\([0-9.]\+\)$/\1/
        T
        p
      ' | \
      sort -V | \
      tail -n1
  )
elif which pacman >/dev/null 2>&1; then
  # arch linux
  running=$(
    uname -r | \
      sed '
        s|-ARCH$||
        s|-rpi\(-legacy\)\?$||
        s#\(\.0\)\?-ar\(ch\|tix\)#.#
      '
  )
  installed=$(
    {
      pacman -Q linux 2>/dev/null || \
      pacman -Q | \
        grep '^linux-' | \
        sed '
          s/-headers / /
        ' | \
        uniq -d
    } | \
      cut -d' ' -f2 | \
      sed 's/\.ar\(ch\|tix\)/./' | \
      sort -V | \
      tail -n1
  )
elif which apt >/dev/null 2>&1; then
  # debian
  running=$(
    uname -r
  )
  installed=$(
    dpkg-query -Wf'${Package} ${Status}\n' 'linux-image-*' | \
      sed -n '
        s|^linux-image-||
        T
        s/ install ok installed$//
        T
        p
      ' | \
      grep -x '\([^-]\+-\)\{2\}[^-]\+' | \
      sort -V | \
      tail -n1
  )
else
  running=$(
    uname -r
  )
  installed=$(
    ls /boot | \
      sed -n '
        s/^vmlinuz-\([0-9.]\+\)$/\1/
        T
        p
      ' | \
      sort -V | \
      tail -n1
  )
fi
installed=$(
  printf '%s\n' "${installed}" | \
    sed '
      s/\(\.0\)\+$//
    '
)
running=$(
  printf '%s\n' "${running}" | \
    sed '
      s/\(\.0\)\+$//
    '
)

if [ -z "${installed}" ] || \
  [ "$(echo "${installed}" | wc -l)" -ne 1 ]; then
  >&2 printf 'Cannot determin installed kernel.\n'
  exit 2
fi

if [ "${running}" = "${installed}" ]; then
  if ! ${reboot}; then
    >&2 printf 'The installed kernel (%s) is currently running.\n' \
      "${installed}"
  fi
  exit 0
else
  >&2 printf 'The installed (%s) and running kernel (%s) differ.\n' \
    "${installed}" \
    "${running}"
  if ${reboot}; then
    if command -v shutdownasap >/dev/null; then
      >&2 printf 'Press enter to reboot %s ...' \
        "$(uname -n)"
      read s
      if [ -z "${s}" ]; then
        if command -v screen >/dev/null; then
          screen -d -m shutdownasap -R
        else
          shutdownasap -R
        fi
      fi
    else
      if ! reboot_check_hook; then
        >&2 printf 'reboot_check_hook() failed.\n'
        exit 1
      fi
      >&2 printf 'Press enter to reboot %s ...' \
        "$(uname -n)"
      read s
      if [ -z "${s}" ]; then
        PATH=$PATH:/usr/bin:/usr/sbin:/bin:/sbin reboot
      fi
    fi
  else
    exit 1
  fi
fi
