#!/usr/bin/env /bin/bash
target=patch_kernel

prog=$0
[ "${prog/*$target/$target}" = "$target" ] || {
    echo "This script must not be sourced."
    return 1
}  || return 1

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

IEEE80211_VER=$2

function compat_version() {
    (( IEEE80211_VER != 0 )) && return 0
    IEEE80211_VER=$(snapshot/check_ieee80211_compat "$DEST/include")
    cat <<EOF

Using ieee80211 compatibility version: $IEEE80211_VER

EOF

}

function verify_continue()
{
    cat << EOF
This will install this driver into your kernel tree located here:

  ${DEST}

If you would like to instal to a different location, run
this as follows: 

  make KSRC=/path/to/kernel patch_kernel

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 clean_tree()
{
cat <<EOF
Removing any project entries from:
  $DEST/drivers/net/wireless/Kconfig
EOF
    snapshot/remove_kconfig_entry IPW3945 $DEST/drivers/net/wireless/Kconfig | 
    grep -v "^source.*ipw3945" > .tmp-Kconfig

cat <<EOF
Fixing up project entries in:
  $DEST/drivers/net/wireless/Makefile
EOF

    grep -v "CONFIG_IPW3945" \
	${DEST}/drivers/net/wireless/Makefile |
        grep -v "IEEE80211_API_VERSION" |
	grep -v "IPW3945_COMPAT" > .tmp-Makefile

    cat <<EOF >> .tmp-Makefile
obj-\$(CONFIG_IPW3945)           += ipw3945.o
EXTRA_CFLAGS += -DIPW3945_COMPAT=$IEEE80211_VER
EOF

    sed -i -e "s:^endmenu:source \"drivers/net/wireless/Kconfig.ipw3945\"\n\nendmenu:g" .tmp-Kconfig

cat <<EOF
Fixing up project entries in:
  $DEST/.config
EOF
    grep -v "CONFIG_IPW3945" ${DEST}/.config > .tmp-config

cat <<EOF
Removing project entries in:
  $DEST/include/linux/autoconf.h
EOF
    grep -v "CONFIG_IPW3945" ${DEST}/include/linux/autoconf.h > .tmp-autoconf

}

function copy_files()
{
    cp README.ipw3945 ${DEST}/Documentation/networking
    cp in-tree/Kconfig.ipw3945 ${DEST}/drivers/net/wireless
    cp ipw3945.{c,h} ipw3945_daemon.h ${DEST}/drivers/net/wireless/
}

function add_to_tree() 
{
    cp ${DEST}/drivers/net/wireless/Kconfig \
	${DEST}/drivers/net/wireless/Kconfig.bk
    cp ${DEST}/drivers/net/wireless/Makefile \
	${DEST}/drivers/net/wireless/Makefile.bk
    cp ${DEST}/include/linux/autoconf.h \
	${DEST}/include/linux/autoconf.h.bk
    cp ${DEST}/.config \
	${DEST}/.config.bk

    mv -f .tmp-Kconfig ${DEST}/drivers/net/wireless/Kconfig
    mv -f .tmp-Makefile ${DEST}/drivers/net/wireless/Makefile
    mv -f .tmp-autoconf ${DEST}/include/linux/autoconf.h
    mv -f .tmp-config ${DEST}/.config
}

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

Intel PRO/Wireless 3945ABG Network Connection driver for Linux

you can now edit your kernel's configuration via 'make menuconfig' and 
build as you would any kernel image and set of modules.
EOF
}

snapshot/check_kernel $DEST &&
verify_continue && 
compat_version &&
clean_tree && 
copy_files && 
add_to_tree && 
show_results
