#!/bin/bash

tmp=$(mktemp -q -d)

latest_firmware ()
{
cd "$tmp" || exit 1
export FIRMWARE_INSTALL_DIR="/usr/lib/firmware"

if ! wget --timeout=60 http://www.lwfinger.com/b43-firmware/broadcom-wl-6.30.163.46.tar.bz2 ; then
 echo "Some problem occurred during the firmware download. Please check your internet connection."
 exit 1
else
 if [ -d /usr/lib/firmware/b43 ]; then
  echo "Deleting old extracted firmware..."
  rm -rf /usr/lib/firmware/b43
 fi
fi

tar xvjf broadcom-wl-6.30.163.46.tar.bz2
b43-fwcutter -w "$FIRMWARE_INSTALL_DIR" "broadcom-wl-6.30.163.46.wl_apsta.o"

rm -rf "$tmp"

echo "Trying to activate your card..."
modprobe -r b43
modprobe -a b43
}

classic_firmware()
{
cd "$tmp" || exit 1
export FIRMWARE_INSTALL_DIR="/usr/lib/firmware"

if ! wget --timeout=60 http://www.lwfinger.com/b43-firmware/broadcom-wl-5.100.138.tar.bz2 ; then
 echo "Some problem occurred during the firmware download. Please check your internet connection."
 exit 1
else
 if [ -d /usr/lib/firmware/b43 ]; then
  echo "Deleting old extracted firmware..."
  rm -rf /usr/lib/firmware/b43
 fi
fi

tar xvjf broadcom-wl-5.100.138.tar.bz2
cd broadcom-wl-5.100.138/linux || exit 1
b43-fwcutter -w "$FIRMWARE_INSTALL_DIR" wl_apsta.o

rm -rf "$tmp"
echo "Trying to activate your card..."
modprobe -r b43
modprobe -a b43
}

legacy_firmware ()
{
cd "$tmp" || exit 1
export FIRMWARE_INSTALL_DIR="/usr/lib/firmware"

if ! wget --timeout=60 http://downloads.openwrt.org/sources/wl_apsta-3.130.20.0.o ; then
 echo "Some problem occurred during the firmware download. Please check your internet connection."
 exit 1
else
 if [ -d /usr/lib/firmware/b43legacy ]; then
  echo "Deleting old extracted firmware..."
  rm -rf /usr/lib/firmware/b43legacy
 fi
fi

b43-fwcutter -w "$FIRMWARE_INSTALL_DIR" wl_apsta-3.130.20.0.o
rm -rf "$tmp"
echo "Trying to activate your card..."
modprobe -r b43
modprobe -a b43
}

# check environment
if [ "$(stat -c %d/%i /)" != "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
 echo "A chroot environment has been detected."
 echo "Remember this firmware needs kernel >= 2.6.25."
 latest_firmware
 exit 0
else
 echo "No chroot environment found. Starting normal installation"
fi

#check Broadcom's wl module
wl_stat=$(lsmod | grep -wo "wl") || true
if [ "$wl_stat" ] ; then
 echo "[!] WARNING: Conflicting 'wl' module detected! Removal is needed to continue the installation"
 echo "Trying to remove it..."
 rmmod wl
fi

# Fix for BCM4306/3 [14e4:4320] (rev 03)
chip=$(lspci -n | grep -o "14e4:4320 (rev 03)") || true
if [ "$chip" ] ; then
 echo "Your card is BCM4306/3 [14e4:4320] (rev 03), firwmare 5.100.138 will be used"
 classic_firmware
 exit 0
fi

# Fix for BCM4306/3 [14e4:4324] (rev 03)
chip=$(lspci -n | grep -o "14e4:4324 (rev 03)") || true
if [ "$chip" ] ; then
 echo "Your card is BCM4306/3 [14e4:4324] (rev 03), firwmare 5.100.138 will be used"
 classic_firmware
 exit 0
fi

# check chip
pci=$(lspci -n | grep -o "14e4:[1234567890abcdef]\+") || true

if [ -n "$pci" ]; then
 for device in $pci; do
  device_id="$(echo $device | cut -d: -f2)"
  case $device_id in
  4301 | 4306 | 4320 | 4324 | 4325)
   legacy=1
  ;;
  4307 | 4315 | 4318 | 4319 | 4321 | 4328 | 4329 | 432b | 432c | 4331 | 4353 | 4350 | 4357 | a8d8 | 4358 | 4359| 43aa | 43a9)
   latest=1
  ;;
  4311 | 4312)
   classic=1
  ;;
  4358 | 4365 | 4727 | 4360 | 4365 | 43a0 | 43b1)
   unsupported="$unsupported $device_id"
  ;;
  0576 | 4313 | 432a | 432d | 435a | a99d | a8d6 | a8db | 4322)
   nottested=1
  ;;
  *)
  ;;
  esac
 done
fi

if [ "$legacy" ]; then
 echo "A legacy BCM4301, BCM4306 or BCM4306/2 device was found."
 echo "This card uses b43legacy firmware. Trying to install it..."
 legacy_firmware
 exit 0
elif [ "$unsupported" ]; then
 echo -n "Unsupported device(s) found: PCI id "
 for device_id in $unsupported; do echo -n "14e4:$device_id "; done
 echo
 echo "Aborting."
 exit 0
elif [ "$classic" ]; then
 echo "This card has known issues with the newer firmware. Trying to install older version..."
 classic_firmware
 exit 0
elif [ "$nottested" ]; then
 echo "This card is actually not tested. You need to install the driver manually AT YOUR OWN RISK."
 exit 0
elif [ "$latest" ]; then
 echo "This card work with newer firmware. Trying to install it..."
 latest_firmware
 exit 0
fi
