amiro-apps / middleware / middlewaresetup.sh @ deb9d7ea
History | View | Annotate | Download (12.156 KB)
1 | b1f38b27 | Thomas Schöpping | ################################################################################ |
---|---|---|---|
2 | # AMiRo-Apps is a collection of applications for the Autonomous Mini Robot # |
||
3 | # (AMiRo) platform. # |
||
4 | 6d4ba740 | Thomas Schöpping | # Copyright (C) 2018..2020 Thomas Schöpping et al. # |
5 | b1f38b27 | Thomas Schöpping | # # |
6 | # This program is free software: you can redistribute it and/or modify # |
||
7 | # it under the terms of the GNU General Public License as published by # |
||
8 | # the Free Software Foundation, either version 3 of the License, or # |
||
9 | # (at your option) any later version. # |
||
10 | # # |
||
11 | # This program is distributed in the hope that it will be useful, # |
||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
||
14 | # GNU General Public License for more details. # |
||
15 | # # |
||
16 | # You should have received a copy of the GNU General Public License # |
||
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # |
||
18 | # # |
||
19 | # This research/work was supported by the Cluster of Excellence Cognitive # |
||
20 | # Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is # |
||
21 | # funded by the German Research Foundation (DFG). # |
||
22 | ################################################################################ |
||
23 | |||
24 | #!/bin/bash |
||
25 | |||
26 | 8dab1768 | Thomas Schöpping | # load library |
27 | source "$(dirname ${BASH_SOURCE[0]})/../tools/bash/setuplib.sh" |
||
28 | b1f38b27 | Thomas Schöpping | |
29 | ### print welcome text ######################################################### |
||
30 | # Prints a welcome message to standard out. |
||
31 | # |
||
32 | # usage: printWelcomeText |
||
33 | # arguments: n/a |
||
34 | # return: n/a |
||
35 | # |
||
36 | function printWelcomeText { |
||
37 | printf "######################################################################\n" |
||
38 | printf "# #\n" |
||
39 | printf "# Welcome to the uRtWare submodule setup! #\n" |
||
40 | printf "# #\n" |
||
41 | printf "######################################################################\n" |
||
42 | printf "# #\n" |
||
43 | 6d4ba740 | Thomas Schöpping | printf "# Copyright (c) 2018..2020 Thomas Schöpping #\n" |
44 | b1f38b27 | Thomas Schöpping | printf "# #\n" |
45 | printf "# This is free software; see the source for copying conditions. #\n" |
||
46 | printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n" |
||
47 | printf "# A PARTICULAR PURPOSE. The development of this software was #\n" |
||
48 | printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction #\n" |
||
49 | printf "# Technology. The Excellence Cluster EXC 227 is a grant of the #\n" |
||
50 | printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n" |
||
51 | printf "# Excellence Initiative. #\n" |
||
52 | printf "# #\n" |
||
53 | printf "######################################################################\n" |
||
54 | } |
||
55 | |||
56 | ### print help ################################################################# |
||
57 | # Prints a help text to standard out. |
||
58 | # |
||
59 | # usage: printHelp |
||
60 | # arguments: n/a |
||
61 | # return: n/a |
||
62 | # |
||
63 | function printHelp { |
||
64 | printInfo "printing help text\n" |
||
65 | printf "usage: $(basename ${BASH_SOURCE[0]}) [-h|--help] [-i|--init] [-s|--setup] [-w|--wipe] [-q|--quit] [--log=<file>]\n" |
||
66 | printf "options: -h, --help\n" |
||
67 | printf " Print this help text.\n" |
||
68 | printf " -i, --init\n" |
||
69 | printf " Initialize the uRtWare submodule.\n" |
||
70 | printf " -w, --wipe\n" |
||
71 | printf " Wipe the entire uRtWare submodule.\n" |
||
72 | printf " -q, --quit\n" |
||
73 | printf " Quit the script.\n" |
||
74 | printf " --log=<file> \n" |
||
75 | printf " Specify a log file.\n" |
||
76 | } |
||
77 | |||
78 | ### initialize uRtWare submodule ############################################### |
||
79 | # Initialize the uRtWare submodule. |
||
80 | # |
||
81 | # usage: initUrtware |
||
82 | # arguments: n/a |
||
83 | # return: 0 |
||
84 | # No error or warning occurred. |
||
85 | # 1 |
||
86 | # Warning: Arborted by user. |
||
87 | # -1 |
||
88 | # Error: Unexpected user input. |
||
89 | 0136e0ec | Thomas Schöpping | # -2 |
90 | # Error: Missing dependencies. |
||
91 | b1f38b27 | Thomas Schöpping | # |
92 | function initUrtware { |
||
93 | printInfo "initializing uRtWare submodule...\n" |
||
94 | local userdir=$(pwd) |
||
95 | local middlewaredir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
||
96 | local urtwaredir=${middlewaredir}/uRtWare |
||
97 | |||
98 | # if the uRtWare folder is not empty |
||
99 | if [ ! -z "$(ls -A $urtwaredir)" ]; then |
||
100 | printWarning "$(realpath $urtwaredir) is not empty. Delete and reinitialize? [y/n]\n" |
||
101 | local userinput="" |
||
102 | readUserInput "YyNn" userinput |
||
103 | case $userinput in |
||
104 | Y|y) |
||
105 | wipeUrtware |
||
106 | ;; |
||
107 | N|n) |
||
108 | printWarning "uRtWare initialization aborted by user\n" |
||
109 | return 1 |
||
110 | ;; |
||
111 | *) # sanity check (return error) |
||
112 | printError "unexpected input: $userinput\n"; return -1;; |
||
113 | esac |
||
114 | fi |
||
115 | |||
116 | 0136e0ec | Thomas Schöpping | # check dependencies |
117 | checkCommands git |
||
118 | if [ $? -ne 0 ]; then |
||
119 | printError "Missing dependencies detected.\n" |
||
120 | return -2 |
||
121 | fi |
||
122 | |||
123 | b1f38b27 | Thomas Schöpping | # initialize submodule to default branch |
124 | cd $middlewaredir |
||
125 | git submodule update --init $urtwaredir 2>&1 | tee -a $LOG_FILE |
||
126 | while [ ${PIPESTATUS[0]} -ne 0 ]; do |
||
127 | printWarning "initialitaion failed. Retry? [y/n]\n" |
||
128 | local userinput="" |
||
129 | readUserInput "YyNn" userinput |
||
130 | case "$userinput" in |
||
131 | Y|y) |
||
132 | git submodule update --init $urtwaredir 2>&1 | tee -a $LOG_FILE;; |
||
133 | N|n) |
||
134 | printWarning "uRtWare initialization aborted by user\n" |
||
135 | cd $userdir |
||
136 | return 1 |
||
137 | ;; |
||
138 | *) # sanity check (return error) |
||
139 | printError "unexpected input: $userinput\n"; return -1;; |
||
140 | esac |
||
141 | done |
||
142 | cd $userdir |
||
143 | |||
144 | return 0 |
||
145 | } |
||
146 | |||
147 | ### reset uRtWare submodule and wipe directory #################################### |
||
148 | # Resets the uRtWare Git submodule and wipes the directory. |
||
149 | # |
||
150 | # usage: wipeUrtware |
||
151 | # arguments: n/a |
||
152 | 0136e0ec | Thomas Schöpping | # return: 0 |
153 | # No error or warning occurred. |
||
154 | # 1 |
||
155 | # Warning: uRtWare Git submodule already empty |
||
156 | # 2 |
||
157 | # Warning: Aborted by user. |
||
158 | # -1 |
||
159 | # Error: Unexpected input occurred |
||
160 | # -2 |
||
161 | # Error: Missing dependencies. |
||
162 | # |
||
163 | b1f38b27 | Thomas Schöpping | function wipeUrtware { |
164 | local userdir=$(pwd) |
||
165 | local middlewaredir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
||
166 | local urtwaredir=${middlewaredir}/uRtWare |
||
167 | printInfo "reset and wipe Git submodule $urtwaredir\n" |
||
168 | |||
169 | 0136e0ec | Thomas Schöpping | # check dependencies |
170 | checkCommands git |
||
171 | if [ $? -ne 0 ]; then |
||
172 | printError "Missing dependencies detected.\n" |
||
173 | return -2 |
||
174 | fi |
||
175 | |||
176 | b1f38b27 | Thomas Schöpping | # if the uRtWare folder is empty |
177 | if [ -z "$(ls -A $urtwaredir)" ]; then |
||
178 | printWarning "$urtwaredir is already empty\n" |
||
179 | return 1 |
||
180 | else |
||
181 | # get some information from Git |
||
182 | cd $middlewaredir |
||
183 | local git_basehash=($(git ls-tree -d HEAD $urtwaredir)); git_basehash=${git_basehash[2]} |
||
184 | cd $urtwaredir |
||
185 | local git_branch_current=$(git rev-parse --abbrev-ref HEAD) |
||
186 | local git_difftobase="$(git diff ${git_basehash}..HEAD)" |
||
187 | local git_commits=$(git log --format=oneline ${git_basehash}..HEAD) |
||
188 | local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc)) |
||
189 | cd $userdir |
||
190 | local issues=0 |
||
191 | # if HEAD is ahead of submodule base commit |
||
192 | if [ -n "$git_difftobase" ]; then |
||
193 | issues=$((issues + 1)) |
||
194 | printWarning "HEAD is ahead of submodule base\n" |
||
195 | fi |
||
196 | # if there are untracked, modified, or deleted files |
||
197 | if [ ${#git_dirtyfiles[@]} != 0 ]; then |
||
198 | issues=$((issues + 1)) |
||
199 | printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n" |
||
200 | fi |
||
201 | if [ $issues -gt 0 ]; then |
||
202 | local userinput="" |
||
203 | printWarning "$issues issues detected. Do you want to continue? [y/n]\n" |
||
204 | readUserInput "YyNn" userinput |
||
205 | case "$userinput" in |
||
206 | Y|y) |
||
207 | ;; |
||
208 | N|n) |
||
209 | printfWarning "wiping uRtWare Git submodule aborted by user\n" |
||
210 | return 2 |
||
211 | ;; |
||
212 | *) # sanity check (return error) |
||
213 | printError "unexpected input: $userinput\n"; return -1;; |
||
214 | esac |
||
215 | fi |
||
216 | |||
217 | # checkout base commit and delete all local branches |
||
218 | cd $middlewaredir |
||
219 | git submodule update --force --checkout $urtwaredir | tee -a $LOG_FILE |
||
220 | cd $urtwaredir |
||
221 | local git_branches=($(git for-each-ref --format="%(refname)")) |
||
222 | for branch in $git_branches; do |
||
223 | if [[ $branch = *"heads/"* ]]; then |
||
224 | git branch -D ${branch##*/} | tee -a $LOG_FILE |
||
225 | fi |
||
226 | done |
||
227 | cd $userdir |
||
228 | |||
229 | # deinitialize uRtWare submodule and delete any remaining files |
||
230 | cd $middlewaredir |
||
231 | git submodule deinit -f $urtwaredir 2>&1 | tee -a $LOG_FILE |
||
232 | rm -rf $urtwaredir/* |
||
233 | cd $userdir |
||
234 | |||
235 | return 0 |
||
236 | fi |
||
237 | } |
||
238 | |||
239 | ### main function of this script ############################################### |
||
240 | # Initializes or wipes the uRtWare Git submodule, and provides an entry point to |
||
241 | # its setup. |
||
242 | # |
||
243 | # usage: see function printHelp |
||
244 | # arguments: see function printHelp |
||
245 | # return: 0 |
||
246 | # No error or warning occurred. |
||
247 | # |
||
248 | function main { |
||
249 | # print welcome/info text if not suppressed |
||
250 | if [[ $@ != *"--noinfo"* ]]; then |
||
251 | printWelcomeText |
||
252 | else |
||
253 | printf "######################################################################\n" |
||
254 | fi |
||
255 | printf "\n" |
||
256 | |||
257 | # if --help or -h was specified, print the help text and exit |
||
258 | if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
||
259 | printHelp |
||
260 | printf "\n" |
||
261 | quitScript |
||
262 | fi |
||
263 | |||
264 | # set log file if specified |
||
265 | if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then |
||
266 | # get the parameter (file name) |
||
267 | local cmdidx=1 |
||
268 | while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do |
||
269 | cmdidx=$[cmdidx + 1] |
||
270 | done |
||
271 | local cmd="${!cmdidx}" |
||
272 | local logfile="" |
||
273 | if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then |
||
274 | logfile=${cmd#*=} |
||
275 | else |
||
276 | local filenameidx=$((cmdidx + 1)) |
||
277 | logfile="${!filenameidx}" |
||
278 | fi |
||
279 | # optionally force silent appending |
||
280 | if [[ "$cmd" = "--LOG"* ]]; then |
||
281 | setLogFile --option=c --quiet "$logfile" LOG_FILE |
||
282 | else |
||
283 | setLogFile "$logfile" LOG_FILE |
||
284 | printf "\n" |
||
285 | fi |
||
286 | fi |
||
287 | # log script name |
||
288 | printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
||
289 | |||
290 | # parse arguments |
||
291 | local otherargs=() |
||
292 | while [ $# -gt 0 ]; do |
||
293 | if ( parseIsOption $1 ); then |
||
294 | case "$1" in |
||
295 | -h|--help) # already handled; ignore |
||
296 | shift 1;; |
||
297 | -i|--init) |
||
298 | initUrtware; printf "\n"; shift 1;; |
||
299 | -w|--wipe) |
||
300 | wipeUrtware; printf "\n"; shift 1;; |
||
301 | -q|--quit) |
||
302 | quitScript; shift 1;; |
||
303 | --log=*|--LOG=*) # already handled; ignore |
||
304 | shift 1;; |
||
305 | --log|--LOG) # already handled; ignore |
||
306 | shift 2;; |
||
307 | --noinfo) # already handled; ignore |
||
308 | shift 1;; |
||
309 | *) |
||
310 | printError "invalid option: $1\n"; shift 1;; |
||
311 | esac |
||
312 | else |
||
313 | otherargs+=("$1") |
||
314 | shift 1 |
||
315 | fi |
||
316 | done |
||
317 | |||
318 | # interactive menu |
||
319 | while ( true ); do |
||
320 | # main menu info prompt and selection |
||
321 | printInfo "Operating system setup main menu\n" |
||
322 | printf "Please select one of the following actions:\n" |
||
323 | printf " [I] - initialize uRtWare submodule\n" |
||
324 | printf " [W] - wipe uRtWare submodule\n" |
||
325 | printf " [Q] - quit this setup\n" |
||
326 | local userinput="" |
||
327 | readUserInput "IiWwQq" userinput |
||
328 | printf "\n" |
||
329 | |||
330 | # evaluate user selection |
||
331 | case "$userinput" in |
||
332 | I|i) |
||
333 | initUrtware; printf "\n";; |
||
334 | W|w) |
||
335 | wipeUrtware; printf "\n";; |
||
336 | Q|q) |
||
337 | quitScript;; |
||
338 | *) # sanity check (exit with error) |
||
339 | printError "unexpected argument: $userinput\n";; |
||
340 | esac |
||
341 | done |
||
342 | |||
343 | exit 0 |
||
344 | } |
||
345 | |||
346 | ################################################################################ |
||
347 | # SCRIPT ENTRY POINT # |
||
348 | ################################################################################ |
||
349 | |||
350 | main "$@" |