-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·622 lines (539 loc) · 19.2 KB
/
install.sh
File metadata and controls
executable file
·622 lines (539 loc) · 19.2 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env bash
###############################################################################
# NAME : install.sh
# DESCRIPTION : Bash environment dotfiles installer with install/update/uninstall
# modes. Requires common_core library at system location.
# AUTHOR : Adam Compton
# DATE CREATED : 2024-12-16
###############################################################################
# EDIT HISTORY:
# DATE | EDITED BY | DESCRIPTION
# ------------|----------------|------------------------------------------------
# 2024-12-16 | Adam Compton | Initial creation
# 2025-01-09 | Adam Compton | Consolidated from SetupBash.sh and menu_tasks.sh
# 2026-01-19 | Adam Compton | Hardened contracts, fixed preflight logic,
# | logging fallbacks, and common_core API checks
###############################################################################
set -uo pipefail
IFS=$'\n\t'
#===============================================================================
# Globals
#===============================================================================
: "${QUIET:=false}"
: "${PASS:=0}"
: "${FAIL:=1}"
#===============================================================================
# Logging Fallbacks
#===============================================================================
if ! declare -F info > /dev/null 2>&1; then
function info() { [[ "${QUIET}" == "true" ]] && return 0; printf '[INFO ] %s\n' "${*}" >&2; }
fi
if ! declare -F warn > /dev/null 2>&1; then
function warn() { printf '[WARN ] %s\n' "${*}" >&2; }
fi
if ! declare -F error > /dev/null 2>&1; then
function error() { printf '[ERROR] %s\n' "${*}" >&2; }
fi
if ! declare -F debug > /dev/null 2>&1; then
function debug() { [[ "${QUIET}" == "true" ]] && return 0; printf '[DEBUG] %s\n' "${*}" >&2; }
fi
if ! declare -F pass > /dev/null 2>&1; then
function pass() { [[ "${QUIET}" == "true" ]] && return 0; printf '[PASS ] %s\n' "${*}" >&2; }
fi
if ! declare -F fail > /dev/null 2>&1; then
function fail() { printf '[FAIL ] %s\n' "${*}" >&2; }
fi
#===============================================================================
# Constants
#===============================================================================
readonly SCRIPT_NAME="${0##*/}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
# Read version from project VERSION file
if [[ -f "${SCRIPT_DIR}/VERSION" ]]; then
VERSION="$(< "${SCRIPT_DIR}/VERSION")"
else
VERSION="unknown"
fi
readonly VERSION
# System paths
readonly COMMON_CORE_DIR="${HOME}/.config/bash/lib/common_core"
readonly COMMON_CORE_UTIL="${COMMON_CORE_DIR}/util.sh"
readonly BASH_DIR="${HOME}/.config/bash"
readonly BASH_LOG_DIR="${BASH_DIR}/log"
readonly DATA_DIR="${HOME}/DATA"
readonly DOTFILES_DIR="${SCRIPT_DIR}/dotfiles"
#===============================================================================
# Dotfile Lists
#===============================================================================
readonly -a COMMON_DOT_FILES=(
"bashrc"
"profile"
"bash_profile"
"tmux.conf"
"screenrc_v4"
"screenrc_v5"
"inputrc"
"vimrc"
"wgetrc"
"curlrc"
)
readonly -a BASH_DOT_FILES=(
"bash.path.sh"
"bash.env.sh"
"path.env.sh"
"bash.funcs.sh"
"bash.aliases.sh"
"screen.aliases.sh"
"tmux.aliases.sh"
"bash.prompt.sh"
"bash.prompt_funcs.sh"
"bash-preexec.sh"
"combined.history.sh"
"ssh.aliases.sh"
"bash.visuals.sh"
)
readonly -a REQUIRED_DIRECTORIES=(
"${DATA_DIR}/LOGS"
"${BASH_DIR}"
"${BASH_LOG_DIR}"
)
readonly -a RECOMMENDED_TOOLS=(
"eza"
"fzf"
"ncat"
"freeze"
"bat"
"duf"
"btop"
)
#===============================================================================
# Capability flags (populated in preflight)
#===============================================================================
HAS_SHA256_TOOL="false"
SHA256_TOOL="" # "sha256sum" or "shasum"
###############################################################################
# usage
#------------------------------------------------------------------------------
# Purpose : Display help message with usage information
# Usage : usage
# Returns : Always returns 0
###############################################################################
function usage() {
cat << EOF
${SCRIPT_NAME} v${VERSION} - Bash environment dotfiles manager
USAGE:
${SCRIPT_NAME} [COMMAND] [OPTIONS]
COMMANDS:
install Install dotfiles and create directories (default)
update Update only changed dotfiles (checksum comparison)
uninstall Restore original dotfiles from backups
OPTIONS:
-h, --help Show this help message
-v, --version Show version
-q, --quiet Suppress non-error output
-f, --force Force overwrite without backup comparison
--skip-tools Skip recommended tool checks during install
EXAMPLES:
${SCRIPT_NAME} # Install (default)
${SCRIPT_NAME} install # Install dotfiles
${SCRIPT_NAME} update # Update changed files only
${SCRIPT_NAME} uninstall # Restore backups
REQUIREMENTS:
- Bash 4.0+
- common_core library at: ${COMMON_CORE_DIR}
NOTES:
- If neither sha256sum nor shasum exists, update mode will treat all files as "different"
and overwrite targets (a warning will be emitted).
EOF
}
###############################################################################
# preflight_checks
#------------------------------------------------------------------------------
# Purpose : Verify system requirements before installation
# Usage : preflight_checks
# Returns : Number of errors found (0 = all checks passed)
# Checks : Bash version, HOME variable, common_core installation
###############################################################################
function preflight_checks() {
local errors=0
# Bash version
if [[ -z "${BASH_VERSION:-}" ]]; then
fail "This script must be run under Bash."
((errors++))
elif [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
fail "Bash 4.0+ required. Current: ${BASH_VERSION}"
((errors++))
fi
# HOME sanity
if [[ -z "${HOME:-}" ]]; then
fail "HOME environment variable not set."
((errors++))
elif [[ ! -d "${HOME}" ]]; then
fail "HOME does not exist or is not a directory: ${HOME}"
((errors++))
fi
# common_core presence
if [[ ! -d "${COMMON_CORE_DIR}" ]]; then
fail "common_core library not found at: ${COMMON_CORE_DIR}"
fail "Install common_core first (example):"
fail " git clone https://github.com/tatanus/common_core.git"
fail " then run its installer / copy into: ${COMMON_CORE_DIR}"
((errors++))
elif [[ ! -f "${COMMON_CORE_UTIL}" ]]; then
fail "common_core util.sh not found at: ${COMMON_CORE_UTIL}"
((errors++))
fi
# Repo layout
if [[ ! -d "${DOTFILES_DIR}" ]]; then
fail "Dotfiles directory not found: ${DOTFILES_DIR}"
((errors++))
fi
# Capability detection: SHA-256
if command -v sha256sum >/dev/null 2>&1; then
HAS_SHA256_TOOL="true"
SHA256_TOOL="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
HAS_SHA256_TOOL="true"
SHA256_TOOL="shasum"
else
HAS_SHA256_TOOL="false"
SHA256_TOOL=""
warn "No sha256sum or shasum found; update mode will overwrite without checksum equality checks."
fi
[[ "${errors}" -eq 0 ]]
}
###############################################################################
# load_common_core
#------------------------------------------------------------------------------
# Purpose : Source the common_core utility library
# Usage : load_common_core
# Returns : 0 on success, 1 on failure
# Requires : COMMON_CORE_UTIL variable set
###############################################################################
function load_common_core() {
# Force clean bootstrap even if already sourced in this shell
unset COMMON_CORE_INITIALIZED 2>/dev/null || true
# shellcheck source=/dev/null
if ! source "${COMMON_CORE_UTIL}"; then
fail "Failed to source common_core: ${COMMON_CORE_UTIL}"
return 1
fi
# Verify required symbols exist (older common_core versions can differ)
local required_funcs=(
cmd::exists
file::copy
file::restore_old_backup
info warn debug pass fail
)
local f
for f in "${required_funcs[@]}"; do
if ! declare -F "${f}" >/dev/null 2>&1; then
fail "common_core is missing required function: ${f}"
return 1
fi
done
pass "Loaded common_core utilities"
return 0
}
###############################################################################
# setup_directories
#------------------------------------------------------------------------------
# Purpose : Create required directories for bash configuration
# Usage : setup_directories
# Returns : Number of directories that failed to create
# Requires : REQUIRED_DIRECTORIES array, common_core logging functions
###############################################################################
function setup_directories() {
info "Creating required directories..."
local failed=0
local dir=""
for dir in "${REQUIRED_DIRECTORIES[@]}"; do
if [[ -d "${dir}" ]]; then
debug "Directory exists: ${dir}"
elif mkdir -p "${dir}"; then
pass "Created: ${dir}"
else
fail "Failed to create: ${dir}"
((failed++))
fi
done
[[ "${failed}" -eq 0 ]]
}
###############################################################################
# check_recommended_tools
#------------------------------------------------------------------------------
# Purpose : Check if recommended CLI tools are installed
# Usage : check_recommended_tools
# Returns : Always returns 0 (tools are optional)
# Requires : RECOMMENDED_TOOLS array, cmd::exists from common_core
###############################################################################
function check_recommended_tools() {
info "Checking recommended tools..."
local tool=""
for tool in "${RECOMMENDED_TOOLS[@]}"; do
if cmd::exists "${tool}"; then
pass "Found: ${tool}"
else
warn "Missing: ${tool} (optional)"
fi
done
return 0
}
###############################################################################
# files_differ
#------------------------------------------------------------------------------
# Purpose : Compare two files using SHA-256 checksums
# Usage : files_differ <source_file> <dest_file>
# Arguments:
# $1 : Source file path
# $2 : Destination file path
# Returns : 0 if files differ (or dest missing), 1 if identical
# Requires : sha256sum or shasum command
###############################################################################
function files_differ() {
local src="$1"
local dest="$2"
[[ ! -f "${dest}" ]] && return 0
# If no SHA tool, treat as different (caller may overwrite)
if [[ "${HAS_SHA256_TOOL}" != "true" ]]; then
return 0
fi
local src_sum="" dest_sum=""
if [[ "${SHA256_TOOL}" == "sha256sum" ]]; then
src_sum="$(sha256sum "${src}" 2>/dev/null | cut -d' ' -f1)"
dest_sum="$(sha256sum "${dest}" 2>/dev/null | cut -d' ' -f1)"
elif [[ "${SHA256_TOOL}" == "shasum" ]]; then
src_sum="$(shasum -a 256 "${src}" 2>/dev/null | cut -d' ' -f1)"
dest_sum="$(shasum -a 256 "${dest}" 2>/dev/null | cut -d' ' -f1)"
else
# Defensive fallback
return 0
fi
[[ "${src_sum}" != "${dest_sum}" ]]
}
###############################################################################
# cmd_install
#------------------------------------------------------------------------------
# Purpose : Install dotfiles and create required directories
# Usage : cmd_install [skip_tools]
# Arguments:
# $1 : "true" to skip tool checks (optional, default: "false")
# Returns : 0 on success, 1 on failure
# Requires : common_core file::copy function
###############################################################################
function cmd_install() {
local skip_tools="${1:-false}"
info "Starting installation..."
setup_directories || return 1
info "Installing common dotfiles to ${HOME}..."
local file="" src="" dest=""
for file in "${COMMON_DOT_FILES[@]}"; do
src="${DOTFILES_DIR}/${file}"
dest="${HOME}/.${file}"
if [[ ! -f "${src}" ]]; then
warn "Source not found: ${src}"
continue
fi
file::copy "${src}" "${dest}"
done
info "Installing bash dotfiles to ${BASH_DIR}..."
for file in "${BASH_DOT_FILES[@]}"; do
src="${DOTFILES_DIR}/${file}"
dest="${BASH_DIR}/${file}"
if [[ ! -f "${src}" ]]; then
warn "Source not found: ${src}"
continue
fi
file::copy "${src}" "${dest}"
done
# Configure screen based on version
setup_screenrc || return 1
if [[ "${skip_tools}" != "true" ]]; then
check_recommended_tools
fi
if [[ -f "${HOME}/.bashrc" ]]; then
info "To apply changes, run: source ~/.bashrc"
fi
pass "Installation complete!"
return 0
}
###############################################################################
# cmd_update
#------------------------------------------------------------------------------
# Purpose : Update only changed dotfiles (checksum comparison)
# Usage : cmd_update
# Returns : 0 on success, 1 on failure
# Requires : common_core file::copy function, files_differ function
###############################################################################
function cmd_update() {
info "Checking for updates..."
local updated=0
local file="" src="" dest=""
for file in "${COMMON_DOT_FILES[@]}"; do
src="${DOTFILES_DIR}/${file}"
dest="${HOME}/.${file}"
[[ ! -f "${src}" ]] && continue
if files_differ "${src}" "${dest}"; then
info "Updating: ${dest}"
file::copy "${src}" "${dest}"
((updated++))
else
debug "Unchanged: ${dest}"
fi
done
for file in "${BASH_DOT_FILES[@]}"; do
src="${DOTFILES_DIR}/${file}"
dest="${BASH_DIR}/${file}"
[[ ! -f "${src}" ]] && continue
if files_differ "${src}" "${dest}"; then
info "Updating: ${dest}"
file::copy "${src}" "${dest}"
((updated++))
else
debug "Unchanged: ${dest}"
fi
done
if [[ "${updated}" -eq 0 ]]; then
info "All dotfiles are up to date."
else
pass "Updated ${updated} file(s)."
info "To apply changes, run: source ~/.bashrc"
fi
# Configure screen based on version
setup_screenrc || return 1
return 0
}
###############################################################################
# cmd_uninstall
#------------------------------------------------------------------------------
# Purpose : Restore original dotfiles from backups
# Usage : cmd_uninstall
# Returns : 0 always
# Requires : common_core file::restore_old_backup function
###############################################################################
function cmd_uninstall() {
info "Restoring original dotfiles..."
local restored=0
local file="" target=""
for file in "${COMMON_DOT_FILES[@]}"; do
target="${HOME}/.${file}"
if [[ -f "${target}" ]]; then
file::restore_old_backup "${target}" && ((restored++))
fi
done
for file in "${BASH_DOT_FILES[@]}"; do
target="${BASH_DIR}/${file}"
if [[ -f "${target}" ]]; then
file::restore_old_backup "${target}" && ((restored++))
fi
done
if [[ "${restored}" -eq 0 ]]; then
info "No backups found to restore."
else
pass "Restored ${restored} file(s) from backups."
fi
info "To apply changes, run: source ~/.bashrc"
return 0
}
###############################################################################
# setup_screenrc
#------------------------------------------------------------------------------
# Purpose : Install correct ~/.screenrc based on installed screen major version
# Usage : setup_screenrc
# Returns : 0 on success or if screen not installed, 1 on error
###############################################################################
function setup_screenrc() {
if ! cmd::exists screen; then
warn "GNU screen not found; skipping ~/.screenrc setup."
return 0
fi
local version major src dest
version="$(screen --version 2>/dev/null | awk '{print $NF}')"
major="${version%%.*}"
dest="${HOME}/.screenrc"
case "${major}" in
4)
src="${HOME}/.screenrc_v4"
;;
5)
src="${HOME}/.screenrc_v5"
;;
*)
warn "Unsupported screen version: ${version}; skipping ~/.screenrc setup."
return 0
;;
esac
if [[ ! -f "${src}" ]]; then
fail "Expected screen config not found: ${src}"
return 1
fi
info "Installing screen config for screen ${version} -> ${dest}"
file::copy "${src}" "${dest}"
pass "Configured ~/.screenrc for screen ${major}.x"
return 0
}
###############################################################################
# main
#------------------------------------------------------------------------------
# Purpose : Main entry point - parse arguments and execute commands
# Usage : main "$@"
# Returns : 0 on success, 1 on failure
###############################################################################
function main() {
local command="install"
local skip_tools="false"
local force="false"
while [[ $# -gt 0 ]]; do
case "$1" in
install|update|uninstall)
command="$1"
shift
;;
-h|--help)
usage
return 0
;;
-v|--version)
echo "${SCRIPT_NAME} v${VERSION}"
return 0
;;
-q|--quiet)
QUIET="true"
shift
;;
-f|--force)
force="true"
shift
;;
--skip-tools)
skip_tools="true"
shift
;;
*)
fail "Unknown option: $1"
usage >&2
return 1
;;
esac
done
# Preflight checks (fixed: correct boolean handling)
preflight_checks || return 1
# Load common_core and validate API
load_common_core || return 1
# NOTE: If your common_core file::copy supports a force flag, you can
# standardize that via an env var or wrapper. This script does not assume it.
if [[ "${force}" == "true" ]]; then
warn "--force was requested. If common_core honors a force mode via env/flags, ensure it is enabled there."
fi
case "${command}" in
install) cmd_install "${skip_tools}" ;;
update) cmd_update ;;
uninstall) cmd_uninstall ;;
*)
fail "Unknown command: ${command}"
return 1
;;
esac
}
main "$@"