Split Common folder based on operating system families
[mirror/qt/qt5.git] / coin / provisioning / common / unix / InstallFromCompressedFileFromURL.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 test suite 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=try_catch.sh
37 source "${BASH_SOURCE%/*}/try_catch.sh"
38 # shellcheck source=DownloadURL.sh
39 source "${BASH_SOURCE%/*}/DownloadURL.sh"
40
41 ExceptionCreateTmpFile=100
42 ExceptionCreateTmpDirectory=101
43 ExceptionUncompress=102
44 ExceptionMoveApp=103
45 ExceptionDeleteTmpFile=104
46 ExceptionRemoveTmpDirectory=105
47 ExceptionUnknownFormat=106
48
49
50 function InstallFromCompressedFileFromURL {
51     url=$1
52     url_alt=$2
53     expectedSha1=$3
54     installDirectory=$4
55     appPrefix=$5
56
57     try
58     (
59         basefilename=${url##*/}
60         extension=${basefilename##*.}
61         filename=${basefilename%.*}
62         if [ "$extension" == "gz" ] && [ "${filename##*.}" == "tar" ]; then
63             extension="tar.gz"
64         fi
65         echo "Extension for file: $extension"
66         echo "Creating temporary file and directory"
67         targetFile=$(mktemp "$TMPDIR$(uuidgen)XXXXX.$extension") || throw $ExceptionCreateTmpFile
68         targetDirectory=$(mktemp -d) || throw $ExceptionCreateTmpDirectory
69         DownloadURL "$url" "$url_alt" "$expectedSha1" "$targetFile"
70         echo "Uncompress $targetFile"
71         case $extension in
72             "tar.gz")
73                 tar -xzf "$targetFile" --directory "$targetDirectory" || throw $ExceptionUncompress
74             ;;
75             "zip")
76                 unzip "$targetFile" -d "$targetDirectory" || throw $ExceptionUncompress
77             ;;
78             *)
79                 throw $ExceptionUnknownFormat
80             ;;
81         esac
82         echo "Moving app to $installDirectory"
83         sudo mkdir "$installDirectory"
84         sudo mv "$targetDirectory/$appPrefix/"* "$installDirectory" || throw $ExceptionMoveApp
85         echo "Removing file '$targetFile'"
86         rm "$targetFile" || throw $ExceptionDeleteTmpFile
87         echo "Removing directory '$targetDirectory'"
88         rm -rf "$targetDirectory" || throw $ExceptionRemoveTmpDirectory
89     )
90
91     catch || {
92         case $ex_code in
93             $ExceptionCreateTmpFile)
94                 echo "Failed to create temporary file"
95                 exit 1;
96             ;;
97             $ExceptionUncompress)
98                 echo "Failed extracting compressed file."
99                 exit 1;
100             ;;
101             $ExceptionMoveApp)
102                 echo "Failed moving app to target location."
103                 exit 1;
104             ;;
105             $ExceptionDeleteTmpFile)
106                 echo "Failed deleting temporary file."
107                 exit 1;
108             ;;
109             $ExceptionRemoveTmpDirectory)
110                 echo "Failed deleting temporary file."
111                 exit 1;
112             ;;
113             $ExceptionUnknownFormat)
114                 echo "Unknown file format."
115                 exit 1;
116             ;;
117         esac
118     }
119 }
120