amiro-apps / os / ossetup.sh @ 557c9c87
History | View | Annotate | Download (13.129 KB)
1 |
################################################################################ |
---|---|
2 |
# AMiRo-Apps is a collection of applications for the Autonomous Mini Robot # |
3 |
# (AMiRo) platform. # |
4 |
# Copyright (C) 2018..2020 Thomas Schöpping et al. # |
5 |
# # |
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 |
# load library |
27 |
source "$(dirname ${BASH_SOURCE[0]})/../tools/bash/setuplib.sh" |
28 |
|
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 AMiRo-OS submodule setup! #\n" |
40 |
printf "# #\n" |
41 |
printf "######################################################################\n" |
42 |
printf "# #\n" |
43 |
printf "# Copyright (c) 2018..2020 Thomas Schöpping #\n" |
44 |
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 AMiRo-OS submodule.\n" |
70 |
printf " -s, --setup\n" |
71 |
printf " Enter AMiRo-OS setup.\n" |
72 |
printf " -w, --wipe\n" |
73 |
printf " Wipe the entire AMiRo-OS submodule.\n" |
74 |
printf " -q, --quit\n" |
75 |
printf " Quit the script.\n" |
76 |
printf " --log=<file> \n" |
77 |
printf " Specify a log file.\n" |
78 |
} |
79 |
|
80 |
### initialize AMiRo-OS submodule ############################################## |
81 |
# Initialize the AMiRo-OS submodule. |
82 |
# |
83 |
# usage: initAmiroOs |
84 |
# arguments: n/a |
85 |
# return: 0 |
86 |
# No error or warning occurred. |
87 |
# 1 |
88 |
# Warning: Arborted by user. |
89 |
# -1 |
90 |
# Error: Unexpected user input. |
91 |
# -2 |
92 |
# Error: Missing dependencies. |
93 |
# |
94 |
function initAmiroOs { |
95 |
printInfo "initializing AMiRo-OS submodule...\n" |
96 |
local userdir=$(pwd) |
97 |
local osdir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
98 |
local amiroosdir=${osdir}/AMiRo-OS |
99 |
|
100 |
# if the AMiRo-OS folder is not empty |
101 |
if [ ! -z "$(ls -A $amiroosdir)" ]; then |
102 |
printWarning "$(realpath $amiroosdir) is not empty. Delete and reinitialize? [y/n]\n" |
103 |
local userinput="" |
104 |
readUserInput "YyNn" userinput |
105 |
case $userinput in |
106 |
Y|y) |
107 |
wipeAmiroOs |
108 |
;; |
109 |
N|n) |
110 |
printWarning "AMiRo-OS initialization aborted by user\n" |
111 |
return 1 |
112 |
;; |
113 |
*) # sanity check (return error) |
114 |
printError "unexpected input: $userinput\n"; return -1;; |
115 |
esac |
116 |
fi |
117 |
|
118 |
# check dependencies |
119 |
checkCommands git |
120 |
if [ $? -ne 0 ]; then |
121 |
printError "Missing dependencies detected.\n" |
122 |
return -2 |
123 |
fi |
124 |
|
125 |
# initialize submodule to default branch |
126 |
cd $osdir |
127 |
git submodule update --init $amiroosdir 2>&1 | tee -a $LOG_FILE |
128 |
while [ ${PIPESTATUS[0]} -ne 0 ]; do |
129 |
printWarning "initialitaion failed. Retry? [y/n]\n" |
130 |
local userinput="" |
131 |
readUserInput "YyNn" userinput |
132 |
case "$userinput" in |
133 |
Y|y) |
134 |
git submodule update --init $amiroosdir 2>&1 | tee -a $LOG_FILE;; |
135 |
N|n) |
136 |
printWarning "AMiRo-OS initialization aborted by user\n" |
137 |
cd $userdir |
138 |
return 1 |
139 |
;; |
140 |
*) # sanity check (return error) |
141 |
printError "unexpected input: $userinput\n"; return -1;; |
142 |
esac |
143 |
done |
144 |
cd $userdir |
145 |
|
146 |
return 0 |
147 |
} |
148 |
|
149 |
### enter AMiRo-OS setup ####################################################### |
150 |
# Enter AMiRo-OS submodule setup. |
151 |
# |
152 |
# usage: amiroOsSetup |
153 |
# arguments: n/a |
154 |
# return: 0 |
155 |
# No error or warning occurred. |
156 |
# -1 |
157 |
# Error: AMiRo-OS submodule not initialized yet. |
158 |
# |
159 |
function amiroOsSetup { |
160 |
local amiroosdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/AMiRo-OS |
161 |
|
162 |
# check if AMiRo-OS submodule is initialized |
163 |
if [ -z "$(ls -A $amiroosdir)" ]; then |
164 |
printError "$amiroosdir is empty. Please initialize first.\n" |
165 |
return -1 |
166 |
else |
167 |
printInfo "entering AMiRo-OS setup\n" |
168 |
printf "\n" |
169 |
if [ -z "$LOG_FILE" ]; then |
170 |
${amiroosdir}/setup.sh --noinfo |
171 |
else |
172 |
${amiroosdir}/setup.sh --LOG="$LOG_FILE" --noinfo |
173 |
fi |
174 |
return 0 |
175 |
fi |
176 |
} |
177 |
|
178 |
### reset AMiRo-OS submodule and wipe directory ################################ |
179 |
# Resets the AMiRo-OS Git submodule and wipes the directory. |
180 |
# |
181 |
# usage: wipeAmiroOs |
182 |
# arguments: n/a |
183 |
# return: 0 |
184 |
# No error or warning occurred. |
185 |
# 1 |
186 |
# Warning: AMiRo-OS Git submodule already empty. |
187 |
# 2 |
188 |
# Warning: Aborted by user. |
189 |
# -1 |
190 |
# Error: Unexpected input occurred |
191 |
# -2 |
192 |
# Error: Missing dependencies. |
193 |
# |
194 |
function wipeAmiroOs { |
195 |
printInfo "reset and wipe Git submodule $amiroosdir\n" |
196 |
local userdir=$(pwd) |
197 |
local osdir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
198 |
local amiroosdir=${osdir}/AMiRo-OS |
199 |
|
200 |
# check dependencies |
201 |
checkCommands git |
202 |
if [ $? -ne 0 ]; then |
203 |
printError "Missing dependencies detected.\n" |
204 |
return -2 |
205 |
fi |
206 |
|
207 |
# if the AMiRo-OS folder is empty |
208 |
if [ -z "$(ls -A $amiroosdir)" ]; then |
209 |
printWarning "$amiroosdir is already empty\n" |
210 |
return 1 |
211 |
else |
212 |
# get some information from Git |
213 |
cd $osdir |
214 |
local git_basehash=($(git ls-tree -d HEAD $amiroosdir)); git_basehash=${git_basehash[2]} |
215 |
cd $amiroosdir |
216 |
local git_branch_current=$(git rev-parse --abbrev-ref HEAD) |
217 |
local git_difftobase="$(git diff ${git_basehash}..HEAD)" |
218 |
local git_commits=$(git log --format=oneline ${git_basehash}..HEAD) |
219 |
local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc)) |
220 |
cd $userdir |
221 |
local issues=0 |
222 |
# if HEAD is ahead of submodule base commit |
223 |
if [ -n "$git_difftobase" ]; then |
224 |
issues=$((issues + 1)) |
225 |
printWarning "HEAD is ahead of submodule base\n" |
226 |
fi |
227 |
# if there are untracked, modified, or deleted files |
228 |
if [ ${#git_dirtyfiles[@]} != 0 ]; then |
229 |
issues=$((issues + 1)) |
230 |
printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n" |
231 |
fi |
232 |
if [ $issues -gt 0 ]; then |
233 |
local userinput="" |
234 |
printWarning "$issues issues detected. Do you want to continue? [y/n]\n" |
235 |
readUserInput "YyNn" userinput |
236 |
case "$userinput" in |
237 |
Y|y) |
238 |
;; |
239 |
N|n) |
240 |
printfWarning "wiping AMiRo-OS Git submodule aborted by user\n" |
241 |
return 2 |
242 |
;; |
243 |
*) # sanity check (return error) |
244 |
printError "unexpected input: $userinput\n"; return -1;; |
245 |
esac |
246 |
fi |
247 |
|
248 |
# checkout base commit and delete all local branches |
249 |
cd $osdir |
250 |
git submodule update --force --checkout $amiroosdir | tee -a $LOG_FILE |
251 |
cd $amiroosdir |
252 |
local git_branches=($(git for-each-ref --format="%(refname)")) |
253 |
for branch in $git_branches; do |
254 |
if [[ $branch = *"heads/"* ]]; then |
255 |
git branch -D ${branch##*/} | tee -a $LOG_FILE |
256 |
fi |
257 |
done |
258 |
cd $userdir |
259 |
|
260 |
# deinitialize AMiRo-OS submodule and delete any remaining files |
261 |
cd $osdir |
262 |
git submodule deinit -f $amiroosdir 2>&1 | tee -a $LOG_FILE |
263 |
rm -rf $amiroosdir/* |
264 |
cd $userdir |
265 |
|
266 |
return 0 |
267 |
fi |
268 |
} |
269 |
|
270 |
### main function of this script ############################################### |
271 |
# Initializes or wipes the AMiRo-OS Git submodule, and provides an entry point |
272 |
# to its setup. |
273 |
# |
274 |
# usage: see function printHelp |
275 |
# arguments: see function printHelp |
276 |
# return: 0 |
277 |
# No error or warning occurred. |
278 |
# |
279 |
function main { |
280 |
# print welcome/info text if not suppressed |
281 |
if [[ $@ != *"--noinfo"* ]]; then |
282 |
printWelcomeText |
283 |
else |
284 |
printf "######################################################################\n" |
285 |
fi |
286 |
printf "\n" |
287 |
|
288 |
# if --help or -h was specified, print the help text and exit |
289 |
if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
290 |
printHelp |
291 |
printf "\n" |
292 |
quitScript |
293 |
fi |
294 |
|
295 |
# set log file if specified |
296 |
if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then |
297 |
# get the parameter (file name) |
298 |
local cmdidx=1 |
299 |
while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do |
300 |
cmdidx=$[cmdidx + 1] |
301 |
done |
302 |
local cmd="${!cmdidx}" |
303 |
local logfile="" |
304 |
if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then |
305 |
logfile=${cmd#*=} |
306 |
else |
307 |
local filenameidx=$((cmdidx + 1)) |
308 |
logfile="${!filenameidx}" |
309 |
fi |
310 |
# optionally force silent appending |
311 |
if [[ "$cmd" = "--LOG"* ]]; then |
312 |
setLogFile --option=c --quiet "$logfile" LOG_FILE |
313 |
else |
314 |
setLogFile "$logfile" LOG_FILE |
315 |
printf "\n" |
316 |
fi |
317 |
fi |
318 |
# log script name |
319 |
printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
320 |
|
321 |
# parse arguments |
322 |
local otherargs=() |
323 |
while [ $# -gt 0 ]; do |
324 |
if ( parseIsOption $1 ); then |
325 |
case "$1" in |
326 |
-h|--help) # already handled; ignore |
327 |
shift 1;; |
328 |
-i|--init) |
329 |
initAmiroOs; printf "\n"; shift 1;; |
330 |
-s|--setup) |
331 |
amiroOsSetup; printf "\n"; shift 1;; |
332 |
-w|--wipe) |
333 |
wipeAmiroOs; printf "\n"; shift 1;; |
334 |
-q|--quit) |
335 |
quitScript; shift 1;; |
336 |
--log=*|--LOG=*) # already handled; ignore |
337 |
shift 1;; |
338 |
--log|--LOG) # already handled; ignore |
339 |
shift 2;; |
340 |
--noinfo) # already handled; ignore |
341 |
shift 1;; |
342 |
*) |
343 |
printError "invalid option: $1\n"; shift 1;; |
344 |
esac |
345 |
else |
346 |
otherargs+=("$1") |
347 |
shift 1 |
348 |
fi |
349 |
done |
350 |
|
351 |
# interactive menu |
352 |
while ( true ); do |
353 |
# main menu info prompt and selection |
354 |
printInfo "Operating system setup main menu\n" |
355 |
printf "Please select one of the following actions:\n" |
356 |
printf " [I] - initialize AMiRo-OS submodule\n" |
357 |
printf " [S] - enter AMiRo-OS setup\n" |
358 |
printf " [W] - wipe AMiRo-OS submodule\n" |
359 |
printf " [Q] - quit this setup\n" |
360 |
local userinput="" |
361 |
readUserInput "IiSsWwQq" userinput |
362 |
printf "\n" |
363 |
|
364 |
# evaluate user selection |
365 |
case "$userinput" in |
366 |
I|i) |
367 |
initAmiroOs; printf "\n";; |
368 |
S|s) |
369 |
amiroOsSetup; printf "\n";; |
370 |
W|w) |
371 |
wipeAmiroOs; printf "\n";; |
372 |
Q|q) |
373 |
quitScript;; |
374 |
*) # sanity check (exit with error) |
375 |
printError "unexpected argument: $userinput\n";; |
376 |
esac |
377 |
done |
378 |
|
379 |
exit 0 |
380 |
} |
381 |
|
382 |
################################################################################ |
383 |
# SCRIPT ENTRY POINT # |
384 |
################################################################################ |
385 |
|
386 |
main "$@" |