#!/bin/bash

#(some rights reserved) 2008 mcsmart
#license: http://creativecommons.org/licenses/by-nc-sa/3.0/
#the author is not responsible for any damages caused by this script 



#simple check if the user is logged in as root
USER=`whoami`
if [[ $USER != "root" ]]; then
	echo "You need to have root-proviledges!"
	echo "To run this script, simply type 'sudo $0' and enter your password"
	exit
fi


#name of the kext
TYPE="AppleAirPortBrcm4311.kext"

#default locations
DEFAULTKEXT="/System/Library/Extensions/IO80211Family.kext/Contents/PlugIns/AppleAirPortBrcm4311.kext"
DEFAULTBCKP="/backup"

#select the kextfile
function kextsel() {
	KEXT=""
        echo "Where is the $TYPE located?"
	echo "Please enter the full path or simply press return if the default path is correct for your system!"
        read -p "[$DEFAULTKEXT] " KEXT
        if [[ $KEXT == "" ]]; then
                KEXT=$DEFAULTKEXT;
        fi
}

#select a location for the backup of the Info.plist file
function backupsel() {
	BACKUP=""
        echo "Please enter a place for the backup of the kext's Info.plist!"
        echo "If you don't want a backup, type '/dev/null' :)"
        read -p "[$DEFAULTBCKP] " BACKUP
        if [[ $BACKUP == "" ]]; then
                BACKUP=$DEFAULTBCKP;
		#create the dir if it does not exist
		if !([ -e $BACKUP ]); then
			mkdir $BACKUP
		fi
        fi
}



clear
echo "####################################"
echo "#mcsmart's bcm43xx wireless enabler#"
echo "#            v. 0.5.3              #"
echo "####################################"
echo "This script will enable your Broadcom wireless card (aka AirPortOne)!"
echo "You may run this script after every AirPort Update released by Apple to re-enable your card."
echo "For more information visit: http://forum.insanelymac.com/index.php?showtopic=51725"
echo ""
echo "Use this script at your own risk!"
echo "The author is not responsible for any damages that may be caused by this script!"
echo ""
echo "Supported chipsets:"
echo "- Broadcom BCM4306 802.11 b/g Wireless LAN"
echo "- Broadcom BCM4309 802.11 b Wireless LAN"
echo "- Broadcom BCM4318 [AirForce One 54g] 802.11g Wireless LAN"
echo "- Broadcom BCM4320 b/g"
echo "- Broadcom BCM4321 a/g/n"
echo "- Broadcom BCM4324 802.11 a/b/g Wireless LAN"
echo "- Broadcom BCM4325 802.11 a/b/g Wireless LAN"
echo "- All Apple AirPortOne cards"
echo "- Probably many other BCM43XX chipsets"
echo ""
echo ""
echo "Press Enter to continue"
read none

#select the kext
clear
kextsel

#try again if the kext file is invalid
while !([ -e $KEXT/Contents/Info.plist ]); do
	echo ""
	echo "Invalid path, try it again!"
	kextsel
done

#final location of the Info.plist
FILE="$KEXT/Contents/Info.plist"

#select a path to backup the Info.plist
clear
backupsel

#try again if the kext file is invalid
while !([ -e $BACKUP ]); do
        echo ""
        echo "Invalid path, try it again!"
        backupsel
done

echo $BACKUP

cat $FILE > /tmp/patch
cp $FILE $BACKUP/Info.plist.$TYPE
cat /tmp/patch|grep -B 100 "<array>" > /tmp/new
echo "				<string>pci106b,4e</string>
				<string>pci14e4,4301</string>
				<string>pci14e4,4302</string>
				<string>pci14e4,4303</string>
				<string>pci14e4,4304</string>
				<string>pci14e4,4305</string>
				<string>pci14e4,4306</string>
				<string>pci14e4,4307</string>
				<string>pci14e4,4308</string>
				<string>pci14e4,4309</string>
				<string>pci14e4,4310</string>
				<string>pci14e4,4311</string>
                                <string>pci14e4,4312</string>
                                <string>pci14e4,4313</string>
				<string>pci14e4,4314</string>
				<string>pci14e4,4315</string>
				<string>pci14e4,4316</string>
				<string>pci14e4,4317</string>
                                <string>pci14e4,4318</string>
                                <string>pci14e4,4319</string>
                                <string>pci14e4,4320</string>
				<string>pci14e4,4321</string>
				<string>pci14e4,4322</string>
				<string>pci14e4,4323</string>
				<string>pci14e4,4324</string>
				<string>pci14e4,4325</string>
				<string>pci14e4,4326</string>
				<string>pci14e4,4327</string>
				<string>pci14e4,4328</string>
                                <string>pci14e4,4329</string>
				<string>pci14e4,431a</string>
				<string>pci14e4,431b</string>
				<string>pci14e4,431c</string>
				<string>pci14e4,431d</string>
				<string>pci14e4,431e</string>
				<string>pci14e4,431f</string>
                                <string>pci14e4,432a</string>
				<string>pci14e4,432b</string>
                                <string>pci14e4,432c</string>
                                <string>pci14e4,432d</string>
				<string>pci14e4,432e</string>
				<string>pci14e4,432f</string>
				<string>pci14e4,4330</string>
				<string>pci14e4,4331</string>
				<string>pci14e4,4332</string>
				<string>pci14e4,4333</string>
				<string>pci14e4,4334</string>
				<string>pci14e4,4335</string>
				<string>pci14e4,4336</string>
				<string>pci14e4,4337</string>
				<string>pci14e4,4338</string>
				<string>pci14e4,4339</string>" >> /tmp/new 
cat /tmp/patch|grep -A 100 "</array>" >> /tmp/new
cp /tmp/new $FILE
chown root:wheel $FILE
chmod 0644 $FILE
rm /tmp/patch
rm /tmp/new

#delete AirPort2 kext
if [ -e /System/Library/Extensions/AppleAirPort2.kext ]; then
	clear
	echo "Do you want to delete the AppleAirPort2.kext (recommended)? [yes|no]"
	echo "The kext will be moved to your backup location"
	read -p "[yes] " AP2
	if [[ $AP2 == "yes" ]]; then
		mv /System/Library/Extensions/AppleAirPort2.kext $BACKUP
	elif [[ $AP2 == "" ]]; then
		mv /System/Library/Extensions/AppleAirPort2.kext $BACKUP
	fi
fi

#delete the kextcache
if [ -e /System/Library/Extensions.kextcache ]; then rm -rf /System/Library/Extensions.kextcache; fi
if [ -e /System/Library/Extensions.mkext ];then rm -rf /System/Library/Extensions.mkext;fi
#Leopard Stuff
if [ -e /System/Library/Extensions/Caches/com.apple.kext.info ]; then rm /System/Library/Extensions/Caches/com.apple.kext.info;fi

echo "$TYPE successfully patched!"

#repair permissions
echo -n "Reparing permissions:"
chmod -R 755 /System/Library/Extensions
chown -R root:wheel /System/Library/Extensions
echo " [OK]"

echo "Please reboot!"

