-
Notifications
You must be signed in to change notification settings - Fork 15
/
run
executable file
·183 lines (160 loc) · 4.33 KB
/
run
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env bash
# This file might be not executable as is (for example when run in Termux on
# Android). This file is best run as: bash run
# (after changing into this ulnoiot directory)
#
# At first start it installs the local ulnoiot-environment
#
# Author: ulno
# Create date: 2017-05-17
#
# if parameters are given and the first is "exec", ulnoiot will execute
# the rest of the parameters as command in ulnoiot environment
HELP_PATH=doc/node_help
# TODO: adjust path to different selected micro controllers depending on node
function welcome() {
cat << EOF
ulnoiot
=======
Your friendly and affordable Internet of Things environment.
More at http://github.com/ulno/ulnoiot
EOF
}
function usage() {
welcome
cat << EOF
Syntax: ulnoiot [command command_parameters]
The following commands are possible:
- exec [cmd+arguments]:
Execute cmd inside the ulnoiot environment. Check out all the helper scripts
in ulnoiot/bin
- upgrade:
Update current version ulnoiot with newest version from git repository.
- welcome:
Print the welcome message (again).
- help [cmd]:
Display help on a specific command given in cmd. If cmd is empty,
show this help.
After entering the ulnoiot environment, this help is also available just via
typing uhelp.
If no command is given, execute the bash shell in the ulnoiot environment.
The first time this is run, external dependencies are downloaded and
installed here too.
cmd in help [cmd] can be any of the following:
EOF
comma=""
ls "$ULNOIOT_ROOT/$HELP_PATH/"*.txt | while read f; do
f=$(basename "$f")
if [[ ${f:0:1} != "_" ]]; then
echo -n $comma${f:0:-4}
fi
comma=", "
done
echo
echo
}
function check_files() {
for file in "$@"; do
if [ ! -e "$file" ]; then
echo "Can't find $file."
return 1
fi
done
return 0
}
function check_root() {
if [ -e "$ULNOIOT_ROOT/lib/shell_starter/ulnoiot.bash" ]; then
cd "$ULNOIOT_ROOT"
fi
# else we should assume that we were started from the root-directory
#echo -n "Checking now that we are in the ulnoiot root directory. "
check_files "lib/shell_starter/ulnoiot.bash" \
"bin/shebang_run" \
"etc/ulnoiot.conf.example"
if [ $? -ne 0 ]; then
echo
echo "Cannot find ulnoiot files."
echo "Stopping now."
return 1
fi
# Yes, found them.
echo
return 0
}
function check_install() {
# Check if all local directories and paths have been set up
check_files .local/bin/shebang_run .local/vp/bin/activate
return $?
}
function install() {
echo "Installing now."
echo
bash "$ULNOIOT_ROOT/bin/install"
}
function help() {
local m
local p
local l
p="$ULNOIOT_ROOT/$HELP_PATH/$1.txt"
{ cat "$p"; echo; } | while read l; do
if [[ "$l" = "!!"* ]]; then
if [[ "$l" =~ '!!help("'(.*)'")' ]]; then
m="${BASH_REMATCH[1]}"
help "$m"
else
echo "[[Cannot parse (call help on micro controller prompt:"
echo " ${l:2}]]"
fi
else
echo "$l"
fi
done
}
function run() {
case "$1" in
welcome)
welcome
;;
help | -h | --help)
shift
if [[ "$1" ]]; then
help "$1"
else
usage
fi
;;
upgrade)
cd "$ULNOIOT_ROOT"
echo "Upgrading ulnoiot."
echo "Getting changes from git."
git pull
fix_bin # prints out Fixing links
echo "Downloading latest firmware."
download_firmware
echo "Upgrade finished."
;;
exec | x | run | -x | -c | --exec)
shift
exec "$@"
;;
*)
welcome
exec bash
esac
}
######## Main #########
current_path="$(pwd)" # save to restore later
if check_root; then
export ULNOIOT_ROOT=$(realpath "$(pwd)")
source "$ULNOIOT_ROOT/lib/shell_starter/ulnoiot.bash" \
|| { echo "Can't read configuration. Aborting."; exit 1; }
check_install
cd "$current_path" # back to where we have been called
if [ $? -eq 0 ]; then
run "$@"
else
if install; then # if it failed, try to install first
run "$@"
fi
fi
fi