1-
21#! /bin/bash
2+
33set -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+ }
530
631# 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/')
832LATEST_RELEASE=v2.1.1
933
10- # Download the correct version based on the platform
34+ # Select the correct package based on OS and architecture
1135case " $( uname -s) " in
1236 Linux)
1337 if [[ $( uname -m) == " aarch64" ]]; then
@@ -29,18 +53,67 @@ case "$(uname -s)" in
2953 ;;
3054esac
3155
56+ # Main download URL
3257DOWNLOAD_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 "
3359
34- echo " Downloading $PLATFORM from $DOWNLOAD_URL "
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+ chmod +x libchdb.so
3570
36- # Download the file
37- curl -L -o libchdb.tar.gz $DOWNLOAD_URL
71+ if [[ " $1 " == " --global" ]]; then
72+ # If current uid is not 0, check if sudo is available and request the user to input the password
73+ if [[ $EUID -ne 0 ]]; then
74+ command -v sudo > /dev/null 2>&1 || { echo >&2 " This script requires sudo privileges but sudo is not installed. Aborting." ; exit 1; }
75+ echo " Installation requires administrative access. You will be prompted for your password."
76+ fi
3877
39- # Untar the file
40- tar -xzf libchdb.tar.gz
78+ # Define color messages if terminal supports them
79+ if [[ -t 1 ]]; then
80+ RED=' \033[0;31m'
81+ GREEN=' \033[0;32m'
82+ NC=' \033[0m' # No Color
83+ REDECHO () { echo -e " ${RED} $@ ${NC} " ; }
84+ GREENECHO () { echo -e " ${GREEN} $@ ${NC} " ; }
85+ ENDECHO () { echo -ne " ${NC} " ; }
86+ else
87+ REDECHO () { echo " $@ " ; }
88+ GREENECHO () { echo " $@ " ; }
89+ ENDECHO () { : ; }
90+ fi
4191
42- # Set execute permission for libchdb.so
43- chmod +x libchdb.so
92+ # Use sudo if not running as root
93+ SUDO=' '
94+ if [[ $EUID -ne 0 ]]; then
95+ SUDO=' sudo'
96+ GREENECHO " \nYou will be asked for your sudo password to install:"
97+ echo " libchdb.so to /usr/local/lib/"
98+ echo " chdb.h to /usr/local/include/"
99+ fi
100+
101+ # Make sure the library and header directory exists
102+ ${SUDO} mkdir -p /usr/local/lib /usr/local/include || true
103+
104+ # Install the library and header file
105+ ${SUDO} /bin/cp libchdb.so /usr/local/lib/
106+ ${SUDO} /bin/cp chdb.h /usr/local/include/
107+
108+ # Set execute permission for libchdb.so
109+ ${SUDO} chmod +x /usr/local/lib/libchdb.so
110+
111+ # Update library cache (Linux specific)
112+ if [[ " $( uname -s) " == " Linux" ]]; then
113+ ${SUDO} ldconfig
114+ fi
44115
45- # Clean up
46- rm -f libchdb.tar.gz
116+ GREENECHO " Installation completed successfully." ; ENDECHO
117+ GREENECHO " If any error occurred, please report it to:" ; ENDECHO
118+ GREENECHO " https://github.com/chdb-io/chdb/issues/new/choose" ; ENDECHO
119+ fi
0 commit comments