1+ #! /bin/bash
2+ set -e
3+
4+ # Colors for output
5+ RED=' \033[0;31m'
6+ GREEN=' \033[0;32m'
7+ YELLOW=' \033[0;33m'
8+ NC=' \033[0m' # No Color
9+
10+ # GitHub repository information
11+ GITHUB_REPO=" threefoldtech/zinit"
12+
13+ # Get the latest version from GitHub API
14+ echo -e " ${YELLOW} Fetching latest version information...${NC} "
15+ if command -v curl & > /dev/null; then
16+ VERSION=$( curl -s " https://api.github.com/repos/${GITHUB_REPO} /releases/latest" | grep -o ' "tag_name": "[^"]*' | grep -o ' [^"]*$' )
17+ elif command -v wget & > /dev/null; then
18+ VERSION=$( wget -qO- " https://api.github.com/repos/${GITHUB_REPO} /releases/latest" | grep -o ' "tag_name": "[^"]*' | grep -o ' [^"]*$' )
19+ else
20+ echo -e " ${RED} Neither curl nor wget found. Please install one of them and try again.${NC} "
21+ exit 1
22+ fi
23+
24+ if [ -z " $VERSION " ]; then
25+ echo -e " ${RED} Failed to fetch the latest version. Please check your internet connection.${NC} "
26+ exit 1
27+ fi
28+
29+ echo -e " ${GREEN} Latest version: ${VERSION}${NC} "
30+ DOWNLOAD_URL=" https://github.com/${GITHUB_REPO} /releases/download/${VERSION} "
31+ MIN_SIZE_BYTES=2000000 # 2MB in bytes
32+
33+ echo -e " ${GREEN} Installing zinit ${VERSION} ...${NC} "
34+
35+ # Create temporary directory
36+ TMP_DIR=$( mktemp -d)
37+ trap ' rm -rf "$TMP_DIR"' EXIT
38+
39+ # Detect OS and architecture
40+ OS=$( uname -s | tr ' [:upper:]' ' [:lower:]' )
41+ ARCH=$( uname -m)
42+
43+ # Map architecture names
44+ if [ " $ARCH " = " x86_64" ]; then
45+ ARCH_NAME=" x86_64"
46+ elif [ " $ARCH " = " aarch64" ] || [ " $ARCH " = " arm64" ]; then
47+ ARCH_NAME=" aarch64"
48+ else
49+ echo -e " ${RED} Unsupported architecture: $ARCH ${NC} "
50+ exit 1
51+ fi
52+
53+ # Determine binary name based on OS and architecture
54+ if [ " $OS " = " linux" ]; then
55+ if [ " $ARCH_NAME " = " x86_64" ]; then
56+ BINARY_NAME=" zinit-linux-x86_64"
57+ else
58+ echo -e " ${RED} Unsupported Linux architecture: $ARCH ${NC} "
59+ exit 1
60+ fi
61+ elif [ " $OS " = " darwin" ]; then
62+ if [ " $ARCH_NAME " = " x86_64" ]; then
63+ BINARY_NAME=" zinit-macos-x86_64"
64+ elif [ " $ARCH_NAME " = " aarch64" ]; then
65+ BINARY_NAME=" zinit-macos-aarch64"
66+ else
67+ echo -e " ${RED} Unsupported macOS architecture: $ARCH ${NC} "
68+ exit 1
69+ fi
70+ else
71+ echo -e " ${RED} Unsupported operating system: $OS ${NC} "
72+ exit 1
73+ fi
74+
75+ # Download URL
76+ DOWNLOAD_PATH=" ${DOWNLOAD_URL} /${BINARY_NAME} "
77+ LOCAL_PATH=" ${TMP_DIR} /${BINARY_NAME} "
78+
79+ echo -e " ${YELLOW} Detected: $OS on $ARCH_NAME ${NC} "
80+ echo -e " ${YELLOW} Downloading from: $DOWNLOAD_PATH ${NC} "
81+
82+ # Download the binary
83+ if command -v curl & > /dev/null; then
84+ curl -L -o " $LOCAL_PATH " " $DOWNLOAD_PATH "
85+ elif command -v wget & > /dev/null; then
86+ wget -O " $LOCAL_PATH " " $DOWNLOAD_PATH "
87+ else
88+ echo -e " ${RED} Neither curl nor wget found. Please install one of them and try again.${NC} "
89+ exit 1
90+ fi
91+
92+ # Check file size
93+ FILE_SIZE=$( stat -f%z " $LOCAL_PATH " 2> /dev/null || stat -c%s " $LOCAL_PATH " 2> /dev/null)
94+ if [ " $FILE_SIZE " -lt " $MIN_SIZE_BYTES " ]; then
95+ echo -e " ${RED} Downloaded file is too small (${FILE_SIZE} bytes). Expected at least ${MIN_SIZE_BYTES} bytes.${NC} "
96+ echo -e " ${RED} This might indicate a failed or incomplete download.${NC} "
97+ exit 1
98+ fi
99+
100+ echo -e " ${GREEN} Download successful. File size: $( echo " $FILE_SIZE / 1000000" | bc -l | xargs printf " %.2f" ) MB${NC} "
101+
102+ # Make the binary executable
103+ chmod +x " $LOCAL_PATH "
104+
105+ # Determine installation directory
106+ if [ " $OS " = " darwin" ]; then
107+ # macOS - install to ~/hero/bin/
108+ INSTALL_DIR=" $HOME /hero/bin"
109+ else
110+ # Linux - install to /usr/local/bin/ if running as root, otherwise to ~/.local/bin/
111+ if [ " $( id -u) " -eq 0 ]; then
112+ INSTALL_DIR=" /usr/local/bin"
113+ else
114+ INSTALL_DIR=" $HOME /.local/bin"
115+ # Ensure ~/.local/bin exists and is in PATH
116+ mkdir -p " $INSTALL_DIR "
117+ if [[ " :$PATH :" != * " :$INSTALL_DIR :" * ]]; then
118+ echo -e " ${YELLOW} Adding $INSTALL_DIR to your PATH. You may need to restart your terminal.${NC} "
119+ if [ -f " $HOME /.bashrc" ]; then
120+ echo " export PATH=\"\$ PATH:$INSTALL_DIR \" " >> " $HOME /.bashrc"
121+ fi
122+ if [ -f " $HOME /.zshrc" ]; then
123+ echo " export PATH=\"\$ PATH:$INSTALL_DIR \" " >> " $HOME /.zshrc"
124+ fi
125+ fi
126+ fi
127+ fi
128+
129+ # Create installation directory if it doesn't exist
130+ mkdir -p " $INSTALL_DIR "
131+
132+ # Copy the binary to the installation directory
133+ cp " $LOCAL_PATH " " $INSTALL_DIR /zinit"
134+ echo -e " ${GREEN} Installed zinit to $INSTALL_DIR /zinit${NC} "
135+
136+ # Test the installation
137+ echo -e " ${YELLOW} Testing installation...${NC} "
138+ if " $INSTALL_DIR /zinit" --help & > /dev/null; then
139+ echo -e " ${GREEN} Installation successful! You can now use 'zinit' command.${NC} "
140+ echo -e " ${YELLOW} Example usage: zinit --help${NC} "
141+ " $INSTALL_DIR /zinit" --help | head -n 5
142+ else
143+ echo -e " ${RED} Installation test failed. Please check the error messages above.${NC} "
144+ exit 1
145+ fi
146+
147+ echo -e " ${GREEN} zinit ${VERSION} has been successfully installed!${NC} "
0 commit comments