-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·488 lines (416 loc) · 13.3 KB
/
install.sh
File metadata and controls
executable file
·488 lines (416 loc) · 13.3 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
#!/bin/bash
# GlobalProtect Automation - Installation Script
# Installs the GlobalProtect automation tools following XDG standards
set -euo pipefail
# Script info
SCRIPT_NAME="GlobalProtect Automation Installer"
VERSION="1.0.0"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Installation paths following XDG Base Directory Specification
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}"
XDG_BIN_HOME="${HOME}/.local/bin"
APP_NAME="globalprotect-bot"
CONFIG_DIR="${XDG_CONFIG_HOME}/${APP_NAME}"
DATA_DIR="${XDG_DATA_HOME}/${APP_NAME}"
BIN_DIR="${XDG_BIN_HOME}"
# Source directory (where this script is located)
SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Logging function
log() {
echo -e "${1}" >&2
}
log_info() {
log "${BLUE}[INFO]${NC} ${1}"
}
log_success() {
log "${GREEN}[SUCCESS]${NC} ${1}"
}
log_warning() {
log "${YELLOW}[WARNING]${NC} ${1}"
}
log_error() {
log "${RED}[ERROR]${NC} ${1}"
}
# Show help
show_help() {
cat << EOF
${SCRIPT_NAME} v${VERSION}
USAGE:
${0} [OPTIONS]
OPTIONS:
-h, --help Show this help message
--uninstall Uninstall GlobalProtect automation tools
--system Install system-wide (requires sudo) [NOT IMPLEMENTED]
--force Force installation, overwrite existing files
--dry-run Show what would be done without making changes
DESCRIPTION:
Installs GlobalProtect automation tools to standard locations:
Binaries: ${BIN_DIR}
Config: ${CONFIG_DIR}
Data/Logs: ${DATA_DIR}
The installer will:
- Create necessary directories
- Install scripts as 'globalconnect', 'gc-connect', 'gp-test'
- Set up default configuration files
- Automatically configure PATH in your shell profile (with permission)
EXAMPLES:
${0} # Standard installation
${0} --force # Force overwrite existing installation
${0} --dry-run # Preview installation
${0} --uninstall # Remove installation
EOF
}
# Parse command line arguments
UNINSTALL=false
SYSTEM_INSTALL=false
FORCE=false
DRY_RUN=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
--uninstall)
UNINSTALL=true
shift
;;
--system)
SYSTEM_INSTALL=true
shift
;;
--force)
FORCE=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
-*)
log_error "Unknown option $1"
show_help
exit 1
;;
*)
log_error "Unexpected argument: $1"
show_help
exit 1
;;
esac
done
# Check if running on macOS
check_macos() {
if [[ "$(uname)" != "Darwin" ]]; then
log_error "This script is designed for macOS only"
exit 1
fi
}
# Create directory with dry-run support
create_dir() {
local dir="$1"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would create directory: ${dir}"
else
mkdir -p "${dir}"
log_success "Created directory: ${dir}"
fi
}
# Copy file with dry-run support
copy_file() {
local src="$1"
local dest="$2"
local executable="${3:-false}"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would copy: ${src} → ${dest}"
if [[ "${executable}" == "true" ]]; then
log_info "Would make executable: ${dest}"
fi
else
cp "${src}" "${dest}"
if [[ "${executable}" == "true" ]]; then
chmod +x "${dest}"
fi
log_success "Copied: ${src} → ${dest}"
fi
}
# Remove file with dry-run support
remove_file() {
local file="$1"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would remove: ${file}"
else
if [[ -f "${file}" ]]; then
rm "${file}"
log_success "Removed: ${file}"
fi
fi
}
# Remove directory with dry-run support
remove_dir() {
local dir="$1"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would remove directory: ${dir}"
else
if [[ -d "${dir}" ]]; then
rm -rf "${dir}"
log_success "Removed directory: ${dir}"
fi
fi
}
# Check if source files exist
check_source_files() {
log_info "Checking source files..."
local required_files=(
"bin/globalconnect"
"bin/gc-connect"
"bin/gp-test"
"lib/yaml-loader.sh"
"config/environment.yaml.template"
)
local missing_files=()
for file in "${required_files[@]}"; do
if [[ ! -f "${SOURCE_DIR}/${file}" ]]; then
missing_files+=("${file}")
fi
done
if [[ ${#missing_files[@]} -gt 0 ]]; then
log_error "Missing required source files:"
printf " - %s\n" "${missing_files[@]}"
log_error "Please run this script from the project root directory"
exit 1
fi
log_success "All source files found"
}
# Detect user's shell and config file
detect_shell_config() {
local shell_name=$(basename "$SHELL")
local config_file=""
case "$shell_name" in
zsh)
if [[ -f "${HOME}/.zshrc" ]]; then
config_file="${HOME}/.zshrc"
else
config_file="${HOME}/.zshrc" # Will be created
fi
;;
bash)
if [[ -f "${HOME}/.bashrc" ]]; then
config_file="${HOME}/.bashrc"
elif [[ -f "${HOME}/.bash_profile" ]]; then
config_file="${HOME}/.bash_profile"
else
config_file="${HOME}/.bashrc" # Will be created
fi
;;
fish)
config_file="${HOME}/.config/fish/config.fish"
;;
*)
# Generic fallback
if [[ -f "${HOME}/.profile" ]]; then
config_file="${HOME}/.profile"
else
config_file="${HOME}/.profile" # Will be created
fi
;;
esac
echo "$config_file"
}
# Add PATH to shell configuration
add_to_shell_config() {
local config_file="$1"
local path_export="export PATH=\"${BIN_DIR}:\$PATH\""
local comment="# Added by GlobalProtect Automation installer"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would add to ${config_file}:"
log_info " ${comment}"
log_info " ${path_export}"
return 0
fi
# Check if PATH is already configured
if [[ -f "$config_file" ]] && grep -q "${BIN_DIR}" "$config_file"; then
log_info "PATH already configured in ${config_file}"
return 0
fi
# Create config file directory if needed
mkdir -p "$(dirname "$config_file")"
# Add the PATH export
{
echo ""
echo "$comment"
echo "$path_export"
} >> "$config_file"
log_success "Added PATH configuration to ${config_file}"
log_info "Restart your terminal or run: source ${config_file}"
return 0
}
# Check PATH configuration and offer to fix it
check_path() {
log_info "Checking PATH configuration..."
if echo ":${PATH}:" | grep -q ":${BIN_DIR}:"; then
log_success "✓ ${BIN_DIR} is in PATH"
return 0
fi
log_warning "⚠ ${BIN_DIR} is not in PATH"
# Detect shell config file
local config_file=$(detect_shell_config)
local shell_name=$(basename "$SHELL")
log_info "Detected shell: ${shell_name}"
log_info "Config file: ${config_file}"
# Ask user if they want automatic configuration
if [[ "${DRY_RUN}" != "true" ]]; then
echo
echo -n "Add ${BIN_DIR} to PATH in ${config_file}? [Y/n]: "
read -r add_to_path
if [[ "${add_to_path}" =~ ^[Nn]$ ]]; then
log_info "Skipping automatic PATH configuration."
log_info ""
log_info "Manual setup instructions:"
log_info "Add this to your shell profile (${config_file}):"
log_info " export PATH=\"${BIN_DIR}:\$PATH\""
log_info ""
log_info "For immediate use, run:"
log_info " export PATH=\"${BIN_DIR}:\$PATH\""
else
if add_to_shell_config "$config_file"; then
log_info ""
log_info "To use commands immediately in this terminal:"
log_info " export PATH=\"${BIN_DIR}:\$PATH\""
log_info ""
log_info "Or restart your terminal to pick up the new PATH automatically."
else
log_error "Failed to add PATH configuration"
log_info "Please add manually: export PATH=\"${BIN_DIR}:\$PATH\""
fi
fi
else
log_info "Would ask to add ${BIN_DIR} to ${config_file}"
fi
}
# Install configuration files
install_configs() {
log_info "Installing configuration files..."
# Install YAML template
local yaml_template="${CONFIG_DIR}/environment.yaml.template"
copy_file "${SOURCE_DIR}/config/environment.yaml.template" "${yaml_template}"
# Create default environment.yaml if it doesn't exist
local yaml_config="${CONFIG_DIR}/environment.yaml"
if [[ ! -f "${yaml_config}" ]] || [[ "${FORCE}" == "true" ]]; then
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Would create default config: ${yaml_config}"
else
cp "${yaml_template}" "${yaml_config}"
log_success "Created default config: ${yaml_config}"
log_info "Edit ${yaml_config} to customize for your organization"
fi
else
log_info "Config file already exists: ${yaml_config}"
log_info "Use --force to overwrite or edit manually"
fi
# Install legacy config template (for backward compatibility)
local conf_template="${CONFIG_DIR}/globalconnect-config.conf.template"
if [[ -f "${SOURCE_DIR}/config/globalconnect-config.conf" ]]; then
copy_file "${SOURCE_DIR}/config/globalconnect-config.conf" "${conf_template}"
fi
}
# Install scripts
install_scripts() {
log_info "Installing scripts..."
# Install main automation script as 'globalconnect'
copy_file "${SOURCE_DIR}/bin/globalconnect" "${BIN_DIR}/globalconnect" true
# Install wrapper script as 'gc-connect'
copy_file "${SOURCE_DIR}/bin/gc-connect" "${BIN_DIR}/gc-connect" true
# Install test script as 'gp-test'
copy_file "${SOURCE_DIR}/bin/gp-test" "${BIN_DIR}/gp-test" true
# Install yaml-loader as library
copy_file "${SOURCE_DIR}/lib/yaml-loader.sh" "${DATA_DIR}/yaml-loader.sh"
}
# Main installation
install() {
log_info "Starting GlobalProtect Automation installation..."
log_info "Installation mode: ${DRY_RUN:+DRY RUN }${FORCE:+FORCE }${SYSTEM_INSTALL:+SYSTEM}"
check_macos
check_source_files
# Check for existing installation
if [[ -d "${CONFIG_DIR}" ]] && [[ "${FORCE}" != "true" ]]; then
log_warning "Existing installation found at ${CONFIG_DIR}"
log_warning "Use --force to overwrite or --uninstall to remove first"
exit 1
fi
# Create directories
log_info "Creating directories..."
create_dir "${CONFIG_DIR}"
create_dir "${DATA_DIR}"
create_dir "${BIN_DIR}"
# Install files
install_configs
install_scripts
# Check PATH
check_path
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Dry run completed. No changes were made."
else
log_success "Installation completed successfully!"
log_info ""
log_info "Available commands:"
log_info " globalconnect - Main automation script"
log_info " gc-connect - Quick connect wrapper"
log_info " gp-test - Test suite"
log_info ""
log_info "Configuration:"
log_info " ${CONFIG_DIR}/environment.yaml"
log_info ""
log_info "Logs:"
log_info " ${DATA_DIR}/logs/"
fi
}
# Uninstall
uninstall() {
log_info "Starting GlobalProtect Automation uninstallation..."
# Remove binaries
log_info "Removing installed scripts..."
remove_file "${BIN_DIR}/globalconnect"
remove_file "${BIN_DIR}/gc-connect"
remove_file "${BIN_DIR}/gp-test"
# Ask about configuration and data
if [[ "${DRY_RUN}" != "true" ]]; then
echo -n "Remove configuration files? [y/N]: "
read -r remove_config
if [[ "${remove_config}" =~ ^[Yy]$ ]]; then
remove_dir "${CONFIG_DIR}"
else
log_info "Keeping configuration directory: ${CONFIG_DIR}"
fi
echo -n "Remove data/logs? [y/N]: "
read -r remove_data
if [[ "${remove_data}" =~ ^[Yy]$ ]]; then
remove_dir "${DATA_DIR}"
else
log_info "Keeping data directory: ${DATA_DIR}"
fi
else
log_info "Would ask about removing config and data directories"
fi
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "Dry run completed. No changes were made."
else
log_success "Uninstallation completed!"
fi
}
# Main execution
main() {
if [[ "${UNINSTALL}" == "true" ]]; then
uninstall
else
install
fi
}
# Execute main function
main "$@"