-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
executable file
·95 lines (80 loc) · 2.11 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
# Function to get the latest release download URL for the specified OS and architecture
get_latest_release_url() {
repo="baalimago/clai"
os="$1"
arch="$2"
# Fetch the latest release data from GitHub API
release_data=$(curl -s "https://api.github.com/repos/$repo/releases/latest")
# Extract the asset URL for the specified OS and architecture
download_url=$(echo "$release_data" | grep "browser_download_url" | grep "$os" | grep "$arch" | cut -d '"' -f 4)
echo "$download_url"
}
# Detect the OS
case "$(uname)" in
Linux*)
os="linux"
;;
Darwin*)
os="darwin"
;;
*)
echo "Unsupported OS: $(uname)"
exit 1
;;
esac
# Detect the architecture
arch=$(uname -m)
case "$arch" in
x86_64)
arch="amd64"
;;
armv7*)
arch="arm"
;;
aarch64|arm64)
arch="arm64"
;;
i?86)
arch="386"
;;
*)
echo "Unsupported architecture: $arch"
exit 1
;;
esac
printf "detected os: '%s', arch: '%s'\n" "$os" "$arch"
# Get the download URL for the latest release
printf "finding asset url..."
download_url=$(get_latest_release_url "$os" "$arch")
printf "OK!\n"
# Download the binary
tmp_file=$(mktemp)
printf "downloading binary..."
if ! curl -s -L -o "$tmp_file" "$download_url"; then
echo
echo "Failed to download the binary."
exit 1
fi
printf "OK!\n"
printf "setting file executable file permissions..."
# Make the binary executable
if ! chmod +x "$tmp_file"; then
echo
echo "Failed to make the binary executable. Try running the script with sudo."
exit 1
fi
printf "OK!\n"
# Move the binary to standard XDG location and handle permission errors
INSTALL_DIR=$HOME/.local/bin
# If run as 'sudo', install to /usr/local/bin for systemwide use
if [ -x /usr/bin/id ]; then
if [ `/usr/bin/id -u` -eq 0 ]; then
INSTALL_DIR=/usr/local/bin
fi
fi
if ! mv "$tmp_file" $INSTALL_DIR/clai; then
echo "Failed to move the binary to $INSTALL_DIR/clai, see error above. Try making sure you have write permission there, or run 'mv $tmp_file <desired-position>'."
exit 1
fi
echo "clai installed successfully in $INSTALL_DIR, try it out with 'clai h'"