#!/bin/bash

#
# This scruipt is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.

#
# 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

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.
#

if [ -z "$KSRC" ]; then
	KSRC=/lib/modules/`uname -r`/build
fi

#
# Apply all applicable patches
#
make -C mac80211 KSRC=${KSRC} source
make -C iwlwifi KSRC=${KSRC} source

#
# Insert the include path into the mac80211 Makefile
#
MF=Makefile.mac80211
echo CONFIG_MAC80211=m > ${MF}
echo "NOSTDINC_FLAGS += -I\$(src)/../../include" >> ${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 "NOSTDINC_FLAGS += -I\$(src)/../../mac80211/compatible/include" >> ${MF}
echo "EXTRA_CFLAGS += -DCONFIG_IWLWIFI_QOS=1" >> ${MF}
cat iwlwifi/compatible/Makefile >> ${MF}
mv -f ${MF} iwlwifi/compatible/Makefile

#
# Do not auto-load iwl3945. ipw3945 gets precedence.
#
sed -i '/MODULE_DEVICE_TABLE/d' iwlwifi/origin/iwl-3945.c

#
# Munge include file paths since there are some duplicates.
# The differences appear to be a superset, so using these versions instead of what
# is in the kernel tree ought to be OK.
#
#IF="linux/nl80211.h linux/ieee80211.h linux/wireless.h net/cfg80211.h"
#IF="${IF} net/ieee80211_radiotap.h net/mac80211.h net/wext.h"
#IF="${IF} net/iw_handler.h net/wireless.h"

#for i in ${IF}
#do
#	find . -name "*.[ch]" | while read f
#	do
#		sed -i "s;<$i>;<include/$i>;" "${f}"
#	done
#done

#
# 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

