xpp: migrate everything to libxtalk
[dahdi/tools.git] / dahdi_waitfor_span_assignments
1 #! /bin/sh
2
3 usage() {
4         echo >&2 "Usage: $0 {assigned|unassigned}"
5         echo >&2 "# wait until all spans known are assigned/unassigned"
6         exit 1
7 }
8
9 TIMEOUT=5       # How much time to wait for spans
10
11 if [ "$#" -lt 1 ]; then
12         usage
13 fi
14 wanted_event="$1"
15 shift
16
17 case "$wanted_event" in
18 assigned)
19         ;;
20 unassigned)
21         ;;
22 *)
23         usage
24         ;;
25 esac
26
27 devbase='/sys/bus/dahdi_devices/devices'
28
29 spans_of() {
30         dev="$1"
31         wc -l < "$dev/spantype"
32 }
33
34 assigned_spans_of() {
35         dev="$1"
36         ls -d "$dev/span-"* 2>/dev/null | wc -l
37 }
38
39
40 waitfor_span_assignments() {
41         wanted_state="$1"
42         device_list=`ls -d "$devbase/"* 2> /dev/null`
43         finished=''
44         count="$TIMEOUT"
45         echo -n "Waiting for spans to become $wanted_state: "
46         while [ "$count" -gt 0 ]; do
47                 finished='yes'
48                 for dev in $device_list
49                 do
50                         spans=`spans_of "$dev"`
51                         assigned_spans=`assigned_spans_of "$dev"`
52                         if [ "$wanted_state" = 'assigned' -a "$assigned_spans" -ne "$spans" ]; then
53                                 finished='no'
54                         elif [ "$wanted_state" = 'unassigned' -a "$assigned_spans" -ne 0 ]; then
55                                 finished='no'
56                         fi
57                 done
58                 if [ "$finished" = 'yes' ]; then
59                         break
60                 else
61                         sleep 1
62                         echo -n "."
63                 fi
64                 count=`expr "$count" - 1`
65         done
66         if [ "$finished" = 'yes' ]; then
67                 echo "done"
68         else
69                 echo "timeout"
70         fi
71 }
72
73 waitfor_span_assignments "$wanted_event"