#!/usr/bin/env bash

# first check uEnv.txt because it will specify our kernel in uname_r
if [ -f /boot/uEnv.txt ]; then
    kernel="`cat /boot/uEnv.txt | grep uname_r= | sed 's/uname_r=//'`"
    if [ "${kernel}" != "" ]; then
        echo "${kernel}"
        exit 0
    fi
fi

# then check dpkg-query because it will contain the latest installed kernel
# (which will most likely be the kernel we are running)
dpkg_query="`which dpkg-query`"
if [ $? = 0 ]; then
    kernel="`${dpkg_query} -W -f='${binary:Package}\n' linux-image-* | head -n 1 | sed 's/linux-image-//'`"
    if [ "${kernel}" != "" ]; then
        echo "${kernel}"
        exit 0
    fi
fi

# last we just grab what uname -r says
kernel="`uname -r`"
echo "${kernel}"
exit 0
