#!/bin/sh

PATH=$PATH:/usr/bin

case "$1" in
suspend)
   if [ "`/usr/sbin/hciconfig`" != "" ]; then
      #If hciconfig outputs anything then there's probably a Bluetooth
      # CF card in the slot so shut it down.
      hcitool dc `hcitool con | grep ACL | sed 's/^.*\([0-9A-F]\{2\}\(:[0-9A-F]\{2\}\)\{5\}\).*$/\1/'`
      hciconfig hci0 down
      killall hciattach > /dev/null 2>&1 
   fi 
;;
resume)
   if [ "`lsmod | grep hci_uart`" != "" ]; then
      #If the hci_usb module is still loaded then there's a serial based
      # Bluetooth CF card in the slot, which only needs a resume to get it going
      # again. 
      rfcomm bind all
      cardctl resume
      hciconfig hci0 up
   else
      for f in /lib/modules/`uname -r`/kernel/drivers/bluetooth/*_cs.o
      do
         #Enumerate all the self-contained Bluetooth CF card drivers
         f=`echo $f | sed 's/\.o$//'`
         f=`basename $f`
         if [ "`lsmod | grep $f`" != "" ]; then
            #If one of these drivers is still loaded, then there is probably
            #a non-serial based Bluetooth CF card in the slot that needs
            #ejecting and reinserting to get it going again
            rfcomm bind all
            cardctl eject
            cardctl insert
            hciconfig hci0 up
         fi
      done
   fi
esac


