Drop grml-postfix
[grml-scripts.git] / usr_sbin / bt-hid
1 #!/bin/sh
2 # Filename:      bt-hid
3 # Purpose:       connect human input device via bluetooth to local system
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 . /etc/grml/lsb-functions
10
11 if [ "$(id -ru)" != "0" ] ; then
12    eerror "Need root privileges. Please run $0 as user root." ; eend 1
13    exit 1
14 fi
15
16 case "$1" in
17   start)
18    einfo "Starting bluetooth support."
19     eindent
20
21     if pgrep dbus-daemon 1>/dev/null; then
22       einfo "dbus already running." ; eend 0
23     else
24       einfo "Starting dbus."
25       /etc/init.d/dbus start 1>/dev/null ; eend $?
26     fi
27
28     if pgrep sdpd 1>/dev/null; then
29       einfo "Main bluetooth daemons seem to be already running." ; eend 0
30     else
31       einfo "Starting main bluetooth support."
32       /etc/init.d/bluetooth start 1>/dev/null ; eend $?
33     fi
34
35     if pgrep hcid 1>/dev/null; then
36       einfo "hcid already running."
37     else
38       einfo "Starting hcid."
39        HCIINFO=$(mktemp)
40        if start-stop-daemon --start --exec /usr/sbin/hcid 1>$HCIINFO 2>&1 ; then
41          rm -f $HCIINFO
42          eend 0
43        else
44          eerror "hcid could not be started: `cat $HCIINFO`. Exiting."
45          rm -f $HCIINFO
46          eend 1
47          exit 1
48        fi
49     fi
50      einfo "Loading bluetooth modules:"
51      for module in bluetooth ohci1394 hci_usb ; do
52        eindent
53        einfo "$module" ; modprobe $module ; eend $?
54        eoutdent
55      done
56
57      einfo "Scanning for human input device(s). Press the 'connect' button on the device!"
58      einfo "Scanning might take a while. Searching..."
59      SUCCESS=$(mktemp)
60      ERROR=$(mktemp)
61      if hidd --search 1>${SUCCESS} 2>${ERROR} ; then
62       if ! grep -q 'Connecting to device' $SUCCESS ; then
63          eerror "Could not find any devices. Exiting." ; eend 1
64          exit 1
65       else
66        ID=$(awk '/Connecting to device/ {print $4}' $SUCCESS)
67        if [ -n "$ID" ] ; then
68         einfo "Success: connected device ${ID}." ; eend 0
69         logger -t "bluez-connect" "connected human input device ${ID}"
70        else
71         ewarn "Warning: searching for device succeded but no connection could be established."
72        fi
73       fi
74      else
75        eerror "Error: `cat $ERROR`" ; eend 1
76      fi
77      rm -f $SUCCESS $ERROR
78     ;;
79   stop)
80     einfo "Stopping hcid."
81     killall hcid ; eend $? # workaround because start-stop-daemon does not work :-/
82     einfo "Disconnecting all human input devices."
83     logger -t "bluez-connect" "disconnected all human input devices"
84     hidd --killall ; eend $?
85     ;;
86   restart|force-reload)
87     $0 stop
88     sleep 1
89     $0 start
90     ;;
91   status)
92     local INFO="$(hidd --show)"
93     einfo "$0 - checking status:"
94     eindent
95     if pgrep hcid 1>/dev/null ; then
96       einfo "hcid running." ; eend 0
97     else
98       eerror "hcid not running." ; eend 1
99     fi
100     if [ -n "$INFO" ] ; then
101       einfo "$INFO" ; eend 0
102     else
103       eerror "No devices connected." ; eend 1
104     fi
105     eoutdent
106     ;;
107   *)
108     echo "Usage: $0 {start|stop|status|restart|force-reload}"
109     exit 1
110     ;;
111 esac
112
113 eoutdent
114
115 exit 0
116
117 ## END OF FILE #################################################################