1
-
2
1
#! /bin/bash
2
+
3
3
set -e
4
- cd $( dirname " ${BASH_SOURCE[0]} " )
4
+
5
+ # Check for necessary tools
6
+ command -v curl > /dev/null 2>&1 || { echo >&2 " curl is required but it's not installed. Aborting." ; exit 1; }
7
+ command -v tar > /dev/null 2>&1 || { echo >&2 " tar is required but it's not installed. Aborting." ; exit 1; }
8
+
9
+ # Function to download and extract the file
10
+ download_and_extract () {
11
+ local url=$1
12
+ local file=" libchdb.tar.gz"
13
+
14
+ echo " Attempting to download $PLATFORM from $url "
15
+
16
+ # Download the file with a retry logic
17
+ if curl -L -o " $file " " $url " ; then
18
+ echo " Download successful."
19
+
20
+ # Optional: Verify download integrity here, if checksums are provided
21
+
22
+ # Untar the file
23
+ if tar -xzf " $file " ; then
24
+ echo " Extraction successful."
25
+ return 0
26
+ fi
27
+ fi
28
+ return 1
29
+ }
5
30
6
31
# Get the newest release version
7
- # LATEST_RELEASE=$(curl --silent "https://api.github.com/repos/chdb-io/chdb/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
8
32
LATEST_RELEASE=v2.1.1
9
33
10
- # Download the correct version based on the platform
34
+ # Select the correct package based on OS and architecture
11
35
case " $( uname -s) " in
12
36
Linux)
13
37
if [[ $( uname -m) == " aarch64" ]]; then
@@ -29,18 +53,79 @@ case "$(uname -s)" in
29
53
;;
30
54
esac
31
55
56
+ # Main download URL
32
57
DOWNLOAD_URL=" https://github.com/chdb-io/chdb/releases/download/$LATEST_RELEASE /$PLATFORM "
58
+ FALLBACK_URL=" https://github.com/chdb-io/chdb/releases/latest/download/$PLATFORM "
59
+
60
+ # Try the main download URL first
61
+ if ! download_and_extract " $DOWNLOAD_URL " ; then
62
+ echo " Retrying with fallback URL..."
63
+ if ! download_and_extract " $FALLBACK_URL " ; then
64
+ echo " Both primary and fallback downloads failed. Aborting."
65
+ exit 1
66
+ fi
67
+ fi
68
+
69
+ # check if --local flag is passed
70
+ if [[ " $1 " == " --local" ]]; then
71
+ # Set execute permission for libchdb.so
72
+ chmod +x libchdb.so
73
+
74
+ # Clean up
75
+ rm -f libchdb.tar.gz
76
+ exit 0
77
+ elif [[ " $1 " == " --global" ]]; then
78
+ # If current uid is not 0, check if sudo is available and request the user to input the password
79
+ if [[ $EUID -ne 0 ]]; then
80
+ command -v sudo > /dev/null 2>&1 || { echo >&2 " This script requires sudo privileges but sudo is not installed. Aborting." ; exit 1; }
81
+ echo " Installation requires administrative access. You will be prompted for your password."
82
+ fi
83
+
84
+ # Define color messages if terminal supports them
85
+ if [[ -t 1 ]]; then
86
+ RED=' \033[0;31m'
87
+ GREEN=' \033[0;32m'
88
+ NC=' \033[0m' # No Color
89
+ REDECHO () { echo -e " ${RED} $@ ${NC} " ; }
90
+ GREENECHO () { echo -e " ${GREEN} $@ ${NC} " ; }
91
+ ENDECHO () { echo -ne " ${NC} " ; }
92
+ else
93
+ REDECHO () { echo " $@ " ; }
94
+ GREENECHO () { echo " $@ " ; }
95
+ ENDECHO () { : ; }
96
+ fi
97
+
98
+ # Use sudo if not running as root
99
+ SUDO=' '
100
+ if [[ $EUID -ne 0 ]]; then
101
+ SUDO=' sudo'
102
+ GREENECHO " \nYou will be asked for your sudo password to install:"
103
+ echo " libchdb.so to /usr/local/lib/"
104
+ echo " chdb.h to /usr/local/include/"
105
+ fi
106
+
107
+ # Make sure the library and header directory exists
108
+ ${SUDO} mkdir -p /usr/local/lib /usr/local/include || true
33
109
34
- echo " Downloading $PLATFORM from $DOWNLOAD_URL "
110
+ # Install the library and header file
111
+ ${SUDO} /bin/cp libchdb.so /usr/local/lib/
112
+ ${SUDO} /bin/cp chdb.h /usr/local/include/
35
113
36
- # Download the file
37
- curl -L -o libchdb.tar.gz $DOWNLOAD_URL
114
+ # Set execute permission for libchdb.so
115
+ ${SUDO} chmod +x /usr/local/lib/ libchdb.so
38
116
39
- # Untar the file
40
- tar -xzf libchdb.tar.gz
117
+ # Update library cache (Linux specific)
118
+ if [[ " $( uname -s) " == " Linux" ]]; then
119
+ ${SUDO} ldconfig
120
+ fi
41
121
42
- # Set execute permission for libchdb.so
43
- chmod +x libchdb.so
122
+ # Clean up
123
+ rm -f libchdb.tar.gz libchdb.so chdb.h
44
124
45
- # Clean up
46
- rm -f libchdb.tar.gz
125
+ GREENECHO " Installation completed successfully." ; ENDECHO
126
+ GREENECHO " If any error occurred, please report it to:" ; ENDECHO
127
+ GREENECHO " https://github.com/chdb-io/chdb/issues/new/choose" ; ENDECHO
128
+ else
129
+ echo " Invalid option. Use --local to install locally or --global to install globally."
130
+ exit 1
131
+ fi
0 commit comments