-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinstall.sh
executable file
·166 lines (144 loc) · 3.88 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env sh
set -eu
GITHUB_REPO="coder/wush"
BINARY_NAME="wush"
INSTALL_DIR="/usr/local/bin"
# Function to determine the platform
detect_platform() {
OS=$(uname -s)
ARCH=$(uname -m)
case $OS in
Linux)
PLATFORM="linux"
;;
Darwin)
PLATFORM="darwin"
;;
FreeBSD)
PLATFORM="freebsd"
;;
CYGWIN*|MINGW*|MSYS*)
PLATFORM="windows"
;;
*)
echo "Unsupported OS: $OS"
exit 1
;;
esac
case $ARCH in
x86_64|amd64)
ARCH="amd64"
;;
i386|i686)
ARCH="386"
;;
armv7l|armv6l)
ARCH="armv7"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo "Unsupported architecture: $ARCH"
exit 1
;;
esac
echo "${PLATFORM}_${ARCH}"
}
# Function to determine the preferred archive format
select_archive_format() {
PLATFORM_ARCH=$1
case "$PLATFORM_ARCH" in
darwin_*)
# Check if Homebrew is installed
if command -v brew >/dev/null 2>&1; then
>&2 echo "Using Homebrew for installation."
brew install "$BINARY_NAME" # Install using Homebrew
exit 0 # Exit after installation
else
echo "zip" # We only ship .zip archives for macOS
fi
;;
windows_*)
echo "zip" # We only ship .zip archives for Windows
;;
*)
if command -v tar >/dev/null 2>&1; then
echo "tar.gz"
elif command -v unzip >/dev/null 2>&1; then
echo "zip"
else
echo "Unsupported: neither tar nor unzip are available."
exit 1
fi
;;
esac
}
main() {
PLATFORM_ARCH=$(detect_platform)
# Determine preferred archive format
FILE_EXT=$(select_archive_format "$PLATFORM_ARCH")
# Get the latest release download URL from GitHub API
LATEST_RELEASE_URL=$(curl -fsSL \
"https://api.github.com/repos/$GITHUB_REPO/releases/latest" \
| grep "browser_download_url" \
| grep "$PLATFORM_ARCH.$FILE_EXT" \
| cut -d '"' -f 4 | head -n 1)
if [ -z "$LATEST_RELEASE_URL" ]; then
echo "No release found for platform $PLATFORM_ARCH with format $FILE_EXT"
exit 1
fi
# Download the release archive
TMP_DIR=$(mktemp -d)
ARCHIVE_PATH="$TMP_DIR/$BINARY_NAME.$FILE_EXT"
echo "Downloading $BINARY_NAME from $LATEST_RELEASE_URL..."
curl -L -o "$ARCHIVE_PATH" "$LATEST_RELEASE_URL"
# Extract the archive
echo "Extracting $BINARY_NAME..."
if [ "$FILE_EXT" = "zip" ]; then
unzip -d "$TMP_DIR" "$ARCHIVE_PATH"
elif [ "$FILE_EXT" = "tar.gz" ]; then
tar -xzf "$ARCHIVE_PATH" -C "$TMP_DIR"
else
echo "Unsupported file extension: $FILE_EXT"
exit 1
fi
# Find the binary (assuming it's in the extracted files)
BINARY_PATH=$(find "$TMP_DIR" -type f -name "$BINARY_NAME")
# Make the binary executable
chmod +x "$BINARY_PATH"
# Install the binary
if [ "$PLATFORM_ARCH" = "windows-amd64" ] || [ "$PLATFORM_ARCH" = "windows-386" ]; then
INSTALL_DIR="$HOME/bin"
mkdir -p "$INSTALL_DIR"
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME.exe"
else
# Run using sudo if not root
if [ "$(id -u)" -ne 0 ]; then
sudo sh <<EOF
if [ "$(uname -s)" = "Linux" ]; then
if command -v setcap >/dev/null 2>&1; then
setcap cap_net_admin=eip "$BINARY_PATH"
else
echo "Warning: 'setcap' command is not available. Transfer speeds may be slower."
fi
fi
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"
EOF
else
if [ "$(uname -s)" = "Linux" ]; then
if command -v setcap >/dev/null 2>&1; then
setcap cap_net_admin=eip "$BINARY_PATH"
else
echo "Warning: 'setcap' command is not available. Transfer speeds may be slower."
fi
fi
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"
fi
fi
# Clean up
rm -rf "$TMP_DIR"
echo "$BINARY_NAME installed successfully!"
}
# Run the installation
main