forked from paradise-fi/RoFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·201 lines (165 loc) · 5.52 KB
/
setup.sh
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# RoFI environment setup script
# Backup a value of a variable in order to restore it during teardown
backup() {
local varcontent="$(eval echo \"'$'$1\")"
local backupcontent="$(eval echo \"'$ORIGINAL_'$1\")"
# Do not overwrite backups (e.g., when re-invoking setup)
if [ -z "$backupcontent" ]
then
if [ -n "$varcontent" ]
then
eval export ORIGINAL_$1=\""$varcontent"\"
TEARDOWN_CMD="$TEARDOWN_CMD export $1=\"\$ORIGINAL_$1\"; unset ORIGINAL_$1;"
else
# Set backup so it's not overwritten
eval export ORIGINAL_$1=\"" "\"
TEARDOWN_CMD="$TEARDOWN_CMD unset $1; unset ORIGINAL_$1;"
fi
fi
}
teardownImpl() {
eval $TEARDOWN_CMD
unset ROFI_BUILD_CONFIGURATION
unset ROFI_ROOT
unset ROFI_BUILD_DIR
unset TEARDOWN_CMD
unset -f teardown
unset -f teardownImpl
}
teardown() {
echo "Tearing down the RoFI environment"
teardownImpl
}
configurationDesc() {
case "$1" in
"Debug") echo D;;
"Release") echo R;;
"RelWithDebInfo") echo RD;;
"MinSizeRel") echo MSR;;
*) echo U
esac
}
setGazeboVariables() {
backup GAZEBO_NAME
# backup GAZEBO_MASTER_URI
# backup GAZEBO_MODEL_DATABASE_URI
backup GAZEBO_RESOURCE_PATH
backup GAZEBO_PLUGIN_PATH
backup GAZEBO_MODEL_PATH
if [ -z "$GAZEBO_NAME" ]; then
GAZEBO_NAME="gazebo"
fi
local GAZEBO_INFO=$(whereis $GAZEBO_NAME | cut -d: -f2)
# xargs trims whitespace
# local GAZEBO_BIN=$(echo $INFO | xargs | cut -d' ' -f1)
local GAZEBO_SHARE=$(echo $GAZEBO_INFO | xargs | cut -d' ' -f2)
if [ -z "$GAZEBO_SHARE" ]; then
return
fi
# GAZEBO_MASTER_URI=$ORIGINAL_GAZEBO_MASTER_URI
# GAZEBO_MODEL_DATABASE_URI=$ORIGINAL_GAZEBO_MODEL_DATABASE_URI
GAZEBO_RESOURCE_PATH=$ORIGINAL_GAZEBO_RESOURCE_PATH
GAZEBO_PLUGIN_PATH=$ORIGINAL_GAZEBO_PLUGIN_PATH
GAZEBO_MODEL_PATH=$ORIGINAL_GAZEBO_MODEL_PATH
source $GAZEBO_SHARE/setup.sh
}
print_help() {
cat << EOF
Usage:
source setup.sh [-f] <Release|Debug|RelWithDebInfo|MinSizeRel>
Setup environment for given configuration. Release is the default one.
./setup.sh -h
Print help
Options:
-f Alternate prompt to indicate the environment.
EOF
}
############## Main script ##############
## Check if sourced - has to be called outside function
# Took from https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced
([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] ||
[[ -n $KSH_VERSION && $(cd "$(dirname -- "$0")" &&
printf '%s' "${PWD%/}/")$(basename -- "$0") != "${.sh.file}" ]] ||
[[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)) && sourced=1
run() {
local OPTIND flag
local ALTER_PROMPT
while getopts "fh" flag; do
case "$flag" in
f) ALTER_PROMPT=1;;
h)
print_help
return 0
;;
?)
return 1
;;
esac
done
CWD=$(pwd)
if [ ! -e ${CWD}/setup.sh ] || [ "$(head -n1 ${CWD}/setup.sh)" != "# RoFI environment setup script" ]; then
echo "The script needs to be invoked from the root of the project."
return 1
fi
export PS1 # Bring shell variable into env, so we can back it up
backup PS1
backup PATH
setGazeboVariables
ROFI_BUILD_CONFIGURATION="Release"
if [ -z "$sourced" ]; then
echo "Invalid usage; do not invoke directly, use 'source [-f] <Release|Debug|RelWithDebInfo>' instead\n"
return 1
fi
ARG1=${@:$OPTIND:1}
if [ -n "${ARG1}" ];
then ROFI_BUILD_CONFIGURATION=${ARG1};
fi
case "$ROFI_BUILD_CONFIGURATION" in
Release|Debug|RelWithDebInfo|MinSizeRel);;
*)
echo "Unsupported build configuration $ROFI_BUILD_CONFIGURATION"
return 1
;;
esac
echo "Setting up environment for $ROFI_BUILD_CONFIGURATION."
echo " To go back, invoke teardown"
echo " To change the configuration, simply invoke setup again"
if [ -n "$ALTER_PROMPT" ]
then
PS1="🤖 $(configurationDesc $ROFI_BUILD_CONFIGURATION) $ORIGINAL_PS1"
else
PS1="$ORIGINAL_PS1"
fi
export ROFI_BUILD_CONFIGURATION
export ROFI_ROOT=$(pwd)
export ROFI_BUILD_DIR="$(pwd)/build.${ROFI_BUILD_CONFIGURATION}"
export GAZEBO_MODEL_PATH="$ROFI_ROOT/data/gazebo/models:$GAZEBO_MODEL_PATH"
export GAZEBO_PLUGIN_PATH="$ROFI_BUILD_DIR/desktop/gazebosim/rofiModule:$GAZEBO_PLUGIN_PATH"
export GAZEBO_PLUGIN_PATH="$ROFI_BUILD_DIR/desktop/gazebosim/roficom:$GAZEBO_PLUGIN_PATH"
export GAZEBO_PLUGIN_PATH="$ROFI_BUILD_DIR/desktop/gazebosim/distributor:$GAZEBO_PLUGIN_PATH"
export GAZEBO_RESOURCE_PATH="$ROFI_ROOT/data/gazebo:$GAZEBO_RESOURCE_PATH"
export GAZEBO_RESOURCE_PATH="$ROFI_BUILD_DIR/desktop/data/gazebo:$GAZEBO_RESOURCE_PATH"
export PATH="$(realpath releng/tools):$ORIGINAL_PATH"
## Add bin directories of the suites to path
for suite in $(find suites -maxdepth 1 -type d -exec basename {} \;); do
export PATH="${ROFI_BUILD_DIR}/$suite/bin:$PATH"
done
## Register tab completion
if [ -n "$ZSH_VERSION" ]; then
autoload bashcompinit
bashcompinit
fi
source releng/tools/_registerCompletion.sh
}
run $@
if [ $? -ne 0 ]; then
echo "No changes have been made"
print_help
teardownImpl
fi
## Cleanup namespace
unset -f print_help
unset -f backup
unset -f configurationDesc
unset -f setGazeboVariables
unset -f run