-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·178 lines (158 loc) · 6.23 KB
/
install.sh
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
#!/usr/bin/env bash
# Setupr Installation Script - Package Selection Interface
set -euo pipefail
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
source "${SCRIPT_DIR}/lib/utils.sh"
# Parse command line arguments
DRY_RUN=0
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=1
shift
;;
esac
done
# Print dry run banner if enabled
if [ "$DRY_RUN" -eq 1 ]; then
gum style \
--foreground 214 --bold --border-foreground 214 --border thick \
--align center --width 50 --margin "1 2" \
"🔍 DRY RUN MODE - No changes will be made"
fi
# Display installation modes
gum style --foreground 99 --bold --margin "1 0" "Installation Mode"
echo
MODES=(
"🚀 Auto Install (Recommended)"
"🔨 Interactive Installation"
"⚙️ Save Configuration"
"📂 Load Configuration"
)
# Display menu for mode selection
MODE=$(gum choose \
--cursor.foreground="212" \
--selected.foreground="82" \
"${MODES[@]}")
gum style --foreground 99 --bold --margin "1 0" "Processing"
echo
case "$MODE" in
"🚀 Auto Install"*)
if [ -f "${SCRIPT_DIR}/recommended-config.json" ]; then
gum style --foreground 99 "📦 Loading recommended configuration..."
# Process recommended config
tempfile=$(mktemp)
gum style --foreground 99 "📦 Loading recommended configuration..."
if jq -r '.packages[]' "${SCRIPT_DIR}/recommended-config.json" >"$tempfile"; then
sudo bash "${SCRIPT_DIR}/install-pkg.sh" < "$tempfile"
fi
rm -f "$tempfile"
else
log_error "Recommended configuration not found!"
exit 1
fi
;;
"🔨 Interactive Installation"*)
if [ "$DRY_RUN" -eq 1 ]; then
log_info "Dry run: Would run menu.sh"
exit 0
else
# Run menu.sh and capture package selections from FD 3
tempfile=$(mktemp)
if bash "${SCRIPT_DIR}/menu.sh" >&2 3>"$tempfile"; then
if [ -s "$tempfile" ]; then
gum style --foreground 99 "📦 Processing selections..."
cat "$tempfile" | sudo bash "${SCRIPT_DIR}/install-pkg.sh"
else
log_error "No packages were selected or menu was cancelled"
fi
fi
rm -f "$tempfile"
fi
;;
"⚙️ Save Configuration"*)
# Run menu.sh in save config mode and capture package selections from FD 3
SELECTIONS=$(mktemp)
if bash "${SCRIPT_DIR}/menu.sh" --save-config >&2 3>"$SELECTIONS"; then
if [ -s "$SELECTIONS" ]; then
REAL_USER="${SUDO_USER:-$USER}"
DOWNLOADS_DIR="${SUDO_HOME:-$HOME}/Downloads"
CONFIG_NAME=$(sudo -u "$REAL_USER" gum input \
--placeholder "Enter configuration name (e.g., dev, media)" \
--value "myconfig")
CONFIG_NAME=${CONFIG_NAME:-default}
CONFIG_FILE="$DOWNLOADS_DIR/${CONFIG_NAME}-setupr.json"
CURRENT_TIME=$(date +"%Y-%m-%d %H:%M:%S")
# Create configuration JSON
jq -n \
--arg name "$CONFIG_NAME" \
--arg time "$CURRENT_TIME" \
--arg desc "Setupr configuration created on $CURRENT_TIME" \
--argjson pkgs "$(cat "$SELECTIONS" | tr '\n' ' ' | jq -R -s 'split(" ")')" \
'{name: $name, description: $desc, timestamp: $time, packages: $pkgs}' \
> "$CONFIG_FILE"
chown "$REAL_USER:$REAL_USER" "$CONFIG_FILE"
chmod 644 "$CONFIG_FILE"
log_success "Configuration saved as '${CONFIG_NAME}-setupr.json'"
log_info "File location: $CONFIG_FILE"
else
log_error "No packages were selected!"
exit 1
fi
fi
rm -f "$SELECTIONS"
;;
"📂 Load Configuration"*)
REAL_USER="${SUDO_USER:-$USER}"
DOWNLOADS_DIR="${SUDO_HOME:-$HOME}/Downloads"
configs=()
while IFS= read -r file; do
if [ -f "$file" ]; then
name=$(basename "$file" | sed 's/-setupr\.json$//')
timestamp=$(jq -r '.timestamp // "Unknown date"' "$file")
configs+=("$name ($timestamp)")
fi
done < <(find "$DOWNLOADS_DIR" -name "*-setupr.json" -type f)
if [ ${#configs[@]} -eq 0 ]; then
log_error "No configuration files found in $DOWNLOADS_DIR!"
exit 1
fi
gum style --foreground 99 --bold --margin "1 0" "Saved Configurations"
echo
selected=$(gum choose --cursor.foreground="212" \
--selected.foreground="82" \
--header.border="rounded" \
--header.border-foreground="99" \
"${configs[@]}")
if [ -n "$selected" ]; then
config_name=$(echo "$selected" | cut -d' ' -f1)
config_file="$DOWNLOADS_DIR/${config_name}-setupr.json"
if [ "$DRY_RUN" -eq 1 ]; then
log_info "Dry run: Would install from $config_name"
jq -r '.packages[]' "$config_file" | while read -r pkg; do
log_info "Would install: $pkg"
done
else
gum style --foreground 99 "📦 Loading configuration..."
# Extract and process packages from saved config
jq -r '.packages[]' "$config_file" >/tmp/pkg_list
if [ -s /tmp/pkg_list ]; then
sudo bash "${SCRIPT_DIR}/install-pkg.sh" < /tmp/pkg_list
rm -f /tmp/pkg_list
fi
fi
else
log_error "No configuration selected!"
exit 1
fi
;;
*)
log_error "Invalid mode selected."
exit 1
;;
esac
# Run cleanup if packages were installed
if [[ "$MODE" =~ ^"🚀 Auto Install"|"🔨 Interactive Installation"|"📂 Load Configuration" ]]; then
"${SCRIPT_DIR}/system-cleanup.sh"
log_success "Setup completed successfully!"
fi