-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnshcfg.sh
executable file
·382 lines (301 loc) · 8.28 KB
/
nshcfg.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
###########################################################################
# Linux configuration JSON based script #
# Version 1.0.1 09.10.2023 #
# #
# (C) Copyright Daniel Nashed/NashCom 2023 #
# Feedback domino_unix@nashcom.de #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and #
# limitations under the License. #
###########################################################################
# This script is mainly designed to deploy Domino One-Touch JSON configurations.
# It is based on a flexible design, it can be also used to deploy other files.
# The script uses a flexible menu structure which is designed to load configurations from https://, http:// and file:/ URLs.
# The menu is explicitly designed to span multiple files e.g. in multiple GitHub repositories.
# It also supports auto detection if no configuration is specified for automated setups.
# Parameters
# ----------
# $1 -> Local target file
# -----------------------
#
# Local file name to be written.
# Existing files are not overwritten for security reasons.
# In additon the remote site should not be able to specify the file name or directory for security reasons.
# $2 -> Download URL
# ------------------
#
# The URL can be either https://, http:// or file:/ for a local file
# If no URL is specified, domain name of the current server with the well known URL on HTTPS is assumed
# Example: https://www.acme.com/.well-known/domino.cfg
# $3 -> Index in JSON file
# ------------------------
#
# Index in JSON file can be optionally configured.
# By default /config is assumed
# Debug Mode
# nshcfg_debug=yes
# Default config environment file
if [ -z "$DOMINO_AUTO_CFG_DEFAULTS_ENV_FILE" ]; then
DOMINO_AUTO_CFG_DEFAULTS_ENV_FILE=/local/notesdata/DominoAutoConfigDefault.env
fi
DebugText()
{
if [ "$nshcfg_debug" = "yes" ]; then
echo "$(date '+%F %T') Debug:" $@
fi
return 0
}
LogError()
{
echo "ERROR: $@"
}
DownloadFile()
{
local URL=$1
local TARGET_FILE=$2
if [ -z "$URL" ]; then
LogError "No download URL specified!"
return 1
fi
if [ -z "$TARGET_FILE" ]; then
TARGET_FILE="$TARGET_CFG_FILE"
fi
if [ -z "$TARGET_FILE" ]; then
LogError "No download target file specified!"
return 1
fi
case $URL in
/*)
if [ ! -e "$URL" ]; then
LogError "Cannot copy file. Local source file does not exist!"
return 1
fi
cp "$URL" "$TARGET_FILE"
;;
esac
curl -sL "$URL" -o "$TARGET_FILE"
DebugText "DownloadFile [$URL] -> [$TARGET_FILE]"
return 0
}
GetConfig()
{
local N=0
local KEY=
local CFG=
local LINE=
local INDEX=
local URL=
local ONE_TOUCH_ENV=
local ONE_TOUCH_JSON=
local DXL=
local SELECTED=
if [ -z "$1" ]; then
return 1
fi
JQ_VERSION=$(jq --version 2>/dev/null)
if [ -z "$JQ_VERSION" ]; then
LogError "Setup requires JQ!"
return 1
fi
case $1 in
/*)
if [ -e "$1" ]; then
JSON=$(cat $1)
fi
;;
*)
JSON=$(curl -sL $1)
;;
esac
if [ -z "$JSON" ]; then
LogError "No JSON returned from [$1]!"
return 1
fi
KEY=$(echo $2 | tr [/] [.])
CFG=$(echo $JSON | jq $KEY.index.cfg 2>/dev/null)
if [ "null" = "$CFG" ]; then
CFG=$(echo $JSON | jq $KEY.cfg)
if [ "null" = "$CFG" ]; then
LogError "No configruation found!"
return 1
fi
fi
if [ -z "$CFG" ]; then
LogError "No configuration or invalid JSON! [$1]"
return 1
fi
SELECT=$(echo $CFG | jq -r ' . | map (.name) | join("\n")')
echo
N=0
while IFS= read -r LINE
do
if [ -n "$LINE" ]; then
N=$(($N + 1))
# Don't print lines with one dot only
if [ "." != "$LINE" ]; then
echo "[$N] $LINE"
fi
fi
done <<< "$SELECT"
if [ "$N" = "0" ]; then
LogError "No configuration found!"
return 1
fi
# Set to one to automatically select if only one entry is in list
if [ "$N" = "1" ]; then
SELECTED=1
else
echo
read -p "Select [1-$N] 0 to cancel? " SELECTED
echo
if [ "$SELECTED" = "0" ]; then
return 0
fi
fi
N=0
while IFS= read -r LINE
do
if [ -n "$LINE" ]; then
N=$(($N + 1))
if [ "$N" = "$SELECTED" ]; then
KEY=$LINE
fi
fi
done <<< "$SELECT"
# Get config
URL=$(echo $CFG | jq --arg key "$KEY" -r '.[] | select(.name==$key) | .URL')
INDEX=$(echo $CFG | jq --arg key "$KEY" -r '.[] | select(.name==$key) | .index')
ONE_TOUCH_ENV=$(echo $CFG | jq --arg key "$KEY" -r '.[] | select(.name==$key) | .oneTouchENV')
ONE_TOUCH_JSON=$(echo $CFG | jq --arg key "$KEY" -r '.[] | select(.name==$key) | .oneTouchJSON')
DXL=$(echo $CFG | jq --arg key "$KEY" -r '.[] | select(.name==$key) | .DXL')
# Process setup index config
if [ "null" != "$URL" ] || [ "null" != "$INDEX" ]; then
if [ "null" = "$URL" ]; then
URL=
fi
if [ "null" = "$INDEX" ]; then
INDEX=
fi
if [ -z "$URL" ]; then
URL=$1
fi
# Ensure to clear previous selection before jumping to sub "menu"
SELECTED=
GetConfig "$URL" "$INDEX"
return 0
fi
if [ "null" = "$ONE_TOUCH_JSON" ] ; then
ONE_TOUCH_JSON=
fi
if [ "null" = "$ONE_TOUCH_ENV" ]; then
ONE_TOUCH_ENV=
fi
if [ "null" = "$DXL" ]; then
DXL=
fi
# Allow to download multiple files and only log errors if none is found
local FOUND=
if [ -n "$ONE_TOUCH_JSON" ] ; then
DownloadFile "$ONE_TOUCH_JSON"
FOUND=1
fi
if [ -n "$ONE_TOUCH_ENV" ]; then
# In case a JSON was already found, an environment file is a defaults file with a fixed name
if [ -z "$FOUND" ]; then
DownloadFile "$ONE_TOUCH_ENV"
FOUND=1
else
DownloadFile "$ONE_TOUCH_ENV" "$DOMINO_AUTO_CFG_DEFAULTS_ENV_FILE"
fi
fi
if [ -n "$DXL" ]; then
DownloadFile "$DXL"
FOUND=1
fi
if [ -z "$FOUND" ]; then
LogError "No configuration option found [$SELECTED]"
return 1
fi
return 0
}
DownloadConfig()
{
TARGET_CFG_FILE=$1
local URL=$2
local INDEX=$3
local DOMAIN=
# For security reasons a target file cannot be assumed!
if [ -z "$TARGET_CFG_FILE" ]; then
LogError "No target file specified!"
return 1
fi
# For security reasons, never overwrite files!
if [ -e "$TARGET_CFG_FILE" ]; then
LogError "Target file already exists! [$TARGET_CFG_FILE]"
return 1
fi
# Dot is a placeholder for an empty hostname
if [ "." = "$URL" ]; then
URL=
fi
# Use server's domain for target URL, if not specified
if [ -z "$URL" ]; then
DOMAIN=$(hostname -d)
if [ -n "$DOMAIN" ]; then
URL=https://$DOMAIN/.well-known/domino.cfg
fi
fi
if [ -z "$URL" ]; then
LogError "No URL specified!"
return 1
fi
# Assume well known config if no known file extension is specified
case $URL in
*.cfg)
;;
*.json)
;;
*.txt)
;;
*)
URL=$URL/.well-known/domino.cfg
;;
esac
# Set protocol to https:// if not specified
case $URL in
http://*)
;;
https://*)
;;
file:/*)
;;
/*)
;;
*)
URL=https://$URL
;;
esac
# Set index to /index if not specified
if [ -n "$INDEX" ]; then
case $INDEX in
/*)
;;
*)
INDEX=/$INDEX
;;
esac
fi
echo "Getting configuration from $URL $INDEX"
GetConfig "$URL" "$INDEX"
}
DownloadConfig "$@"