This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·401 lines (338 loc) · 10.9 KB
/
deploy.sh
File metadata and controls
executable file
·401 lines (338 loc) · 10.9 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
#!/usr/bin/env bash
#
# BatteryGuardian Deployment Script
# ----------------------------------------
# Deploys BatteryGuardian scripts and configurations to XDG base directories
# Author: Cyber-Syntax
# License: BSD 3-Clause License
# Ensure script exits on errors, undefined variables, and pipe failures
set -o errexit -o nounset -o pipefail
# Colors for output
readonly BG_RED='\033[0;31m'
readonly BG_GREEN='\033[0;32m'
readonly BG_YELLOW='\033[0;33m'
readonly BG_BLUE='\033[0;34m'
readonly BG_NC='\033[0m' # No Color
# Default locations based on XDG Base Directory Specification
readonly XDG_BIN_HOME="${XDG_BIN_HOME:-$HOME/.local/bin}"
readonly XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
readonly XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
readonly XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
# Repo and installation directories
# Get the absolute path to the repository directory
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
BG_REPO_DIR="$(pwd)"
readonly BG_REPO_DIR
cd - > /dev/null || exit 1
readonly BG_INSTALL_DIR="${XDG_DATA_HOME}/battery-guardian"
readonly BG_BIN_DIR="${XDG_BIN_HOME}"
readonly BG_CONFIG_DIR="${XDG_CONFIG_HOME}/battery-guardian"
readonly BG_STATE_DIR="${XDG_STATE_HOME}/battery-guardian"
# Define script directories and their installation paths
declare -A BG_SCRIPT_DIRS
BG_SCRIPT_DIRS["src"]="${BG_INSTALL_DIR}/src"
BG_SCRIPT_DIRS["configs"]="${BG_CONFIG_DIR}"
# Paths for runtime state
readonly BG_LOGS_DIR="${BG_STATE_DIR}/logs"
# Print usage information
bg_print_usage() {
echo -e "${BG_BLUE}BatteryGuardian - Deployment Script${BG_NC}"
echo
echo "Usage: $0 [OPTIONS]"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " -d, --dev Install from current directory (development mode)"
echo " -m, --main Install from main branch (default)"
echo " -f, --force Force installation (overwrite existing files)"
echo " -v, --verbose Verbose output"
echo " -t, --target DIR Specify custom installation directory"
echo
echo "Example:"
echo " $0 --dev # Install from current directory"
echo " $0 --main # Install from main branch"
echo " $0 --target ~/scripts # Install to custom directory"
}
# Log messages with colors based on type
bg_log() {
local level="$1"
local message="$2"
local timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
case "$level" in
"DEBUG")
if [[ "${BG_VERBOSE}" == "true" ]]; then
echo -e "${timestamp} ${BG_BLUE}[DEBUG]${BG_NC} $message"
fi
;;
"INFO")
if [[ "${BG_VERBOSE}" == "true" ]]; then
echo -e "${timestamp} ${BG_BLUE}[INFO]${BG_NC} $message"
fi
;;
"SUCCESS")
echo -e "${timestamp} ${BG_GREEN}[SUCCESS]${BG_NC} $message"
;;
"WARNING")
echo -e "${timestamp} ${BG_YELLOW}[WARNING]${BG_NC} $message"
;;
"ERROR")
echo -e "${timestamp} ${BG_RED}[ERROR]${BG_NC} $message" >&2
;;
esac
}
# Log level wrappers
bg_debug() { bg_log "DEBUG" "$1"; }
bg_info() { bg_log "INFO" "$1"; }
bg_success() { bg_log "SUCCESS" "$1"; }
bg_warn() { bg_log "WARNING" "$1"; }
bg_error() { bg_log "ERROR" "$1"; }
# Check if a command exists
bg_command_exists() {
command -v "$1" &> /dev/null
}
# Check required dependencies are installed
bg_check_dependencies() {
local missing_deps=false
for cmd in git cp chmod; do
if ! bg_command_exists "$cmd"; then
bg_error "Required command not found: $cmd"
missing_deps=true
fi
done
if [[ "$missing_deps" == "true" ]]; then
bg_error "Missing required dependencies. Please install them and try again."
return 1
fi
return 0
}
# Create directory if it doesn't exist
bg_ensure_dir_exists() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
bg_debug "Creating directory: $dir"
mkdir -p "$dir" || {
bg_error "Failed to create directory: $dir"
return 1
}
fi
return 0
}
# Copy script with proper permissions
bg_copy_script() {
local src="$1"
local dest="$2"
local base_dest_dir
# Validate paths - prevent directory traversal
if [[ "$src" == *".."* ]] || [[ "$dest" == *".."* ]]; then
bg_error "Path contains forbidden pattern: .."
return 1
fi
base_dest_dir="$(dirname "$dest")"
bg_ensure_dir_exists "$base_dest_dir" || return 1
if [[ -f "$dest" && "${BG_FORCE}" != "true" ]]; then
bg_warn "File already exists: $dest (use --force to overwrite)"
return 0
fi
cp "$src" "$dest" || {
bg_error "Failed to copy $src to $dest"
return 1
}
if [[ "$src" == *.sh ]]; then
chmod +x "$dest" || {
bg_error "Failed to set executable permission on $dest"
return 1
}
fi
bg_info "Installed: $dest"
return 0
}
# Create symbolic links in bin directory
bg_create_symlinks() {
bg_ensure_dir_exists "$BG_BIN_DIR" || return 1
# Find all executable scripts
find "$BG_INSTALL_DIR" -type f -name "*.sh" | while read -r script; do
local script_name
local link_path
script_name="$(basename "$script" .sh)"
link_path="$BG_BIN_DIR/$script_name"
# Skip if link exists and not forcing
if [[ -L "$link_path" || -e "$link_path" ]] && [[ "${BG_FORCE}" != "true" ]]; then
bg_warn "Symlink already exists: $link_path (use --force to overwrite)"
continue
fi
# Remove existing symlink or file if it exists
if [[ -L "$link_path" || -e "$link_path" ]] && [[ "${BG_FORCE}" == "true" ]]; then
rm -f "$link_path" || {
bg_error "Failed to remove existing link: $link_path"
continue
}
fi
ln -sf "$script" "$link_path" || {
bg_error "Failed to create symlink: $link_path -> $script"
continue
}
bg_info "Created symlink: $link_path -> $script"
done
}
# Install from current directory (development mode)
bg_install_from_dev() {
bg_info "Installing from development directory..."
# Create installation directories
bg_ensure_dir_exists "$BG_INSTALL_DIR"
bg_ensure_dir_exists "$BG_CONFIG_DIR"
bg_ensure_dir_exists "$BG_STATE_DIR"
bg_ensure_dir_exists "$BG_LOGS_DIR"
# Install scripts by category
for dir in "${!BG_SCRIPT_DIRS[@]}"; do
local source_dir="${BG_REPO_DIR}/${dir}"
local target_dir="${BG_SCRIPT_DIRS[$dir]}"
if [[ -d "$source_dir" ]]; then
bg_ensure_dir_exists "$target_dir"
# Find all shell scripts in this category
find "$source_dir" -type f -name "*.sh" | while read -r script; do
local script_name
script_name="$(basename "$script")"
bg_copy_script "$script" "${target_dir}/${script_name}"
done
else
bg_warn "Directory not found: $source_dir"
fi
done
# Create symlinks for easy access
bg_create_symlinks
bg_success "Development installation complete!"
bg_info "Scripts installed to: $BG_INSTALL_DIR"
bg_info "Symlinks created in: $BG_BIN_DIR"
}
# Install from main branch by cloning directly to the installation directory
bg_install_from_main() {
bg_info "Installing from main branch..."
# Check if git is available
if ! bg_command_exists git; then
bg_error "Git is not installed. Please install git first."
exit 1
fi
# Clean installation directory if it exists and force is enabled
if [[ -d "$BG_INSTALL_DIR" ]]; then
if [[ "${BG_FORCE}" == "true" ]]; then
bg_info "Removing existing installation directory..."
rm -rf "$BG_INSTALL_DIR"
else
bg_warn "Installation directory already exists: $BG_INSTALL_DIR"
bg_warn "Use --force to reinstall from main branch"
exit 1
fi
fi
# Create parent directories
bg_ensure_dir_exists "$(dirname "$BG_INSTALL_DIR")"
# Clone directly to the installation directory
local repo_url="https://github.com/cyber-syntax/BatteryGuardian.git"
bg_info "Cloning repository to: $BG_INSTALL_DIR"
if ! git clone --depth 1 --branch main "$repo_url" "$BG_INSTALL_DIR"; then
bg_error "Failed to clone repository."
exit 1
fi
# Reorganize files if needed - if the repository structure doesn't match our expected structure
# Check if we have the expected directory structure
local has_expected_structure=true
for dir in "${!BG_SCRIPT_DIRS[@]}"; do
if [[ ! -d "$BG_INSTALL_DIR/$dir" ]]; then
has_expected_structure=false
break
fi
done
if [[ "$has_expected_structure" == "false" ]]; then
bg_info "Repository structure differs from expected. Reorganizing..."
# Create a temporary directory for reorganization
local temp_dir
temp_dir="$(mktemp -d)"
# Look for script files in the repository and move them to the appropriate category
find "$BG_INSTALL_DIR" -type f -name "*.sh" | while read -r script; do
local script_name
script_name="$(basename "$script")"
local script_path
script_path="$(dirname "$script")"
# Determine script category based on BatteryGuardian structure
local category="src"
# Check if it's a config file
if [[ "$script_name" == "defaults.sh" || "$script_path" == *"/configs"* ]]; then
category="configs"
# Check if it's a test file
elif [[ "$script_name" == *"test"* || "$script_name" == *".bats" || "$script_path" == *"/tests"* ]]; then
category="tests"
fi
# Create category directory in temp dir
bg_ensure_dir_exists "$temp_dir/$category"
# Copy script to temp directory
cp "$script" "$temp_dir/$category/"
chmod +x "$temp_dir/$category/$script_name"
done
# Clean installation directory except .git
find "$BG_INSTALL_DIR" -mindepth 1 -not -path "$BG_INSTALL_DIR/.git*" -delete
# Move reorganized files back to installation directory
cp -r "$temp_dir/"* "$BG_INSTALL_DIR/"
# Clean up
rm -rf "$temp_dir"
fi
# Create necessary directories
bg_ensure_dir_exists "$BG_CONFIG_DIR"
bg_ensure_dir_exists "$BG_STATE_DIR"
bg_ensure_dir_exists "$BG_LOGS_DIR"
# Create symlinks for easy access
bg_create_symlinks
bg_success "Main branch installation complete!"
bg_info "Scripts installed to: $BG_INSTALL_DIR"
bg_info "Symlinks created in: $BG_BIN_DIR"
}
# Parse command-line arguments
BG_MODE="main" # Default mode is main branch
BG_FORCE="false"
BG_VERBOSE="false"
BG_CUSTOM_INSTALL_DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
bg_print_usage
exit 0
;;
-d|--dev)
BG_MODE="dev"
shift
;;
-m|--main)
BG_MODE="main"
shift
;;
-f|--force)
BG_FORCE="true"
shift
;;
-v|--verbose)
BG_VERBOSE="true"
shift
;;
-t|--target)
if [[ -n "$2" ]]; then
BG_CUSTOM_INSTALL_DIR="$2"
BG_INSTALL_DIR="$BG_CUSTOM_INSTALL_DIR"
shift 2
else
bg_error "Option --target requires a directory argument."
exit 1
fi
;;
*)
bg_error "Unknown option: $1"
bg_print_usage
exit 1
;;
esac
done
# Run installation based on selected mode
if [[ "$BG_MODE" == "dev" ]]; then
bg_install_from_dev
else
bg_install_from_main
fi
exit 0