-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·127 lines (103 loc) · 3.08 KB
/
install.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
set -e
# Constants
DEFAULT_INSTALL_DIR="$HOME/.privateer/bin"
PRIVATEER_REPO="privateerproj/privateer"
LATEST_RELEASE_URL="https://api.github.com/repos/${PRIVATEER_REPO}/releases/latest"
# Detect OS (darwin = macOS, linux = Linux, msys or cygwin = Windows)
OS=""
case "$(uname -s)" in
Darwin)
OS="darwin"
;;
Linux)
OS="linux"
;;
CYGWIN*|MSYS*|MINGW*)
OS="windows"
;;
*)
echo "Unsupported Environment: $(uname -s)"
exit 1
;;
esac
download_latest_release() {
local install_dir="$1"
local install_file="$install_dir/privateer"
# Ensure the directory exists
mkdir -p "$install_dir"
# Fetch the download URL for the latest release
local url
url=$(curl -s ${LATEST_RELEASE_URL} | grep "browser_download_url.*${OS}.*" | cut -d '"' -f 4)
if [[ -z "$url" ]]; then
echo "Failed to fetch the download URL for the latest release."
exit 1
fi
echo "Downloading from: $url"
# Download the binary to the specified install directory
curl -L -o "$install_file" "$url"
if [[ $? -ne 0 ]]; then
echo "Failed to download the binary."
exit 1
fi
# Ensure the binary is executable
chmod +x "$install_file"
echo "Downloaded binary to $install_file"
}
update_path() {
local install_dir="$1"
# Check if the install directory is already in PATH
if [[ ":$PATH:" != *":$install_dir:"* ]]; then
echo "$install_dir is not in the PATH."
# Detect current shell
current_shell=$(basename "$SHELL")
case "$current_shell" in
bash)
config_file="$HOME/.bash_profile"
;;
zsh)
config_file="$HOME/.zshrc"
;;
fish)
config_file="$HOME/.config/fish/config.fish"
;;
*)
echo "Unsupported shell: $current_shell. You may need to manually add $install_dir to your PATH."
return
;;
esac
# Check if the path is already added to the config file
if ! grep -q "$install_dir" "$config_file"; then
echo "export PATH=\"$install_dir:\$PATH\"" >> "$config_file"
echo "$install_dir added to $config_file"
source $config_file
else
echo "$install_dir is already in $config_file."
fi
else
echo "$install_dir is already in the PATH."
fi
}
# Main logic
main() {
local install_dir="$DEFAULT_INSTALL_DIR"
# Handle CLI arguments for installation path override
while getopts "p:" opt; do
case $opt in
p)
install_dir="$OPTARG"
;;
*)
echo "Usage: $0 [-p install_path]"
exit 1
;;
esac
done
mkdir -p "$install_dir"
# Download the latest release
download_latest_release "$install_dir"
# Ensure the binary is accessible via PATH
update_path "$install_dir"
echo "Privateer installation complete!"
}
main "$@"