#!/bin/bash
#
# Run this script when you update from one version of iwlwifi
# to the next.
#
# The current Intel iwlwifi driver depends on a version
# of the mac80211 stack that conflicts with the version
# that exists in the kernel. Therefore, the fix is to modify
# the public symbols in Intel's version and run both softmac
# stacks.

cd `dirname $0`

#
# Get the current tarballs and prepare them for munging.
#
./BOM

#
# Make iwlwifi build with 2.6.24
#
patch -p0 < 1.2.0.update.patch 

#
# Here is the algorithm I followed to munge the iwlwifi and mac80211
# tarballs. Hopefully the next update will be close enough that you can
# just run this script.
#

#
# Apply all applicable patches
#
make -C mac80211 KSRC=/lib/modules/`uname -r`/build source
make -C iwlwifi KSRC=/lib/modules/`uname -r`/build source

#
# Insert the include path into the mac80211 Makefile
#
MF=Makefile.mac80211
echo CONFIG_MAC80211=m > ${MF}
echo "NOSTDINC_FLAGS += -I\$(src)/../../include" >> ${MF}
echo "EXTRA_CFLAGS += -DCONFIG_MAC80211_LEDS" >> ${MF}
cat mac80211/compatible/net/mac80211/Makefile >> ${MF}
sed -i -e 's/mac80211\.o/iwlwifi_mac80211\.o/' -e 's/mac80211-objs/iwlwifi_mac80211-objs/g' -e 's/rc80211_simple/iwlwifi_rc80211_simple/g' ${MF}
mv -f ${MF} mac80211/compatible/net/mac80211/Makefile
mv  mac80211/compatible/net/mac80211/rc80211_simple.c mac80211/compatible/net/mac80211/iwlwifi_rc80211_simple.c

#
# Since radiotap is isolated, just build it into the mac80211 module.
#
mv mac80211/compatible/net/wireless/radiotap.c mac80211/compatible/net/mac80211
echo "iwlwifi_mac80211-objs += radiotap.o" >> mac80211/compatible/net/mac80211/Makefile

#
# Remove calls to debugfs_rename.
#
#patch -d mac80211 -p1 < 0001-debugfs.diff

#
# Munge the iwlwifi Makefile
#
MF=Makefile.iwlwifi
echo CONFIG_IWL4965=m > ${MF}
echo CONFIG_IWL3945=m >> ${MF}
echo CONFIG_IWL3945_LEDS=y >> ${MF}
echo CONFIG_IWL4965_LEDS=y >> ${MF}
echo "NOSTDINC_FLAGS += -I\$(src)/../../mac80211/compatible/include" >> ${MF}
echo "EXTRA_CFLAGS += -DCONFIG_IWL4965_QOS -DCONFIG_IWL3945_QOS" >> ${MF}
echo "EXTRA_CFLAGS += -DCONFIG_IWL4965_LEDS -DCONFIG_IWL3945_LEDS" >> ${MF}
cat iwlwifi/compatible/Makefile >> ${MF}
mv -f ${MF} iwlwifi/compatible/Makefile

#
# Apply anti-debug patch to mac80211
#
(cd mac80211/compatible; patch -p0) < mac80211-debug.patch

#
# Apply iwlwifi led support
#
(cd iwlwifi/compatible; patch -p1) < iwlwifi-led-support.patch


#
# Isolate the mac80211 exported symbols and munge every occurence such that
# there is no possibility of conflict with the main kernel.
#
TL=token_list
find mac80211/compatible/net/mac80211 -name "*.[ch]" | \
xargs grep EXPORT_SYMBOL | \
sed -e 's/^.*EXPORT_SYMBOL(//' -e 's/);//' > ${TL}

cat ${TL} | while read token
do
	find . -name "*.[ch]" | while read f
	do
		sed -i "s/${token}/iwlwifi_${token}/g" "${f}"
	done
done
rm -f ${TL}

#
# Build the top level makefile
#
echo "obj-m += mac80211/compatible/net/mac80211/" > Makefile
echo "obj-m += iwlwifi/compatible/" >> Makefile

#
# Get rid of cruft
#
find -name \*.orig | xargs rm -f
