#!/usr/bin/env /bin/bash
if [ ! "$1" ]; then
    DEST="/lib/modules/$(uname -r)/build"
else
    DEST=$1
fi

function verify_continue()
{
    cat << EOF
This will modify your kernel's ieee80211 Kconfig to define a new entry
for CONFIG_IEEE80211_RADIOTAP.
EOF

    read -p "Do you wish to continue? [Yn] " reply
    case $reply in
	Y|y|"") 
	    return 0 
	    ;;
	*) echo "Terminating patch prcoess." 
	    return 1 
	    ;; 
    esac
    
    if [ "$(shell whoami)" != "root" ]; then
	cat << EOF
If this fails, you may need to be root to patch the kernel.
EOF
    fi
}

function newer_version_needed()
{
    cat <<EOF
The ieee80211 subsystem installed does not provide the necessary files
required for radiotap to be defined.  Please download a newer version 
of the ieee80211 subsystem from:

      http://ieee80211.sf.net/

and run 'make patch_kernel KSRC=$DEST' from that package.  You can then
try again.
EOF
}

function check_tree()
{
    [ -e ${DEST}/include/net/ieee80211_radiotap.h ] && return 0

    newer_version_needed
    return 1
}

function patch_files()
{
    [ ! -e ${DEST}/net/ieee80211/Kconfig ] && newer_version_needed && return 1

    cp ${DEST}/net/ieee80211/Kconfig \
	${DEST}/net/ieee80211/Kconfig.bk

    cat <<EOF>>${DEST}/net/ieee80211/Kconfig
config IEEE80211_RADIOTAP
        bool "Enable RAIODTAP headers for capibilities that can support it."
        depends on IEEE80211
        ---help---
          This option will enable the use of RADIOTAP headers for any 
          driver functionality that supports it.  This is frequently used
          when exporting raw 802.11 frames to user space, for example in 
          monitor and parasite modes.

          NOTE:  For most user space monitoring tools (ethereal, etc.)
          you must have a version of libpcap that supports radiotap header
          formats.

EOF
}

function show_results()
{
    cat <<EOF
Kernel has been udpated to include:

ieee80211 CONFIG_IEEE80211_RADIOTAP entry in Kconfig
EOF
}

verify_continue && 
check_tree && 
patch_files && 
show_results
