-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdomdownload.sh
executable file
·2705 lines (2078 loc) · 61.1 KB
/
domdownload.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
###########################################################################
# Domino Software Download Script #
# Version 1.0.5 22.09.2024 #
# Copyright Nash!Com, Daniel Nashed #
# #
# 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. #
###########################################################################
# Supported Environments
# ----------------------
#
# - RedHat/CentOS & clones
# - Ubuntu/Debian
# - VMware Photon OS
# - MacOS
# - Alpine Linux
# - GitBash on Windows
# Change History
# --------------
#
# 0.9.2 Support for GitBash
# 0.9.3 Bug fixes
# 0.9.4 Custom download and "local" mode
# 0.9.5 Fix for software with multiple languages (e.g language packs)
# '-silent' option for not printing information about a web-kit
# Caching access token for 40 minutes to avoid round trips got get a new access token (token is valid for 60 minutes)
# 0.9.6 Bug fixes
# 0.9.7 Fit & Finish changes
# 0.9.8 Performance counter for Linux but not for Mac because there is only a seconds timer in "date" and we don't want to install tools extra for this
# 0.9.9 Better error output for invalid refesh tokens
# 1.0.0 Support for Alpine Linux
# 1.0.1 Allow to be invoked if stdin is redirected. Default config changes
# 1.0.2 Add redirect support for custom download option
# 1.0.3 Add support for direct download specifying -filename & -fileid and -hash
# 1.0.4 Add error checks for fileURL results specially for license accept returned errors
# 1.0.5 Support for Alpine Linux
SCRIPT_NAME=$0
SCRIPT_DIR=$(dirname $SCRIPT_NAME)
DOMDOWNLOAD_SCRIPT_VERSION=1.0.5
# Just print version and exit
case "$1" in
-version|--version)
echo "$DOMDOWNLOAD_SCRIPT_VERSION"
exit 0
;;
esac
# Ensure to always read from terminal even stdin was redirected
exec < /dev/tty
ClearScreen()
{
if [ "$DISABLE_CLEAR_SCREEN" = "yes" ]; then
return 0
fi
clear
}
LogError()
{
echo >& 2
echo "ERROR: $@" >& 2
echo >& 2
}
LogMessage()
{
echo
echo "$@"
echo
}
LogMessageIfNotSilent()
{
if [ "$SILENT_MODE" = "yes" ]; then
return 0
fi
echo
echo "$@"
echo
}
print_delim()
{
echo "--------------------------------------------------------------------------------"
}
header()
{
echo
print_delim
echo "$1"
print_delim
echo
}
DebugText()
{
if [ "$DOMDOWNLOAD_DEBUG" = "yes" ]; then
echo
echo "$(date '+%F %T') Debug:" $@
echo
fi
return 0
}
DebugDump()
{
if [ "$DOMDOWNLOAD_DEBUG" = "yes" ]; then
echo
echo "-------------------- $1 --------------------"
echo "$2"
echo "-------------------- $1 --------------------"
echo
fi
}
DebugDumpFile()
{
if [ "$DOMDOWNLOAD_DEBUG" = "yes" ]; then
echo
echo "-------------------- $1 --------------------"
cat "$2"
echo "-------------------- $1 --------------------"
echo
fi
}
PerfTimerLogSession()
{
if [ "$OS_PLATFORM" = "Darwin" ]; then
return 0
fi
if [ -z "$PERF_LOG_FILE" ]; then
return 0
fi
# truncate log
if [ -e "$PERF_LOG_FILE" ]; then
tail -$PERF_MAX_LOG_LINES "$PERF_LOG_FILE" >> "$PERF_LOG_FILE.tmp"
mv -f "$PERF_LOG_FILE.tmp" "$PERF_LOG_FILE"
fi
echo >> "$PERF_LOG_FILE"
date '+%F %T' >> "$PERF_LOG_FILE"
echo "--------------------" >> "$PERF_LOG_FILE"
}
PerfTimerBegin()
{
if [ "$OS_PLATFORM" = "Darwin" ]; then
return 0
fi
if [ -z "$PERF_LOG_FILE" ]; then
return 0
fi
PERF_BEGIN_TIMER=$(($(date +%s%N)/1000000))
}
PerfTimerEnd()
{
if [ "$OS_PLATFORM" = "Darwin" ]; then
return 0
fi
if [ -z "$PERF_LOG_FILE" ]; then
return 0
fi
local END_TIMER=$(($(date +%s%N)/1000000))
local DIFF_TIMER=$(($END_TIMER-$PERF_BEGIN_TIMER))
if [ $DIFF_TIMER -gt $1 ]; then
printf "%6s ms %s\n" "$DIFF_TIMER" "$2" >> "$PERF_LOG_FILE"
fi
}
CheckWriteFile()
{
if [ ! -w "$1" ]; then
LogError "Cannot write to file: $1"
exit 1
fi
}
CheckWriteDir()
{
if [ ! -w "$1" ]; then
LogError "Cannot write to directory: $1"
exit 1
fi
}
remove_file()
{
if [ -z "$1" ]; then
DebugText "remove_file - No file specified"
return 1
fi
if [ ! -e "$1" ]; then
DebugText "remove_file - File does not exist: $1"
return 2
fi
rm -f "$1"
if [ ! "$?" = "0" ]; then
LogError "Cannot delete file: $1"
return 3
fi
DebugText "File removed: $1"
return 0
}
create_link()
{
if [ -z "$1" ]; then
return 1
fi
if [ -z "$2" ]; then
return 2
fi
if [ -e "$2" ]; then
DebugText "create_link - Link already exists [$2]"
return 0
fi
if [ ! -r "$1" ]; then
LogError "create_link - Cannot read file when creating a link: $1"
return 0
fi
ln -s "$1" "$2"
if [ ! "$?" = "0" ]; then
LogError "create_link - Cannote create link [$1] -> [$2]"
return 3
fi
DebugText "Link created [$1] -> [$2]"
return 0
}
install_package()
{
local SUDO=
if [ ! "$UID" = "0" ]; then
SUDO=sudo
fi
if [ -x /usr/bin/zypper ]; then
$SUDO /usr/bin/zypper install -y "$@"
elif [ -x /usr/bin/dnf ]; then
$SUDO /usr/bin/dnf install -y "$@"
elif [ -x /usr/bin/tdnf ]; then
$SUDO /usr/bin/tdnf install -y "$@"
elif [ -x /usr/bin/microdnf ]; then
$SUDO /usr/bin/microdnf install -y "$@"
elif [ -x /usr/bin/yum ]; then
$SUDO /usr/bin/yum install -y "$@"
elif [ -x /usr/bin/apt-get ]; then
if [ -z "$UBUNTU_PACKAGES_LOADED" ]; then
LogMessage "Info: Loading Ubuntu packages first to ensure the repository is available and latest"
$SUDO /usr/bin/apt-get update -y
UBUNTU_PACKAGES_LOADED=1
fi
$SUDO /usr/bin/apt-get install -y "$@"
elif [ -x /sbin/apk ]; then
$SUDO /sbin/apk add "$@"
else
if [ -z "$2" ]; then
LogError "No package manager found!"
else
LogError "No package manager found. Please check $2 how to download $1."
exit 1
fi
fi
}
AssistInstallPackage()
{
local QUESTION=
echo
read -p "Install $1 : (yes/no) ? " QUESTION
if [ "$QUESTION" = "yes" ] || [ "$QUESTION" = "y" ]; then
header "Installing $1"
install_package "$1"
echo
echo
return 0
else
return 1
fi
}
AssistInstallJQ4Mac()
{
local QUESTION=
local JQ_GITHUB_BIN=
echo
read -p "Install JQ for MacOSX from GitHub project : (yes/no) ? " QUESTION
if [ "$QUESTION" = "yes" ] || [ "$QUESTION" = "y" ]; then
if [ "$CPU_TYPE" = "arm64" ]; then
JQ_GITHUB_BIN="https://github.com/jqlang/jq/releases/download/jq-1.7/jq-macos-arm64"
else
JQ_GITHUB_BIN="https://github.com/jqlang/jq/releases/download/jq-1.7/jq-macos-amd64"
fi
$CURL_BIN -sL "$JQ_GITHUB_BIN" -o jq
chmod 755 jq
if [ -w /usr/local/bin ]; then
mv jq /usr/local/bin/jq
else
LogMessage "Info: Need root permissions to move jq to /usr/local/bin/jq (you might get promted for sudo)"
sudo mv jq /usr/local/bin/jq
if [ "$?" = "0" ]; then
JQ_CMD=/usr/local/bin/jq
return 0
else
LogError "Cannot move jq to /usr/local/bin. Please log in with root permissions and move jq manually"
return 1
fi
fi
echo
echo
return 0
else
return 1
fi
}
InstallJQ()
{
local HOME_PAGE="https://jqlang.github.io/jq/"
header "JQ package required"
echo "This script requires the JQ package."
echo "JQ is the de-facto standard for JSON files and used to parse JSON data."
echo
echo "See $HOME_PAGE for details."
echo
if [ "$OS_PLATFORM" = "Darwin" ]; then
AssistInstallJQ4Mac
else
AssistInstallPackage jq "$HOME_PAGE"
fi
JQ_VERSION=$($JQ_CMD --version 2>/dev/null)
if [ -z "$JQ_VERSION" ]; then
LogError "This script requires JQ!"
exit 1
fi
LogMessage "$JQ_VERSION installed"
}
InstallCurl()
{
local HOME_PAGE="https://curl.se/"
header "Curl package required"
echo "This script requires the Curl package."
echo "Curl is the de-facto standard for command-line URL requests."
echo
echo "See $HOME_PAGE for details."
echo
AssistInstallPackage curl "$HOME_PAGE"
CURL_VERSION=$(curl --version 2>/dev/null)
if [ -z "$CURL_VERSION" ]; then
LogError "This script requires Curl!"
exit 1
fi
header "Curl installed"
echo "$CURL_VERSION"
echo ---
echo
}
CheckBin()
{
if [ ! -x "/usr/bin/$1" ] && [ ! -x "/bin/$1" ] ; then
AssistInstallPackage "$1"
fi
if [ ! -x "/usr/bin/$1" ] && [ ! -x "/bin/$1" ] ; then
LogError "$1 is required!"
exit 1
fi
}
CheckEnvironment()
{
OS_PLATFORM=$(uname -s)
CPU_TYPE=$(uname -m)
DebugText "Platform: $OS_PLATFORM"
DebugText "CPU Type: $CPU_TYPE"
if [ "$OS_PLATFORM" = "Darwin" ]; then
CHECKSUM_VERSION=$(shasum --version 2>/dev/null | head -1)
CHECKSUM_CMD="shasum -a 256 -b"
if [ -z "$CHECKSUM_VERSION" ]; then
LogError "No shasum found"
exit 1
fi
else
if [ -x /usr/bin/sha256sum ]; then
CHECKSUM_CMD="/usr/bin/sha256sum"
elif [ -x /usr/local/bin/sha256sum ]; then
CHECKSUM_CMD="/usr/local/bin/sha256sum"
fi
if [ -z "$CHECKSUM_CMD" ]; then
LogError "No sha256sum found"
exit 1
fi
fi
if [ -e /usr/bin/curl ]; then
CURL_BIN=/usr/bin/curl
elif [ -e /bin/curl ]; then
CURL_BIN=/bin/curl
elif [ -e /mingw64/bin/curl ]; then
CURL_BIN=/mingw64/bin/curl
else
CURL_BIN=
fi
if [ -z "$CURL_BIN" ]; then
InstallCurl
CURL_BIN=/usr/bin/curl
fi
CURL_VERSION=$($CURL_BIN --version 2>/dev/null)
if [ -z "$CURL_VERSION" ]; then
LogError "This script requires Curl!"
exit 1
fi
if [ -e /usr/bin/jq ]; then
JQ_CMD=/usr/bin/jq
elif [ -e /bin/jq ]; then
JQ_CMD=/bin/jq
elif [ -e /usr/local/bin/jq ]; then
JQ_CMD=/usr/local/bin/jq
elif [ -e /mingw64/bin/jq ]; then
JQ_CMD=/mingw64/bin/jq
else
JQ_CMD=
fi
if [ -z "$JQ_CMD" ]; then
JQ_CMD=/usr/bin/jq
InstallJQ
# Check JQ again
if [ -e /usr/bin/jq ]; then
JQ_CMD=/usr/bin/jq
elif [ -e /bin/jq ]; then
JQ_CMD=/bin/jq
elif [ -e /usr/local/bin/jq ]; then
JQ_CMD=/usr/local/bin/jq
elif [ -e /mingw64/bin/jq ]; then
JQ_CMD=/mingw64/bin/jq
else
JQ_CMD=
fi
fi
JQ_VERSION=$($JQ_CMD --version 2>/dev/null | head -1)
DebugText "Curl : $CURL_VERSION"
DebugText "JQ : $JQ_VERSION"
CheckBin awk
CheckBin cut
CheckBin stat
CheckBin du
}
LogConnectionError()
{
if [ "$DOMDOWNLOAD_MODE" = "local" ]; then
LogMessage "LOCAL - MODE"
fi
if [ "$DOMDOWNLOAD_MODE" = "not-agreeed" ]; then
LogMessage "No agreement to connect to internet"
fi
LogError "$1"
}
ConfirmConnection()
{
if [ -e "$CONNECTION_AGREED_FILE" ]; then
return 0
fi
date > "$CONNECTION_AGREED_FILE"
}
CheckConnection()
{
if [ -n "$DOMDOWNLOAD_MODE" ]; then
return 0
fi
if [ ! -e "$CONNECTION_AGREED_FILE" ]; then
local QUESTION=
echo
header "Agree Internet Connection"
echo "Please agree to connect to Internet to"
echo
echo "- Download Domino installation software from My HCL Software Portal"
echo "- Download software information (JWT/JSON data) from HCL Domino AutoUpdate site"
echo
echo "Host names to connect to via HTTPS (port 443)"
echo
echo "$MYHCL_PORTAL_URL"
echo "$MYHCL_API_URL"
echo "$HCL_AUTOUPDATE_URL"
echo "$MYHCL_CDN_URL"
echo "$GITHUB_URL"
echo
echo
read -p "Confirm connection : (yes/no) ? " QUESTION
echo
if [ "$QUESTION" = "yes" ] || [ "$QUESTION" = "y" ]; then
date > "$CONNECTION_AGREED_FILE"
else
echo
QUESTION=
read -p "Do you want to use the local mode instead : (yes/no) ? " QUESTION
echo
if [ "$QUESTION" = "yes" ] || [ "$QUESTION" = "y" ]; then
DOMDOWNLOAD_MODE=local
echo >> "$DOMDOWNLOAD_CFG_FILE"
echo "# Local mode - configured at $(date)" >> "$DOMDOWNLOAD_CFG_FILE"
echo "DOMDOWNLOAD_MODE=local" >> "$DOMDOWNLOAD_CFG_FILE"
return 0
else
DOMDOWNLOAD_MODE=not-agreed
LogConnectionError "No internet connection"
exit 1
fi
fi
else
DebugText "Agreed to connect via $CONNECTION_AGREED_FILE at [$(cat $CONNECTION_AGREED_FILE)]"
fi
PerfTimerBegin
local CURL_RET=$($CURL_CMD --silent "$GITHUB_URL" --head -w 'RESP_CODE:%{response_code}\n')
PerfTimerEnd $PERF_MAX_CURL "$MYHCL_PORTAL_URL"
if [ -z "$(echo "$CURL_RET" | grep "RESP_CODE:200")" ]; then
DOMDOWNLOAD_MODE=error
else
DOMDOWNLOAD_MODE=online
fi
DebugText "Connection: $DOMDOWNLOAD_MODE"
}
SetRefreshToken()
{
# Set the download/refresh token to specified value or prompt
local REFRESH_TOKEN=$1
if [ -n "$REFRESH_TOKEN" ]; then
echo "$REFRESH_TOKEN" > "$DOMDOWNLOAD_TOKEN_FILE_NAME"
return 0
fi
if [ -n "$DOMDOWNLOAD_TOKEN" ]; then
echo "$DOMDOWNLOAD_TOKEN" > "$DOMDOWNLOAD_TOKEN_FILE_NAME"
export DOMDOWNLOAD_TOKEN=
return 0
fi
echo
echo "My HCL Software portal requires a download token."
echo
echo "Please visit -> $MYHCL_PORTAL_URL"
echo
echo "- Log in with your HCL software account."
echo "- Navigate to the upper right corner and select 'API keys' to generate a key."
echo "- Specify the generated key as a download token below."
echo
read -p "Enter Download Token: " REFRESH_TOKEN
echo
if [ -z "$REFRESH_TOKEN" ]; then
LogError "No download token specified"
LogMessage "Info: To set a token, please invoke: 'domdownload -token'"
exit 1
fi
echo "$REFRESH_TOKEN" > "$DOMDOWNLOAD_TOKEN_FILE_NAME"
}
GetAccessToken()
{
# Get access token and rotate refresh token from My HCL Software site
local REFRESH_TOKEN=
local JSON=
ACCESS_TOKEN=
if [ -e "$DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME" ]; then
# If access token isn't older than 40 min used cached token, else delete existing cached file
if [ -n "$(find "$DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME" -mmin -40 2>/dev/null)" ]; then
DebugText "Returning cached access token"
ACCESS_TOKEN=$(cat "$DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME")
return 0
fi
DebugText "Deleting old cached access token: $DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME"
remove_file "$DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME"
else
DebugText "No cached access token found: $DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME"
fi
if [ ! -e "$DOMDOWNLOAD_TOKEN_FILE_NAME" ]; then
SetRefreshToken
fi
if [ -e "$DOMDOWNLOAD_TOKEN_FILE_NAME" ]; then
REFRESH_TOKEN=$(cat "$DOMDOWNLOAD_TOKEN_FILE_NAME")
fi
if [ -z "$REFRESH_TOKEN" ]; then
LogError "No refresh token found"
return 1
fi
DebugDump "REFRESH_TOKEN" "$REFRESH_TOKEN"
DebugText "Request: $MYHCL_TOKEN_URL"
CheckWriteFile "$DOMDOWNLOAD_TOKEN_FILE_NAME"
# Exchange refresh token and get access token (JWT)
PerfTimerBegin
JSON=$($CURL_CMD -sL "$MYHCL_TOKEN_URL" -H "Content-Type: application/json" -d "{\"refreshToken\":\"$REFRESH_TOKEN\"}")
PerfTimerEnd $PERF_MAX_CURL "MyHCL-API-ExchangeToken"
DebugDump "JSON" "$JSON"
REFRESH_TOKEN=$(echo "$JSON" | $JQ_CMD -r .refreshToken)
if [ "$REFRESH_TOKEN" = "null" ] || [ -z "$REFRESH_TOKEN" ]; then
ERROR_TEXT=$(echo "$JSON" | $JQ_CMD -r .summary)
if [ -z "$ERROR_TEXT" ]; then
LogError "No refresh token returned"
else
LogError "$ERROR_TEXT"
fi
exit 1
fi
echo "$REFRESH_TOKEN" > "$DOMDOWNLOAD_TOKEN_FILE_NAME"
ACCESS_TOKEN=$(echo "$JSON" | $JQ_CMD -r .accessToken)
# Cache access token to avoid round trips for next request
echo "$ACCESS_TOKEN" > "$DOMDOWNLOAD_CACHED_ACCESS_TOKEN_FILE_NAME"
if [ ! "$DOMDOWNLOAD_DEBUG" = "yes" ]; then
return 0
fi
DebugDump "REFRESH_TOKEN" "$REFRESH_TOKEN"
DebugDump "ACCESS_TOKEN" "$ACCESS_TOKEN"
# Decode token and dump content
local PAYLOAD_BASE64URL=$(echo "$ACCESS_TOKEN" | /usr/bin/cut -d"." -f2)
local PAYLOAD_BASE64=$(echo -n $PAYLOAD_BASE64URL | tr '_-' '/+')
# Add padding for valid BASE64 encoding (base64url encoding removes padding)
local PAD_COUNT=$(expr 4 - ${#PAYLOAD_BASE64} % 4)
if [ "$PAD_COUNT" != "4" ]; then
while ((PAD_COUNT--)); do PAYLOAD_BASE64=$PAYLOAD_BASE64=; done
fi
DECODED_TOKEN=$(echo "$PAYLOAD_BASE64" | openssl base64 -d -A)
echo
echo "---- Decoded Token ----"
echo "$DECODED_TOKEN" | $JQ_CMD
echo "---- Decoded Token ----"
echo
return 0
}
CheckDownloadedFile()
{
local OUTFILE_FULLPATH="$1"
local FILE_SHA256=
if [ -z "FILE_CHECKSUM_SHA256" ]; then
LogMessage "Info: No checksum for web-kit. Skipping verification!"
LogMessage "$OUTFILE_FULLPATH"
exit 0
fi
PerfTimerBegin
FILE_SHA256=$($CHECKSUM_CMD "$OUTFILE_FULLPATH" | /usr/bin/cut -f1 -d" " | /usr/bin/awk '{print tolower($0)}')
PerfTimerEnd $PERF_MAX_CHECKSUM "SHA256: $OUTFILE_FULLPATH"
if [ "$FILE_SHA256" = "$(echo "$FILE_CHECKSUM_SHA256" | /usr/bin/awk '{print tolower($0)}')" ]; then
LogMessage "[OK] Hash verified"
else
LogMessage "[FAILED] Hash does NOT match!"
remove_file "$OUTFILE_FULLPATH"
exit 1
fi
}
DownloadCustom()
{
local DOWNLOAD_URL=
local DOWNLOAD_BASIC_AUTH=
local DOWNLOAD_BASIC_AUTH_CMD=
local OUTFILE_FULLPATH=$SOFTWARE_DIR/$1
if [ "$PRINT_INFO_ONLY" = "yes" ]; then
exit 0
fi
if [ -z "$1" ]; then
LogError "No custom download found"
return 0
fi
if [ -e "$OUTFILE_FULLPATH" ]; then
if [ "$FORCE_DOWNLOAD" = "yes" ]; then
LogMessage "Info: Overwriting existing download [$OUTFILE_FULLPATH]"
remove_file "$OUTFILE_FULLPATH"
else
LogMessageIfNotSilent "Info: File already exists [$OUTFILE_FULLPATH]"
return 0
fi
fi
CheckWriteDir "$SOFTWARE_DIR"
DOWNLOAD_URL="$DOMDOWNLOAD_CUSTOM_URL/$1"
LogMessage "Downloading from $DOWNLOAD_URL ..."
if [ -n "$DOMDOWNLOAD_CUSTOM_USER" ] && [ -n "$DOMDOWNLOAD_CUSTOM_PASSWORD" ]; then
DOWNLOAD_BASIC_AUTH="$DOMDOWNLOAD_CUSTOM_USER:$DOMDOWNLOAD_CUSTOM_PASSWORD"
DOWNLOAD_BASIC_AUTH_CMD="--user"
fi
PerfTimerBegin
$CURL_DOWNLOAD_CMD -L "$DOWNLOAD_URL" -o "$OUTFILE_FULLPATH" "$DOWNLOAD_BASIC_AUTH_CMD" "$DOWNLOAD_BASIC_AUTH"
PerfTimerEnd $PERF_MAX_CURL "$OUTFILE_FULLPATH"
if [ ! -e "$OUTFILE_FULLPATH" ]; then
LogError "Cannot download file from $DOWNLOAD_URL"
exit 1
fi
if [ -z "FILE_CHECKSUM_SHA256" ]; then
LogMessage "Info: No checksum for web-kit. Skipping verification!"
exit 0
fi
CheckDownloadedFile "$OUTFILE_FULLPATH"
LogMessage "$OUTFILE_FULLPATH"
}
DownloadSoftware()
{
# Download software or print curl command for command-line download
# $1: URL
# $2: Filename
# $3: SHA256
local OUTFILE_FULLPATH=$SOFTWARE_DIR/$2
local DOWNLOAD_FILE_URL=
if [ "$PRINT_INFO_ONLY" = "yes" ]; then
exit 0
fi
if [ -z "$1" ]; then
LogError "No file ID specified"
return 1
fi
if [ -z "$2" ]; then
LogError "No download file specified"
return 1
fi
if [ -n "$DOMDOWNLOAD_CUSTOM_URL" ]; then
DownloadCustom "$FILE_NAME"
exit 0
fi
if [ -e "$OUTFILE_FULLPATH" ]; then
if [ ! "$PRINT_DOWNLOAD_CURL_CMD" = "yes" ]; then
CheckWriteDir "$SOFTWARE_DIR"
if [ "$FORCE_DOWNLOAD" = "yes" ]; then
LogMessage "Info: Overwriting existing download [$OUTFILE_FULLPATH]"
remove_file "$OUTFILE_FULLPATH"
else
LogMessageIfNotSilent "Info: File already exists [$OUTFILE_FULLPATH]"
return 0
fi
fi
fi
CheckWriteDir "$SOFTWARE_DIR"
CheckConnection
if [ ! "$DOMDOWNLOAD_MODE" = "online" ]; then
LogConnectionError "Cannot download software"
exit 1
fi
GetAccessToken
if [ -z "$ACCESS_TOKEN" ]; then
LogError "No access token"
return 1
fi
DebugText "Request: ${MYHCL_DOWNLOAD_URL_PREFIX}$1${MYHCL_DOWNLOAD_URL_SUFFIX}"
RESULT_TEMP_FILE="$OUTFILE_FULLPATH.error"
PerfTimerBegin
DOWNLOAD_FILE_URL=$($CURL_CMD -s --write-out "%{redirect_url}\n" --output "$RESULT_TEMP_FILE" "${MYHCL_DOWNLOAD_URL_PREFIX}$1${MYHCL_DOWNLOAD_URL_SUFFIX}" -H "Authorization: Bearer $ACCESS_TOKEN" -o "$OUTFILE_FULLPATH")
PerfTimerEnd $PERF_MAX_CURL "MyHCL-API-FileURL"
DebugDumpFile "FileURL Result" "$RESULT_TEMP_FILE"
if [ -z "$DOWNLOAD_FILE_URL" ]; then
ERROR_TEXT=$(cat "$RESULT_TEMP_FILE" | $JQ_CMD -r .summary)
if [ -z "$ERROR_TEXT" ]; then
LogError "No download URL returned"
else
LogError "$ERROR_TEXT"
fi
remove_file "$RESULT_TEMP_FILE"
return 1
fi
remove_file "$RESULT_TEMP_FILE"
if [ "$PRINT_DOWNLOAD_CURL_CMD" = "yes" ]; then
echo
echo
echo Linux / MacOS Download:
echo
echo curl -L \'$DOWNLOAD_FILE_URL\' -o "$2"
echo
print_delim
echo
echo Windows Download:
echo
echo curl -L \"$DOWNLOAD_FILE_URL\" -o "$2"
echo
echo
return 0
fi
DebugText "Download URL: $DOWNLOAD_FILE_URL"
LogMessage "Downloading $OUTFILE_FULLPATH ..."
HTTP_STATUS=$($CURL_DOWNLOAD_CMD -L "$DOWNLOAD_FILE_URL" -w "%{http_code}" -H "Authorization: Bearer $ACCESS_TOKEN" -o "$OUTFILE_FULLPATH")
if [ "$?" != "0" ]; then
LogError "Failed to download ($1 -> $OUTFILE_FULLPATH)"
return 1
fi
if [ ! -e "$OUTFILE_FULLPATH" ]; then
LogError "Failed to download FileID: ($1 -> $OUTFILE_FULLPATH)"
return 1
fi
if [ "$HTTP_STATUS" = "200" ]; then
LogMessage "Download completed"
else
LogError "Failed to download - HTTP_STATUS: $HTTP_STATUS"
return 1
fi
if [ "$OS_PLATFORM" = "Darwin" ]; then
DOWNLOAD_FILE_SIZE=$(/usr/bin/stat -f%z "$OUTFILE_FULLPATH")
else
DOWNLOAD_FILE_SIZE=$(/usr/bin/du -b "$OUTFILE_FULLPATH" | /usr/bin/cut -f1)
fi