#!/bin/sh

# the purpose of this script is to tell if user needs to install 
# dell_rbu package.

rbu_not_loaded(){
    if /sbin/lsmod | grep -q '^dell_rbu '; then
        return 1
    fi
    return 0
}

try_load_rbu(){
    if rbu_not_loaded; then
        /sbin/modprobe dell_rbu > /dev/null 2>&1
        if rbu_not_loaded; then
            return 1
        fi
    fi
    return 0
}


if try_load_rbu; then
    exit 0
fi

echo "The dell_rbu driver does not appear to be installed for this kernel." 1>&2
echo "Checking kernel version..."

kernel=$(uname -r)

need_rbu=0
rbu_not_available=0

# quick and easy, but not elegant. :-)

if [ "${kernel:0:3}" = "2.4" ]; then
    rbu_not_available=1
elif [ "${kernel:0:6}" = "2.6.0-" -o $kernel = "2.6.0"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.1-" -o $kernel = "2.6.1"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.2-" -o $kernel = "2.6.2"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.3-" -o $kernel = "2.6.3"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.4-" -o $kernel = "2.6.4"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.5-" -o $kernel = "2.6.5"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.6-" -o $kernel = "2.6.6"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.7-" -o $kernel = "2.6.7"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.8-" -o $kernel = "2.6.8"]; then
    need_rbu=1
elif [ "${kernel:0:6}" = "2.6.9-" -o $kernel = "2.6.9"]; then
    need_rbu=1
elif [ "${kernel:0:7}" = "2.6.10-" -o $kernel = "2.6.10" ]; then
    need_rbu=1
elif [ "${kernel:0:7}" = "2.6.11-" -o $kernel = "2.6.11" ]; then
    need_rbu=1
elif [ "${kernel:0:7}" = "2.6.12-" -o $kernel = "2.6.12" ]; then
    need_rbu=1
elif [ "${kernel:0:7}" = "2.6.13-" -o $kernel = "2.6.13" ]; then
    need_rbu=1
fi

if [ $rbu_not_available -eq 1 ]; then
    echo
    echo "Sorry, but you will not be able to do BIOS updates via RBU using this kernel." 1>&2
    echo "  You need the dell_rbu driver, but it is not available for this kernel." 1>&2
fi

if [ $need_rbu -eq 1 ];then
    echo "You need to install the dell_rbu driver."
fi

exit 1
