-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·269 lines (243 loc) · 7.63 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·269 lines (243 loc) · 7.63 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env bash
set -euo pipefail
# install.sh - build and install the aether binary
# Usage:
# ./install.sh [--prefix /usr/local] [--dest /custom/bin] [--no-sudo] [--build-only]
# Examples:
# ./install.sh # build and install to /usr/local/bin (uses sudo if needed)
# ./install.sh --prefix $HOME/.local --no-sudo
# ./install.sh --build-only # only build, don't install
#
# Behavior:
# - Builds the Go project located in the same directory as this script.
# - By default installs the resulting `aether` binary to /usr/local/bin.
# - Use --prefix to change the prefix (DEST defaults to PREFIX/bin).
# - Use --dest to set the exact installation directory (overrides --prefix).
# - Use --no-sudo to avoid invoking sudo when creating directories or moving the binary.
# - Use --build-only to only produce the built binary in ./build/aether and skip installation.
PREFIX="/usr/local"
DEST_DIR=""
USE_SUDO=true
BUILD_ONLY=false
BUILD_DIR="build"
BINARY_NAME="aether"
AUTO_INSTALL_DEPS=false
print_usage() {
cat <<EOF
Usage: $0 [options]
Options:
--prefix DIR Install prefix (default: /usr/local). Binary will go to PREFIX/bin unless --dest is used.
--dest DIR Exact directory to install the binary into (overrides --prefix).
--no-sudo Do not attempt to use sudo when installing to system directories.
--build-only Only build the binary, do not install it.
--auto-install-deps Try to install missing system packages using apt/dnf (requires sudo/root).
-h, --help Show this help and exit.
EOF
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--prefix)
if [[ $# -lt 2 ]]; then
echo "Missing value for --prefix" >&2
exit 2
fi
PREFIX="$2"
shift 2
;;
--dest)
if [[ $# -lt 2 ]]; then
echo "Missing value for --dest" >&2
exit 2
fi
DEST_DIR="$2"
shift 2
;;
--no-sudo)
USE_SUDO=false
shift
;;
--build-only)
BUILD_ONLY=true
shift
;;
--auto-install-deps)
AUTO_INSTALL_DEPS=true
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
print_usage
exit 2
;;
esac
done
if [[ -z "$DEST_DIR" ]]; then
DEST_DIR="$PREFIX/bin"
fi
# Move to script directory (assumes the repo / project root is here)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Ensure Go toolchain is available
# detect package manager (apt or dnf)
detect_pm() {
# Prefer OS release identification when available
if [[ -f /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
ids="$ID ${ID_LIKE:-}"
ids_lc="${ids,,}"
if [[ "$ids_lc" =~ (fedora|rhel|centos|rocky|almalinux) ]]; then
echo "dnf"
return
fi
if [[ "$ids_lc" =~ (debian|ubuntu|pop|raspbian) ]]; then
echo "apt"
return
fi
fi
# Fallback to existence of package manager binaries
if command -v dnf >/dev/null 2>&1; then
echo "dnf"
return
fi
if command -v apt-get >/dev/null 2>&1; then
echo "apt"
return
fi
echo ""
}
# install system packages using detected package manager
install_system_packages() {
local pm="$1"; shift
local pkgs=("$@")
if [[ -z "$pm" || ${#pkgs[@]} -eq 0 ]]; then
return 1
fi
if [[ "$pm" == "apt" ]]; then
if [[ "$USE_SUDO" == true && "$(id -u)" -ne 0 ]]; then
sudo apt-get update -y
sudo apt-get install -y "${pkgs[@]}"
else
apt-get update -y
apt-get install -y "${pkgs[@]}"
fi
return $?
fi
if [[ "$pm" == "dnf" ]]; then
if [[ "$USE_SUDO" == true && "$(id -u)" -ne 0 ]]; then
sudo dnf install -y "${pkgs[@]}"
else
dnf install -y "${pkgs[@]}"
fi
return $?
fi
return 1
}
# If requested, attempt to install missing system dependencies
if [[ "$AUTO_INSTALL_DEPS" == true ]]; then
pm="$(detect_pm)"
if [[ -z "$pm" ]]; then
echo "No supported package manager found (apt or dnf). Skipping auto-install of deps." >&2
else
echo "Detected package manager: $pm"
missing=()
for cmd in go curl tar; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo "Missing commands: ${missing[*]}. Attempting to install corresponding packages via $pm"
pkgs=()
for c in "${missing[@]}"; do
case "$c" in
go)
if [[ "$pm" == "apt" ]]; then
# try common Debian/Ubuntu package names
pkgs+=(golang-go golang)
else
# try Fedora/RPM package names
pkgs+=(golang-bin golang)
fi
;;
curl) pkgs+=(curl) ;;
tar) pkgs+=(tar) ;;
*) pkgs+=("$c") ;;
esac
done
install_system_packages "$pm" "${pkgs[@]}" || echo "Auto-install failed or incomplete; please install: ${pkgs[*]}" >&2
else
echo "All required commands present"
fi
fi
fi
# Ensure Go toolchain is available
if ! command -v go >/dev/null 2>&1; then
echo "Error: Go toolchain not found in PATH. Please install Go and try again." >&2
exit 1
fi
# Print Go version for visibility (non-fatal)
if command -v go >/dev/null 2>&1; then
echo "Using $(go version)"
fi
echo "Building ${BINARY_NAME}..."
# Prepare build directory
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
# Build the project's main package. We intentionally build the package rooted at this directory
# so that the produced binary includes the main program present here.
if ! go build -o "$BUILD_DIR/$BINARY_NAME" .; then
echo "Build failed." >&2
exit 1
fi
echo "Built: $BUILD_DIR/$BINARY_NAME"
if [[ "$BUILD_ONLY" == true ]]; then
echo "Build-only requested; not installing. Binary is at: $BUILD_DIR/$BINARY_NAME"
exit 0
fi
# Ensure destination directory exists (create if necessary)
create_dir() {
local d="$1"
if [[ -d "$d" ]]; then
return 0
fi
if [[ "$USE_SUDO" == true && "$(id -u)" -ne 0 ]]; then
echo "Creating destination directory with sudo: $d"
if ! sudo mkdir -p "$d"; then
echo "Failed to create destination directory: $d" >&2
exit 1
fi
else
mkdir -p "$d"
fi
}
create_dir "$DEST_DIR"
# Install (move) the binary
install_binary() {
local src="$1"
local dst_dir="$2"
local dst="$dst_dir/$BINARY_NAME"
if [[ "$USE_SUDO" == true && "$(id -u)" -ne 0 ]]; then
echo "Installing to $dst_dir with sudo..."
if ! sudo mv -f "$src" "$dst"; then
echo "Failed to move binary to $dst" >&2
exit 1
fi
if ! sudo chmod 755 "$dst"; then
echo "Warning: failed to set permissions on $dst" >&2
fi
else
echo "Installing to $dst_dir..."
mv -f "$src" "$dst"
chmod 755 "$dst"
fi
echo "Installed: $dst"
}
install_binary "$BUILD_DIR/$BINARY_NAME" "$DEST_DIR"
echo "Installation complete. You can run '${BINARY_NAME}' from the command line."
exit 0