forked from ericmandel/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs9load
executable file
·129 lines (116 loc) · 2.53 KB
/
js9load
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# set -x
# in case the user has changed the js9 script, try to figure out the right one
# is this file a link?
BFILE="${BASH_SOURCE[0]}"
LFILE="$( readlink $BFILE )"
if [ x"$LFILE" != x ]; then
XFILE="$LFILE"
else
XFILE="$BFILE"
fi
# is the containing directory a link?
BDIR="$( dirname ${XFILE} )"
LDIR="$( readlink $BDIR )"
if [ x"$LDIR" != x ]; then
XDIR="$LDIR"
else
XDIR="$BDIR"
fi
# any more links we need to know about?
DIR="$( cd "${XDIR}" >/dev/null 2>&1 && pwd )"
if [ -r ${DIR}/js9 ]; then
JS9=${DIR}/js9
else
JS9=js9
fi
error() {
echo "ERROR: $1" 1>&2
exit 1
}
# local variables
DONE=false
VERBOSE=false
MAXTRIES=10
ETRIES=3
SLEEP=1
while [ x"$1" != x ]; do
case $1 in
-h) shift
XARGS="$XARGS -h $1"
shift;;
-id|--id) shift
XARGS="$XARGS --id $1"
shift;;
-s) shift
SLEEP=$1
shift;;
-t) shift
MAXTRIES=$1
shift;;
-v) VERBOSE=true
shift;;
*) break;;
esac
done
# check for required args
if [ $# -lt 1 ]; then
echo "usage: $0 filename"
exit 1
else
PATHNAME="$1"
shift
fi
# start the image load
GOT=`${JS9} $XARGS Load "$PATHNAME" $* `
if [ x"$GOT" != x ]; then
error "$GOT"
fi
# wait for completion
TRIES=$MAXTRIES
while [ $DONE = false ]; do
# get status of current image
GOT=`${JS9} $XARGS GetLoadStatus "$PATHNAME"`
case $GOT in
error)
error "could not load: $PATHNAME";;
loading|please|other|none)
# allow none once before error (might not have loaded enough image yet)
if [ $GOT = none -a $TRIES != $MAXTRIES ]; then
error "could not find: $PATHNAME"
fi
TRIES=`echo "$TRIES - 1" | bc`
if [ $TRIES -le 0 ]; then
error "timeout while loading: $PATHNAME"
fi
sleep $SLEEP
if [ x$VERBOSE = xtrue ] ; then
echo "loading [$TRIES] ..."
fi
continue;;
complete)
if [ x$VERBOSE = xtrue ] ; then
echo "success!"
fi
DONE=true
continue;;
ERROR*)
# look for race condition if js9 display is not ready
echo "$GOT" | egrep "can't find JS9 display with id" 1>/dev/null 2>&1
if [ $? = 0 ] ; then
ETRIES=`echo "$ETRIES - 1" | bc`
if [ $ETRIES -le 0 ]; then
error "timeout awaiting display, or missing file: $PATHNAME"
fi
sleep $SLEEP
if [ x$VERBOSE = xtrue ] ; then
echo "waiting for display [$ETRIES] ..."
fi
else
error "$GOT; could not load: $PATHNAME"
fi
continue;;
esac
done
# signal success
exit 0