Split Common folder based on operating system families
[mirror/qt/qt5.git] / coin / provisioning / common / macos / InstallPKGFromURL.sh
1 #!/bin/bash
2
3 #############################################################################
4 ##
5 ## Copyright (C) 2017 The Qt Company Ltd.
6 ## Contact: http://www.qt.io/licensing/
7 ##
8 ## This file is part of the provisioning scripts of the Qt Toolkit.
9 ##
10 ## $QT_BEGIN_LICENSE:LGPL21$
11 ## Commercial License Usage
12 ## Licensees holding valid commercial Qt licenses may use this file in
13 ## accordance with the commercial license agreement provided with the
14 ## Software or, alternatively, in accordance with the terms contained in
15 ## a written agreement between you and The Qt Company. For licensing terms
16 ## and conditions see http://www.qt.io/terms-conditions. For further
17 ## information use the contact form at http://www.qt.io/contact-us.
18 ##
19 ## GNU Lesser General Public License Usage
20 ## Alternatively, this file may be used under the terms of the GNU Lesser
21 ## General Public License version 2.1 or version 3 as published by the Free
22 ## Software Foundation and appearing in the file LICENSE.LGPLv21 and
23 ## LICENSE.LGPLv3 included in the packaging of this file. Please review the
24 ## following information to ensure the GNU Lesser General Public License
25 ## requirements will be met: https://www.gnu.org/licenses/lgpl.html and
26 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
27 ##
28 ## As a special exception, The Qt Company gives you certain additional
29 ## rights. These rights are described in The Qt Company LGPL Exception
30 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
31 ##
32 ## $QT_END_LICENSE$
33 ##
34 #############################################################################
35
36 # shellcheck source=../unix/try_catch.sh
37 source "${BASH_SOURCE%/*}/../unix/try_catch.sh"
38
39 ExceptionCreateTmpFile=100
40 ExceptionDownloadPrimaryUrl=101
41 ExceptionDownloadAltUrl=102
42 ExceptionSHA1=103
43 ExceptionInstallerPKG=104
44 ExceptionDeleteTmpFile=105
45
46
47 function InstallPKGFromURL {
48     url=$1
49     url_alt=$2
50     expectedSha1=$3
51     targetDirectory=$4
52
53     try
54     (
55         echo "Creating temporary file"
56         targetFile=$(mktemp "$TMPDIR$(uuidgen).pkg") || trow $ExceptionCreateTmpFile
57         try
58         (
59             echo "Downloading PKG from primary URL '$url'"
60             curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url" || throw $ExceptionDownloadPrimaryUrl
61         )
62         catch || {
63             case $ex_code in
64                 $ExceptionDownloadPrimaryUrl)
65                     echo "Failed to download '$url' multiple times"
66                     echo "Downloading PKG from alternative URL '$url_alt'"
67                     curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url_alt" || throw $ExceptionDownloadAltUrl
68                 ;;
69             esac
70         }
71         echo "Checking SHA1 on PKG '$targetFile'"
72         echo "$expectedSha1 *$targetFile" > $targetFile.sha1
73         /usr/bin/shasum --check $targetFile.sha1 || throw $ExceptionSHA1
74         echo "Run installer on PKG"
75         sudo installer -package "$targetFile" -target "$targetDirectory" || throw $ExceptionInstallerPKG
76         echo "Removing file '$targetFile'"
77         rm "$targetFile" || throw $ExceptionDeleteTmpFile
78     )
79
80     catch || {
81         case $ex_code in
82             $ExceptionCreateTmpFile)
83                 echo "Failed to create temporary file"
84                 exit 1;
85             ;;
86             $ExceptionDownloadAltUrl)
87                 echo "Failed downloading PKG from primary and alternative URLs"
88                 exit 1;
89             ;;
90             $ExceptionSHA1)
91                 echo "Failed to check sha1sum."
92                 exit 1;
93             ;;
94             $ExceptionInstallerPKG)
95                 echo "Failed running installer on PKG."
96                 exit 1;
97             ;;
98             $ExceptionDeleteTmpFile)
99                 echo "Failed deleting temporary file."
100                 exit 1;
101             ;;
102         esac
103     }
104 }