#!/bin/bash
# work around issues with S4 caused by bluetooth devices

# store some state info to decide whether to unblock bt on resume
STATEFILE=/var/lib/bluetooth-suspend/state

case $1 in
    suspend|suspend_hybrid|hibernate)
        if [ -f $STATEFILE ]; then
            rm -f $STATEFILE
        fi
        if lsmod | grep -q btusb; then
            echo "modprobe btusb" >> $STATEFILE
            # must block bluetooth before we can rmmod btusb
            if rfkill list bluetooth | grep -q "Soft blocked: no"; then
                # remember to unblock
                echo "rfkill unblock bluetooth" >> $STATEFILE
                rfkill block bluetooth
                sleep 2
            fi
            rmmod btusb || true
        fi
        ;;
    resume|thaw)
        # if there's a state file left around, execute it.
        if [ -f $STATEFILE ]; then
            sh $STATEFILE
            rm $STATEFILE
        fi
        ;;
esac
