#!/bin/sh

# initialize tmp files
touch tmp
touch tmp2

DEP=`which lsusb`

echo
echo "* This script will automatically determine the wireless"
echo "* adapter inserted and install the appropriate drivers."
echo "* DEPENDS ON: lsusb and access to ftp://ftp.embeddedarm.com."
echo

if [ $DEP ]; then
  echo 
else
  echo "Failed to detect lsusb."
  echo "To install lsusb, the usbutils package must be installed.  Try..."
  echo "   dhclient eth0"
  echo "   apt-get install usbutils"
  echo 
  echo "After installing this package, re-run the script."
  echo
  exit
fi

echo "Downloading known-devices list... "
wget ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-arm-sbc/ts-7500-linux/binaries/wifi/known-devices

lsusb | cut -d' ' -f 6 | while read line
do
   grep "$line" known-devices > tmp 
   if [ $? = 0 ]; then
      
      echo -n "Detected: "

      awk 'BEGIN{FS=",";}
      {
         {print $2}
      }' < tmp

      echo "Running known-devices installation script... " 
      cut -d',' -f 3 tmp > tmp2 
      chmod +x tmp2
      ./tmp2
   fi
done

if [ `ls -l tmp2 | awk '{print $5}'` -eq 0 ]
then
  echo "No known devices found."
fi

rm tmp*
rm install.sh
rm known-devices
exit
