-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·486 lines (413 loc) · 11.8 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·486 lines (413 loc) · 11.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/bin/bash
#
# install.sh - Installer for carbon2cocoa
#
# This script detects the platform, checks for prerequisites, builds the
# application, and installs all files to the correct locations.
#
# Usage: ./install.sh [OPTIONS]
#
# Options:
# --prefix=PATH Installation prefix (default: /usr/local)
# --uninstall Remove installed files
# --help Show this help message
#
# Copyright (c) 2025 Pascal Harris, 45RPM Software
# Licensed under the MIT License
#
set -e
# Configuration
PROGRAM="carbon2cocoa"
VERSION="1.1.0"
DEFAULT_PREFIX="/usr/local"
# Colors for output (if terminal supports it)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
# Print functions
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[OK]${NC} $1"
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Show help
show_help() {
cat << EOF
carbon2cocoa Installer v${VERSION}
Usage: $0 [OPTIONS]
Options:
--prefix=PATH Installation prefix (default: ${DEFAULT_PREFIX})
--uninstall Remove installed files
--help Show this help message
The installer will:
1. Detect your operating system
2. Check for required build tools (C compiler, make)
3. Prompt to install missing prerequisites
4. Build the application
5. Install files to:
- \${PREFIX}/bin/${PROGRAM}
- \${PREFIX}/share/man/man1/${PROGRAM}.1
Examples:
$0 # Install to /usr/local
$0 --prefix=\$HOME/.local # Install to ~/.local
$0 --uninstall # Remove installed files
EOF
}
# Parse command line arguments
PREFIX="${DEFAULT_PREFIX}"
UNINSTALL=false
for arg in "$@"; do
case $arg in
--prefix=*)
PREFIX="${arg#*=}"
;;
--uninstall)
UNINSTALL=true
;;
--help|-h)
show_help
exit 0
;;
*)
error "Unknown option: $arg"
echo "Use --help for usage information."
exit 1
;;
esac
done
# Derived paths
BINDIR="${PREFIX}/bin"
MANDIR="${PREFIX}/share/man/man1"
# Detect operating system
detect_os() {
info "Detecting operating system..."
OS_NAME="$(uname -s)"
OS_ARCH="$(uname -m)"
case "${OS_NAME}" in
Darwin*)
OS_TYPE="macos"
OS_PRETTY="macOS"
if command -v sw_vers &> /dev/null; then
OS_VERSION="$(sw_vers -productVersion)"
OS_PRETTY="macOS ${OS_VERSION}"
fi
;;
Linux*)
OS_TYPE="linux"
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_PRETTY="${NAME} ${VERSION_ID:-}"
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS_PRETTY="${DISTRIB_DESCRIPTION:-Linux}"
else
OS_PRETTY="Linux"
fi
;;
FreeBSD*)
OS_TYPE="freebsd"
OS_PRETTY="FreeBSD $(uname -r)"
;;
CYGWIN*|MINGW*|MSYS*)
OS_TYPE="windows"
OS_PRETTY="Windows ($(uname -s))"
;;
*)
OS_TYPE="unknown"
OS_PRETTY="${OS_NAME}"
;;
esac
success "Detected: ${OS_PRETTY} (${OS_ARCH})"
}
# Check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Prompt user for yes/no
prompt_yn() {
local prompt="$1"
local default="${2:-n}"
local reply
if [ "$default" = "y" ]; then
prompt="${prompt} [Y/n] "
else
prompt="${prompt} [y/N] "
fi
read -p "$prompt" reply
reply="${reply:-$default}"
case "$reply" in
[Yy]*) return 0 ;;
*) return 1 ;;
esac
}
# Install prerequisites on macOS
install_prereqs_macos() {
info "Checking for Xcode Command Line Tools..."
if ! xcode-select -p &> /dev/null; then
warning "Xcode Command Line Tools not installed."
if prompt_yn "Install Xcode Command Line Tools?" "y"; then
info "Installing Xcode Command Line Tools..."
xcode-select --install
echo ""
echo "A dialog should have appeared to install the tools."
echo "Please complete the installation and run this script again."
exit 0
else
error "Cannot proceed without Xcode Command Line Tools."
exit 1
fi
fi
success "Xcode Command Line Tools installed"
}
# Install prerequisites on Linux
install_prereqs_linux() {
local missing=()
if ! command_exists gcc && ! command_exists clang; then
missing+=("gcc")
fi
if ! command_exists make; then
missing+=("make")
fi
if [ ${#missing[@]} -eq 0 ]; then
return 0
fi
warning "Missing prerequisites: ${missing[*]}"
if prompt_yn "Install missing prerequisites?" "y"; then
if command_exists apt-get; then
info "Using apt-get to install prerequisites..."
sudo apt-get update
sudo apt-get install -y build-essential
elif command_exists dnf; then
info "Using dnf to install prerequisites..."
sudo dnf install -y gcc make
elif command_exists yum; then
info "Using yum to install prerequisites..."
sudo yum install -y gcc make
elif command_exists pacman; then
info "Using pacman to install prerequisites..."
sudo pacman -S --noconfirm base-devel
elif command_exists zypper; then
info "Using zypper to install prerequisites..."
sudo zypper install -y gcc make
elif command_exists apk; then
info "Using apk to install prerequisites..."
sudo apk add build-base
else
error "Could not detect package manager."
echo "Please install manually: ${missing[*]}"
exit 1
fi
success "Prerequisites installed"
else
error "Cannot proceed without prerequisites: ${missing[*]}"
exit 1
fi
}
# Check prerequisites
check_prereqs() {
info "Checking prerequisites..."
local have_cc=false
local cc_name=""
if command_exists cc; then
have_cc=true
cc_name="cc"
elif command_exists gcc; then
have_cc=true
cc_name="gcc"
elif command_exists clang; then
have_cc=true
cc_name="clang"
fi
if ! $have_cc; then
warning "No C compiler found."
case "${OS_TYPE}" in
macos) install_prereqs_macos ;;
linux) install_prereqs_linux ;;
*) error "Please install a C compiler (gcc or clang) and try again."; exit 1 ;;
esac
else
success "C compiler: ${cc_name}"
fi
if ! command_exists make; then
warning "make not found."
case "${OS_TYPE}" in
macos) install_prereqs_macos ;;
linux) install_prereqs_linux ;;
*) error "Please install make and try again."; exit 1 ;;
esac
else
success "make: $(command -v make)"
fi
}
# Build the project
build_project() {
info "Building ${PROGRAM}..."
make clean &> /dev/null || true
if make; then
success "Build complete"
else
error "Build failed"
exit 1
fi
if [ ! -f "${PROGRAM}" ]; then
error "Binary not found after build"
exit 1
fi
info "Running quick test..."
if ./"${PROGRAM}" --version; then
success "Binary works correctly"
else
error "Binary test failed"
exit 1
fi
}
# Install files
install_files() {
info "Installing to ${PREFIX}..."
local use_sudo=""
if [ ! -w "${PREFIX}" ] 2>/dev/null; then
if [ -d "${PREFIX}" ]; then
warning "Installation directory requires elevated privileges."
use_sudo="sudo"
fi
fi
info "Creating directories..."
${use_sudo} mkdir -p "${BINDIR}"
${use_sudo} mkdir -p "${MANDIR}"
info "Installing binary..."
${use_sudo} install -m 755 "${PROGRAM}" "${BINDIR}/${PROGRAM}"
success "Installed: ${BINDIR}/${PROGRAM}"
if [ -f "${PROGRAM}.1" ]; then
info "Installing man page..."
${use_sudo} install -m 644 "${PROGRAM}.1" "${MANDIR}/${PROGRAM}.1"
success "Installed: ${MANDIR}/${PROGRAM}.1"
if command_exists mandb; then
${use_sudo} mandb -q 2>/dev/null || true
fi
else
warning "Man page not found, skipping"
fi
echo ""
success "Installation complete!"
echo ""
echo "The following files were installed:"
echo " ${BINDIR}/${PROGRAM}"
echo " ${MANDIR}/${PROGRAM}.1"
echo ""
if [[ ":$PATH:" != *":${BINDIR}:"* ]]; then
warning "${BINDIR} is not in your PATH."
echo ""
echo "Add the following to your shell configuration:"
echo " export PATH=\"${BINDIR}:\$PATH\""
echo ""
else
echo "You can now run: ${PROGRAM} --help"
fi
}
# Uninstall files
uninstall_files() {
info "Uninstalling from ${PREFIX}..."
local use_sudo=""
if [ ! -w "${BINDIR}" ] 2>/dev/null; then
use_sudo="sudo"
fi
local removed=false
if [ -f "${BINDIR}/${PROGRAM}" ]; then
info "Removing binary..."
${use_sudo} rm -f "${BINDIR}/${PROGRAM}"
success "Removed: ${BINDIR}/${PROGRAM}"
removed=true
fi
if [ -f "${MANDIR}/${PROGRAM}.1" ]; then
info "Removing man page..."
${use_sudo} rm -f "${MANDIR}/${PROGRAM}.1"
success "Removed: ${MANDIR}/${PROGRAM}.1"
removed=true
fi
if $removed; then
echo ""
success "Uninstallation complete!"
else
warning "No installed files found at ${PREFIX}"
fi
}
# Get the script's directory
get_script_dir() {
local script_path="${BASH_SOURCE[0]}"
while [ -L "$script_path" ]; do
local link_target="$(readlink "$script_path")"
if [[ "$link_target" == /* ]]; then
script_path="$link_target"
else
script_path="$(dirname "$script_path")/$link_target"
fi
done
cd "$(dirname "$script_path")" && pwd
}
# Main function
main() {
echo "========================================"
echo " ${PROGRAM} Installer v${VERSION}"
echo "========================================"
echo ""
local script_dir="$(get_script_dir)"
cd "$script_dir" || {
error "Cannot change to script directory: $script_dir"
exit 1
}
if [ ! -f "Makefile" ] || [ ! -f "main.c" ]; then
error "Source files not found in current directory."
error "Please run this script from the carbon2cocoa source directory."
exit 1
fi
detect_os
case "${OS_TYPE}" in
macos|linux|freebsd) ;;
*)
warning "This OS (${OS_PRETTY}) has not been tested."
if ! prompt_yn "Continue anyway?" "n"; then
exit 0
fi
;;
esac
echo ""
if $UNINSTALL; then
uninstall_files
exit 0
fi
check_prereqs
echo ""
build_project
echo ""
echo "Installation summary:"
echo " Binary: ${BINDIR}/${PROGRAM}"
echo " Man page: ${MANDIR}/${PROGRAM}.1"
echo ""
if prompt_yn "Proceed with installation?" "y"; then
install_files
else
echo ""
info "Installation cancelled."
echo "The binary has been built. You can:"
echo " - Run it directly: ./${PROGRAM} --help"
echo " - Install manually: sudo make install PREFIX=${PREFIX}"
fi
}
main "$@"