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