#!/bin/bash

state_file="/tmp/bt-pre-sleep-state"

case "$1" in
    pre)
        pre_sleep_state="$(LANGUAGE=en rfkill -nr -o type,soft | awk -F ' ' '/bluetooth/{print $2}')"
        echo "$pre_sleep_state" > $state_file
        if [ "$pre_sleep_state" = "unblocked" ]; then
            rfkill block bluetooth
        fi
        ;;
    post)
        if [ -f $state_file ]; then
            pre_sleep_state="$(cat $state_file)"
        fi

        if [ "$pre_sleep_state" = "unblocked" ]; then
            rfkill unblock bluetooth
        fi
        rm "$state_file"
        ;;
esac
