amiro-os / periphery-lld / peripherylldsetup.sh @ e189c0a6
History | View | Annotate | Download (12.181 KB)
1 | e545e620 | Thomas Schöpping | ################################################################################ |
---|---|---|---|
2 | # AMiRo-OS is an operating system designed for the Autonomous Mini Robot # |
||
3 | # (AMiRo) platform. # |
||
4 | 96621a83 | Thomas Schöpping | # Copyright (C) 2016..2020 Thomas Schöpping et al. # |
5 | e545e620 | 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 | 8516dad6 | Thomas Schöpping | # load library |
27 | source "$(dirname ${BASH_SOURCE[0]})/../tools/bash/setuplib.sh" |
||
28 | e545e620 | 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 AMiRo-LLD submodule setup! #\n" |
||
40 | printf "# #\n" |
||
41 | printf "######################################################################\n" |
||
42 | printf "# #\n" |
||
43 | 96621a83 | Thomas Schöpping | printf "# Copyright (c) 2016..2020 Thomas Schöpping #\n" |
44 | e545e620 | 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] [-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 AMiRo-LLD submodule.\n" |
||
70 | printf " -w, --wipe\n" |
||
71 | printf " Wipe AMiRo-LLD 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 AMiRo-LLD submodule ############################################# |
||
79 | # Initialize the AMiRo-LLD submodule. |
||
80 | # |
||
81 | # usage: initAmiroLld |
||
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 | 57cbd1cd | Thomas Schöpping | # -2 |
90 | # Error: Missing dependencies. |
||
91 | e545e620 | Thomas Schöpping | # |
92 | function initAmiroLld { |
||
93 | printInfo "initializing AMiRo-LLD submodule...\n" |
||
94 | local userdir=$(pwd) |
||
95 | local peripheryllddir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
||
96 | local amirollddir=${peripheryllddir}/AMiRo-LLD |
||
97 | |||
98 | 1bb524af | Thomas Schöpping | # if the AMiRo-LLD folder is not empty |
99 | e545e620 | Thomas Schöpping | if [ ! -z "$(ls -A $amirollddir)" ]; then |
100 | printWarning "$(realpath $amirollddir) is not empty. Delete and reinitialize? [y/n]\n" |
||
101 | local userinput="" |
||
102 | readUserInput "YyNn" userinput |
||
103 | case $userinput in |
||
104 | Y|y) |
||
105 | 1bb524af | Thomas Schöpping | wipeAmiroLld |
106 | e545e620 | Thomas Schöpping | ;; |
107 | N|n) |
||
108 | printWarning "AMiRo-LLD 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 | 57cbd1cd | 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 | e545e620 | Thomas Schöpping | # initialize submodule to default branch |
124 | cd $peripheryllddir |
||
125 | git submodule update --init $amirollddir 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 $amirollddir 2>&1 | tee -a $LOG_FILE;; |
||
133 | N|n) |
||
134 | printWarning "AMiRo-LLD 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 AMiRo-LLD submodule and wipe directory ############################### |
||
148 | # Resets the AMiRo-LLD Git submodule and wipes the directory. |
||
149 | # |
||
150 | # usage: wipeAmiroLld |
||
151 | # arguments: n/a |
||
152 | 57cbd1cd | Thomas Schöpping | # return: 0 |
153 | # No error or warning occurred. |
||
154 | # 1 |
||
155 | # Warning: AMiRo-LLD 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 | e545e620 | Thomas Schöpping | function wipeAmiroLld { |
164 | printInfo "reset and wipe Git submodule $amirollddir\n" |
||
165 | local userdir=$(pwd) |
||
166 | local peripheryllddir=$(dirname $(realpath ${BASH_SOURCE[0]})) |
||
167 | local amirollddir=${peripheryllddir}/AMiRo-LLD |
||
168 | |||
169 | 57cbd1cd | 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 | e545e620 | Thomas Schöpping | # if the AMiRo-LLD folder is empty |
177 | if [ -z "$(ls -A $amirollddir)" ]; then |
||
178 | printWarning "$amirollddir is already empty\n" |
||
179 | return 1 |
||
180 | else |
||
181 | # get some information from Git |
||
182 | local userdir=$(pwd) |
||
183 | cd $peripheryllddir |
||
184 | local git_basehash=($(git ls-tree -d HEAD $amirollddir)); git_basehash=${git_basehash[2]} |
||
185 | cd $amirollddir |
||
186 | local git_branch_current=$(git rev-parse --abbrev-ref HEAD) |
||
187 | local git_difftobase="$(git diff ${git_basehash}..HEAD)" |
||
188 | local git_commits=$(git log --format=oneline ${git_basehash}..HEAD) |
||
189 | local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc)) |
||
190 | cd $userdir |
||
191 | local issues=0 |
||
192 | # if HEAD is ahead of submodule base commit |
||
193 | if [ -n "$git_difftobase" ]; then |
||
194 | issues=$((issues + 1)) |
||
195 | printWarning "HEAD is ahead of submodule base\n" |
||
196 | fi |
||
197 | # if there are untracked, modified, or deleted files |
||
198 | if [ ${#git_dirtyfiles[@]} != 0 ]; then |
||
199 | issues=$((issues + 1)) |
||
200 | printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n" |
||
201 | fi |
||
202 | if [ $issues -gt 0 ]; then |
||
203 | local userinput="" |
||
204 | printWarning "$issues issues detected. Do you want to continue? [y/n]\n" |
||
205 | readUserInput "YyNn" userinput |
||
206 | case "$userinput" in |
||
207 | Y|y) |
||
208 | ;; |
||
209 | N|n) |
||
210 | printfWarning "wiping AMiRo-LLD Git submodule aborted by user\n" |
||
211 | return 2 |
||
212 | ;; |
||
213 | *) # sanity check (return error) |
||
214 | printError "unexpected input: $userinput\n"; return -1;; |
||
215 | esac |
||
216 | fi |
||
217 | |||
218 | # checkout base commit and delete all local branches |
||
219 | cd $peripheryllddir |
||
220 | git submodule update --force --checkout $amirollddir | tee -a $LOG_FILE |
||
221 | cd $amirollddir |
||
222 | local git_branches=($(git for-each-ref --format="%(refname)")) |
||
223 | for branch in $git_branches; do |
||
224 | if [[ $branch = *"heads/"* ]]; then |
||
225 | git branch -D ${branch##*/} | tee -a $LOG_FILE |
||
226 | fi |
||
227 | done |
||
228 | cd $userdir |
||
229 | |||
230 | # deinitialize AMiRo-LLD submodule and delete any remaining files |
||
231 | cd $peripheryllddir |
||
232 | git submodule deinit -f $amirollddir 2>&1 | tee -a $LOG_FILE |
||
233 | rm -rf $amirollddir/* |
||
234 | cd $userdir |
||
235 | |||
236 | return 0 |
||
237 | fi |
||
238 | } |
||
239 | |||
240 | ### main function of this script ############################################### |
||
241 | # Initializes or wipes the AMiRo-LLD Git submodule. |
||
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 | initAmiroLld; printf "\n"; shift 1;; |
||
299 | -w|--wipe) |
||
300 | wipeAmiroLld; 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 "Periphery-LLD setup main menu\n" |
||
322 | printf "Please select one of the following actions:\n" |
||
323 | printf " [I] - initialize AMiRo-LLD submodule\n" |
||
324 | printf " [W] - wipe AMiRo-LLD submodule\n" |
||
325 | printf " [Q] - quit this setup\n" |
||
326 | local userinput="" |
||
327 | readUserInput "IiPpDdWwQq" userinput |
||
328 | printf "\n" |
||
329 | |||
330 | # evaluate user selection |
||
331 | case "$userinput" in |
||
332 | I|i) |
||
333 | initAmiroLld; printf "\n";; |
||
334 | W|w) |
||
335 | wipeAmiroLld; 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 "$@" |