#!/bin/bash

. /etc/pm/functions

suspend_bluetooth()
{
	stopservice dund
	stopservice pand
	stopservice hidd
	stopservice bluetooth

	if [ -f /proc/acpi/ibm/bluetooth ]; then
		savestate ibm_bluetooth $(awk '{ print $2 ; exit; }' /proc/acpi/ibm/bluetooth)
	else
		savestate ibm_bluetooth missing
	fi

	for x in rfcomm hidp l2cap ohci1394 ieee1394 hci_usb bluetooth ; do
		modunload $x
	done
}

resume_bluetooth()
{
	for x in bluetooth hci_usb ieee1394 ohci1394 l2cap hidp rfcomm ; do
		modreload $x
	done

	case "$(restorestate ibm_bluetooth)" in
		enabled)
			echo enable > /proc/acpi/ibm/bluetooth
			;;
		disabled)
			echo disable > /proc/acpi/ibm/bluetooth
			;;
		*)
			;;
	esac

	restartservice bluetooth
	restartservice hidd
	restartservice pand
	restartservice dund
}

case "$1" in
	hibernate|suspend)
		suspend_bluetooth
		;;
	thaw|resume)
		resume_bluetooth
		;;
	*)
		;;
esac

exit $?
