Fix bashism/non-POSIX usage of $UID.
[grml-network.git] / sbin / modemlink
1 #!/bin/bash +x
2 # Filename:      modemlink
3 # Purpose:       start device drivers and set /dev/modem link in GRML
4 # Authors:       (c) Klaus Knopper Mar 2004, (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 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin"
10 export PATH
11
12 # XDIALOG_HIGH_DIALOG_COMPAT=1
13 # export XDIALOG_HIGH_DIALOG_COMPAT
14
15 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
16   echo 1>&2 "Error: please run this script with uid 0 (root)." ; exit 1
17 fi
18
19 TMP=$(mktemp)
20
21 bailout(){
22   rm -f "$TMP"
23   exit $1
24 }
25
26 DIALOG="dialog"
27 # [ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"
28
29 trap bailout 1 2 3 15
30
31 # LANGUAGE
32 [ -r /etc/default/locale ] && . /etc/default/locale
33
34 # Language-dependent Messages
35 case "$LANGUAGE" in
36 de*|at*|ch*)
37 TITLE1="Art der Verbindung"
38 MESSAGE1="Bitte die gewuenschten Verbindungsart auswaehlen:"
39 SERIAL="Serielle Schnittstelle (z.B. Modem)"
40 WINMODEM="Nach nicht-unterstuetzten \"WinModems\" suchen"
41 SIR="Standard SIR (oder USB)"
42 USB="USB Schnittstelle (z.B. Modem)"
43 IRDA="Infrarot Schnittstelle (z.B. fuer Handy/PDA)"
44 BLUETOOTH="Bluetooth Funkadapter (z.B. fuer Handy/PDA)"
45 TITLE_DONGLES="IRDA Adapter-Typ"
46 MESSAGE_DONGLES="Bitte den verwendeten IRDA-Adaptertyp auswaehlen:"
47 TITLE_TEST="Adaptertest - Strg-C zum Beenden des Tests"
48 TITLE_SRVFAIL="FEHLER"
49 MESSAGE_SRVFAIL="Konnte Daemon fuer Geraet nicht starten. Wahrscheinlich wurde die falsche Schnittstelle ausgewaehlt." 
50 AUTOSEARCH="ALLE Schnittstellen scannen"
51 TITLE_BLUES="Bluetooth-Geraete"
52 MESSAGE_BLUES="Folgende Bluetooth-Geraete befinden sich in der Umgebung. Bitte eins auswaehlen:"
53 TITLE_PIN="PIN"
54 MESSAGE_PIN="Bitte geben Sie eine 4-stellige PIN zur Authentfizierung der Bluetooth-Geraete ein:"
55 TITLE_GAUGE="Device-Scan..."
56 TITLE_LINK="/dev/modem Link erzeugen..."
57 MESSAGE_LINK="Moechten Sie den Symlink /dev/modem auf das neu konfigurierte Geraet setzen? Dies ist notwendig, wenn Sie das Geraet z.B. als Modem fuer kppp oder GPRS benutzen moechten).
58
59 Verlinken von /dev/modem -> "
60 TITLE_RETRY="FEHLER"
61 MESSAGE_RETRY="Das scheint nicht geklappt zu haben. Noch einmal versuchen?"
62 TITLE_BTID="Keine Bluetooth Geraete in Reichweite"
63 MESSAGE_BTID="Auf die Sendeanfrage hat kein Bluetooth-Geraet geantwortet. Bitte geben Sie die Adresse eines entfernten Bluetooth-Geraetes ein."
64 ;;
65 *)
66 TITLE1="Connection Type"
67 MESSAGE1="Please select type of connection:"
68 WINMODEM="Scan for unsupported \"WinModems\""
69 SERIAL="Serial connector (i.e. Modem)"
70 SIR="Standard SIR (or USB)"
71 USB="USB connector (i.e. Modem)"
72 IRDA="Infrared wireless device (i.e. cellphone/PDA)"
73 BLUETOOTH="Bluetooth wireless device (i.e. cellphone/PDA)"
74 TITLE_DONGLES="IRDA Adapter type"
75 MESSAGE_DONGLES="Please select type of IRDA adapter:"
76 TITLE_TEST="Testing adapter - Hit Ctrl-C to end test."
77 TITLE_SRVFAIL="ERROR"
78 MESSAGE_SRVFAIL="Failed to start device daemon. Probably the selected port is wrong."
79 AUTOSEARCH="scan ALL devices"
80 TITLE_BLUES="Bluetooth Devices"
81 MESSAGE_BLUES="These bluetooth devices exist nearby. Please select:"
82 TITLE_PIN="PIN"
83 MESSAGE_PIN="Please chose a 4-digit PIN number for authention of bluetooth devices:"
84 TITLE_GAUGE="Scanning devices..."
85 TITLE_LINK="Create /dev/modem link..."
86 MESSAGE_LINK="Do you want to set the symlink /dev/modem to point to the newly configured device? This is necessary if you want o use the device as modem for kppp, as an example, or for using GPRS).
87
88 Create link /dev/modem -> "
89 TITLE_RETRY="ERROR"
90 MESSAGE_RETRY="This seems to have failed. Do you want to try again?"
91 TITLE_BTID="No bluetooth devices in range"
92 MESSAGE_BTID="No bluetooth devices responded to our scan. Please enter the address of a remote bluetooth device."
93 ;;
94 esac
95
96 # Shortcut description selected
97 TYPES=(serial "$SERIAL" on \
98 usb "$USB" off \
99 irda "$IRDA" off \
100 bluetooth "$BLUETOOTH" off \
101 winmodem "$WINMODEM" off)
102
103 type scanmodem >/dev/null 2>&1 || { unset TYPES[14]; unset TYPES[13]; unset TYPES[12]; }
104 type rfcomm >/dev/null 2>&1 || { unset TYPES[11]; unset TYPES[10]; unset TYPES[9]; }
105 type irdadump >/dev/null 2>&1 || { unset TYPES[8]; unset TYPES[7]; unset TYPES[6]; }
106
107 # man irattach
108 DONGLES=(auto "$SIR" on \
109 act200l "ACTiSYS Ir-200L dongles" off \
110 actisys "ACTiSYS IR-220L dongle" off \
111 actisys+ "ACTiSYS IR-220L+ dongle" off \
112 airport "Airport dongle" off \
113 ep7211 "IR port driver for the Cirrus Logic EP7211 processor (ARM based)" off \
114 esi "Extended Systems JetEye PC ESI-9680" off \
115 girbil "Greenwich GIrBIL dongle" off \
116 litelink "Parallax LiteLink dongle & Extended Systems JetEye PC ESI-9680B" off \
117 old_belkin "Belkin (old) SmartBeam dongle or any dongle only capable of 9600 bauds" off \
118 ma600 "Mobile Action ma600 dongles" off \
119 mcp2120 "Dongles based on the MCP2120 (Microchip)" off \
120 tekram "Tekram IrMate IR-210B dongle" off)
121
122 AUTODEVICE=(auto "$AUTOSEARCH" off)
123
124 SERIALDEVICES=(ttyS0 "(COM1:)" off \
125 ttyS1 "(COM2:)" off \
126 ttyS2 "(COM3:)" off \
127 ttyS3 "(COM4:)" off \
128 ttyS4 "(COM5:)" off \
129 ttyS5 "(COM6:)" off \
130 ttyS6 "(COM7:)" off \
131 ttyS7 "(COM8:)" off \
132 ttyS8 "(COM9:)" off \
133 ttyS9 "(COM10:)" off \
134 ttyS10 "(COM11:)" off \
135 ttyS11 "(COM12:)" off \
136 ttyS12 "(COM13:)" off \
137 ttyS13 "(COM14:)" off \
138 ttyS14 "(COM15:)" off \
139 ttyS15 "(COM16:)" off)
140
141 USBDEVICES=(ttyACM0 "USB Modem 1" off \
142 ttyACM1 "USB Modem 2" off \
143 ttyACM2 "USB Modem 3" off \
144 ttyACM3 "USB Modem 4" off \
145 ttyUSB0 "USB Serial 0" off \
146 ttyUSB1 "USB Serial 1" off \
147 ttyUSB2 "USB Serial 2" off \
148 ttyUSB3 "USB Serial 3" off)
149
150 num=${#COMMANDS[@]}
151
152 #for ((i=0; i<$num; i++)); do
153 ## Remove non-existing desktops/descriptions from list
154 #d0="$(($i * 3))"
155 #d1="$(($i * 3 + 1))"
156 #d2="$(($i * 3 + 2))"
157 #if type "${COMMANDS[$i]}" >/dev/null 2>&1; then
158 #[ "$DESKTOP" = "${DESKTOPS[$(($i * 3))]}" ] && DESKTOPS[$(($i * 3 + 2))]="on" || DESKTOPS[$(($i * 3 + 2))]="off"
159 #else
160 #unset DESKTOPS[$d0]
161 #unset DESKTOPS[$d1]
162 #unset DESKTOPS[$d2]
163 #unset COMMANDS[$i]
164 #fi
165 #done
166
167 gauge(){
168 rm -f "$TMP.done"
169 status=0
170 while [ ! -e "$TMP.done" ]; do echo "$status" ; status="`expr \( 100 - $status \) / 4 + $status`"; sleep 2; done | $DIALOG --title "$TITLE_GAUGE" --gauge "$1" 8 75 0
171 }
172
173 # Stop status bar
174 killgauge(){
175 touch "$TMP.done" ; wait ; rm -f "$TMP.done"
176 }
177
178 conntype(){
179 rm -f "$TMP"
180 $DIALOG --clear --title "$TITLE1" --radiolist "$MESSAGE1" 18 75 9 "${TYPES[@]}" 2>"$TMP" || bailout 1
181 read TYPE <"$TMP"
182 TYPE="${TYPE#\"}"; TYPE="${TYPE%\"}"
183 rm -f "$TMP"
184 return 0
185 }
186
187 startservice(){
188 case "$TYPE" in
189 serial)
190 rm -f "$TMP"
191 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${SERIALDEVICES[@]}" 2>"$TMP" || bailout 1
192 read DEVICE <"$TMP"
193 DEVICE="${DEVICE#\"}"; DEVICE="${DEVICE%\"}"
194 rm -f "$TMP"
195 ;;
196 winmodem)
197 [ -n "$DISPLAY" ] && xterm -e bash -c "scanmodem; read -p 'Enter to quit.'" || { scanmodem; read -p 'Enter to quit.'; }
198 return 1
199 ;;
200 usb)
201 rm -f "$TMP"
202 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${USBDEVICES[@]}" 2>"$TMP" || bailout 1
203 read DEVICE <"$TMP"
204 DEVICE="${DEVICE#\"}"; DEVICE="${DEVICE%\"}"
205 rm -f "$TMP"
206 ;;
207 irda)
208 killall irattach 2>/dev/null
209 modprobe irda 2>/dev/null
210 modprobe ircomm-tty 2>/dev/null
211 rm -f "$TMP"
212 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${AUTODEVICE[@]}" "${SERIALDEVICES[@]}" "${USBDEVICES[@]}" 2>"$TMP" || bailout 1
213 read RAWDEVICE <"$TMP"
214 RAWDEVICE="${RAWDEVICE#\"}"; RAWDEVICE="${RAWDEVICE%\"}"
215 rm -f "$TMP"
216 $DIALOG --clear --title "$TITLE_DONGLES" --radiolist "$MESSAGE_DONGLES" 18 75 9 "${DONGLES[@]}" 2>"$TMP" || bailout 1
217 read DONGLE <"$TMP"
218 DONGLE="${DONGLE#\"}"; DONGLE="${DONGLE%\"}"
219 rm -f "$TMP"
220 case "$DONGLE" in auto) DONGLE="" ;; *) DONGLE="-d $DONGLE" ;; esac
221 if [ "$RAWDEVICE" = "auto" ]; then
222 gauge "Device-Scan..." &
223 for i in /dev/ttyUSB* /dev/ttyS*; do
224 RAWDEVICE="${i##/dev/}"
225 irattach "/dev/$RAWDEVICE" $DONGLE -s
226 usleep 125000
227 done
228 killgauge
229 sleep 2
230 RAWDEVICE="$(ps auxw | awk '/irattach/{print $12}' | head -1)"
231 RAWDEVICE="${RAWDEVICE##/dev/}"
232 else
233 irattach "/dev/$RAWDEVICE" $DONGLE -s
234 sleep 2
235 fi
236 if ifconfig irda0 >/dev/null 2>&1; then
237 true
238 else
239 $DIALOG --clear --title "$TITLE_SRVFAIL" --msgbox "$MESSAGE_SRVFAIL" 18 75
240 DEVICE=""
241 return 1
242 fi
243 DEVICE="ircomm0"
244 rm -f /etc/irda.conf
245 echo "#irda.conf Version: 1.0
246 IRDADEV=/dev/$RAWDEVICE" >/etc/irda.conf
247 [ -n "$DONGLE" ] && echo "DONGLE=\"${DONGLE##-d }\"" >>/etc/irda.conf
248 echo "DISCOVERY=-s
249 ENABLE=yes" >>/etc/irda.conf
250 [ -x /etc/init.d/irda ] && for i in 2 3 5; do ln -sf /etc/init.d/irda /etc/rc$i.d/S99irda; done
251 ;;
252 bluetooth)
253 killall hcid 2>/dev/null
254 killall rfcomm 2>/dev/null
255 hcid || return 1
256 count=0
257 gauge "Device-Scan..." &
258 while read HW NAME; do
259 d0="$(($count * 3))"
260 d1="$(($count * 3 + 1))"
261 d2="$(($count * 3 + 2))"
262 BLUES[$d0]="$HW"; BLUES[$d1]="$NAME"; BLUES[$d2]="off"
263 count=$(($count + 1))
264 done <<EOT
265 $(hcitool scan 2>/dev/null | awk '/[0-9]:[0-9]/{print $0}')
266 EOT
267 killgauge
268 if [ -n "${BLUES[0]}" ]; then
269 $DIALOG --clear --title "$TITLE_BLUES" --radiolist "$MESSAGE_BLUES" 18 75 9 "${BLUES[@]}" 2>"$TMP" || bailout 1
270 else
271 $DIALOG --title "$TITLE_BTID" --inputbox "$MESSAGE_BTID" 15 60 "00:00:00:00:00:00" 2>"$TMP" || bailout 1
272 fi
273 read RAWDEVICE <"$TMP"
274 RAWDEVICE="${RAWDEVICE#\"}"; RAWDEVICE="${RAWDEVICE%\"}"
275 rm -f "$TMP"
276 read PIN < /etc/bluetooth/pin  2>/dev/null
277 rm -f "$TMP"
278 if [ -n "$DISPLAY" ]; then
279 $DIALOG --title "$TITLE_PIN" --inputbox "$MESSAGE_PIN" 8 65 "$PIN" 2>"$TMP" || bailout 1
280 fi
281 read PIN <"$TMP" 2>/dev/null
282 rm -f "$TMP"
283 rm -f /etc/bluetooth/pin
284 echo "$PIN" >/etc/bluetooth/pin
285 rm -f /etc/bluetooth/rfcomm.conf
286 cat >/etc/bluetooth/rfcomm.conf <<EOT
287 #
288 # RFCOMM configuration file.
289 #
290 # Created by modemlink-grml on $(date)
291 #
292
293 rfcomm0 {
294  bind yes;
295 # Bluetooth address of the device
296  device $RAWDEVICE;
297 # RFCOMM channel for the connection
298 #       channel 1;
299 # Description of the connection
300  comment "Selected Bluetooth device";
301 }
302 EOT
303 rfcomm bind all
304 [ -x /etc/init.d/bluez-utils ] && for i in 2 3 5; do ln -sf /etc/init.d/bluez-utils /etc/rc$i.d/S99bluez-utils; done
305 DEVICE=rfcomm0
306 ;;
307 esac
308 return 0
309 }
310
311 # MAIN
312
313 while true; do
314 conntype
315 startservice
316 if [ "$?" = "0" -a -n "$DEVICE" ]; then
317 $DIALOG --title "$TITLE_LINK" --yesno "$MESSAGE_LINK $DEVICE ?" 15 65 || bailout 1
318 rm -f /dev/modem
319 ln -sf /dev/"$DEVICE" /dev/modem
320 break
321 else
322 $DIALOG --clear --title "$TITLE_RETRY" --yesno "$MESSAGE_RETRY" 18 75 || bailout 1
323 fi
324 done
325
326 bailout 0
327
328 ## END OF FILE #################################################################