-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·344 lines (301 loc) · 10.2 KB
/
install.sh
File metadata and controls
executable file
·344 lines (301 loc) · 10.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
#!/bin/bash
set -e
# Smart Brightness Installer
# Enhanced with calibration and mode selection
echo "========================================"
echo " Smart Brightness Installer"
echo "========================================"
echo
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PACKAGES_DIR="$ROOT_DIR/packages"
ensure_packages_dir() {
mkdir -p "$PACKAGES_DIR"
}
build_release_binary() {
echo "Building release binary..."
cargo build --release --locked
}
# No longer supporting docker builds in this script as docker folder was removed.
# Check dependencies
check_dependencies() {
echo "Checking dependencies..."
# Check for cargo
if ! command -v cargo &> /dev/null; then
echo "❌ Error: cargo not found. Please install Rust toolchain."
echo " Visit: https://rustup.rs/"
exit 1
fi
echo "✓ Cargo found"
# Check for camera
if [ ! -e /dev/video0 ]; then
echo "⚠ Warning: No camera detected at /dev/video0"
echo " Smart Brightness requires a webcam to function."
read -p "Continue anyway? (y/n): " continue_choice
if [[ ! "$continue_choice" =~ ^[Yy]$ ]]; then
exit 1
fi
else
echo "✓ Camera detected"
fi
# Check video group membership
if ! groups | grep -q video; then
echo "⚠ Warning: Current user is not in 'video' group"
echo " This is required for backlight and camera access."
echo " Run: sudo usermod -aG video $USER"
echo " Then log out and log back in."
read -p "Continue anyway? (y/n): " continue_choice
if [[ ! "$continue_choice" =~ ^[Yy]$ ]]; then
exit 1
fi
else
echo "✓ User in video group"
fi
echo
}
# Detect OS
if [ -f /etc/arch-release ]; then
OS="Arch"
else
OS="Generic"
fi
echo "Detected OS: $OS"
echo
# Check dependencies
check_dependencies
# Installation method selection
if [ "$OS" == "Arch" ]; then
echo "Choose installation method:"
echo "1) Install pre-built binary (pacman)"
echo "2) Compile and install (makepkg)"
echo "3) Manual install (cargo build + copy)"
read -p "Selection [1-3]: " method
case $method in
1)
PKG=$(ls "$PACKAGES_DIR"/*x86_64.pkg.tar.zst 2>/dev/null | head -n 1)
if [ -z "$PKG" ]; then
echo "Error: No package found in project directory."
exit 1
fi
echo "Installing $PKG..."
sudo pacman -U "$PKG"
;;
2)
echo "Building with makepkg..."
PKGDEST="$PACKAGES_DIR" makepkg -si
;;
3)
echo "Building with cargo..."
cargo build --release
echo "Installing binary to /usr/local/bin..."
sudo cp target/release/smart-brightness /usr/local/bin/
sudo chmod +x /usr/local/bin/smart-brightness
;;
*)
echo "Invalid selection."
exit 1
;;
esac
else
echo "Generic installation (Cargo)..."
cargo build --release
echo "Installing binary to /usr/local/bin..."
sudo cp target/release/smart-brightness /usr/local/bin/
sudo chmod +x /usr/local/bin/smart-brightness
method="3"
fi
echo
echo "✓ Binary installed successfully!"
echo
# Daemon Mode Selection
echo "=========================================="
echo " Daemon Mode Configuration"
echo "=========================================="
echo
echo "Select daemon mode:"
echo "1) Realtime - Continuously adjust brightness (recommended)"
echo "2) Boot - Run for a duration then exit (good for startup)"
echo "3) Interval - Run periodically with pauses (battery saving)"
echo
read -p "Selection [1-3] (default: 1): " mode_choice
mode_choice=${mode_choice:-1}
case $mode_choice in
1)
DAEMON_MODE="realtime"
echo "✓ Selected: Realtime mode"
;;
2)
DAEMON_MODE="boot"
echo "✓ Selected: Boot mode"
read -p "Run duration in seconds (default: 300): " run_duration
run_duration=${run_duration:-300}
;;
3)
DAEMON_MODE="interval"
echo "✓ Selected: Interval mode"
read -p "Active duration in seconds (default: 60): " run_duration
run_duration=${run_duration:-60}
read -p "Pause duration in seconds (default: 60): " pause_interval
pause_interval=${pause_interval:-60}
;;
*)
echo "Invalid selection, using Realtime mode"
DAEMON_MODE="realtime"
;;
esac
echo
# Config Setup
echo "=========================================="
echo " Configuration Setup"
echo "=========================================="
echo
echo "Smart Brightness loads config from (in order):"
echo " 1. ~/.config/smart-brightness/config.toml (user config)"
echo " 2. /etc/smart-brightness/config.toml (system config)"
echo " 3. ./config.toml (project directory)"
echo
# Create system config directory
SYSTEM_CONFIG_DIR="/etc/smart-brightness"
if [ ! -d "$SYSTEM_CONFIG_DIR" ]; then
echo "Creating $SYSTEM_CONFIG_DIR..."
sudo mkdir -p "$SYSTEM_CONFIG_DIR"
fi
# Create user config directory
USER_CONFIG_DIR="$HOME/.config/smart-brightness"
if [ ! -d "$USER_CONFIG_DIR" ]; then
echo "Creating $USER_CONFIG_DIR..."
mkdir -p "$USER_CONFIG_DIR"
fi
# Copy config if it exists
if [ -f "config.toml" ]; then
# Update mode in config
if [ -f "config.toml" ]; then
cp config.toml config.toml.tmp
sed -i "s/^mode = .*/mode = \"$DAEMON_MODE\"/" config.toml.tmp
if [ "$DAEMON_MODE" == "boot" ] || [ "$DAEMON_MODE" == "interval" ]; then
sed -i "s/^active_seconds = .*/active_seconds = $run_duration.0/" config.toml.tmp
fi
if [ "$DAEMON_MODE" == "interval" ]; then
sed -i "s/^pause_seconds = .*/pause_seconds = $pause_interval.0/" config.toml.tmp
fi
# Helper for overwrite confirmation
confirm_write() {
local dest=$1
if [ -f "$dest" ]; then
read -p "⚠ $dest already exists. Overwrite with new defaults/mode? (y/n): " choice
[[ "$choice" =~ ^[Yy]$ ]] && return 0 || return 1
fi
return 0
}
if confirm_write "$USER_CONFIG_DIR/config.toml"; then
echo "Copying config to $USER_CONFIG_DIR/config.toml..."
cp config.toml.tmp "$USER_CONFIG_DIR/config.toml"
else
echo "Skipping $USER_CONFIG_DIR/config.toml"
fi
if confirm_write "$SYSTEM_CONFIG_DIR/config.toml"; then
echo "Copying config to $SYSTEM_CONFIG_DIR/config.toml (fallback)..."
sudo cp config.toml.tmp "$SYSTEM_CONFIG_DIR/config.toml"
else
echo "Skipping $SYSTEM_CONFIG_DIR/config.toml"
fi
rm config.toml.tmp
echo "✓ Configuration processing complete"
fi
else
echo "⚠ No local config.toml found. Defaults will be used."
fi
echo
# Calibration Option
echo "=========================================="
echo " Calibration"
echo "=========================================="
echo
# Check if already calibrated
ALREADY_CALIBRATED=false
if [ -f "$USER_CONFIG_DIR/config.toml" ] && grep -q "calibrated = true" "$USER_CONFIG_DIR/config.toml"; then
ALREADY_CALIBRATED=true
elif [ -f "$SYSTEM_CONFIG_DIR/config.toml" ] && grep -q "calibrated = true" "$SYSTEM_CONFIG_DIR/config.toml"; then
ALREADY_CALIBRATED=true
fi
if [ "$ALREADY_CALIBRATED" = true ]; then
echo "✨ Existing calibration found. Skipping automatic prompt."
read -p "Recalibrate anyway? (y/n): " calibrate_choice
else
read -p "Run calibration now? (y/n): " calibrate_choice
fi
if [[ "$calibrate_choice" =~ ^[Yy]$ ]]; then
echo
echo "Starting calibration..."
echo "Note: This will be saved to $USER_CONFIG_DIR/config.toml"
echo
# Determine binary path
if [ "$method" == "3" ] || [ "$OS" != "Arch" ]; then
BINARY_PATH="/usr/local/bin/smart-brightness"
else
BINARY_PATH="/usr/bin/smart-brightness"
fi
# Run calibration
if $BINARY_PATH --calibrate; then
echo
echo "✓ Calibration completed successfully!"
else
echo
echo "⚠ Calibration failed or was cancelled."
echo " You can run it later with: smart-brightness --calibrate"
fi
else
echo "Skipping calibration."
echo "You can run it later with: smart-brightness --calibrate"
fi
echo
# Service Setup
echo "=========================================="
echo " Systemd Service Setup"
echo "=========================================="
echo
SERVICE_FILE="smart-brightnessd.service"
DEST_SERVICE="/etc/systemd/system/$SERVICE_FILE"
# Adjust service file path if needed
if [ "$method" == "3" ] || [ "$OS" != "Arch" ]; then
# Manual install to /usr/local/bin
echo "Adjusting service file for /usr/local/bin..."
sed 's|/usr/bin/smart-brightness|/usr/local/bin/smart-brightness|g' "$SERVICE_FILE" > "$SERVICE_FILE.tmp"
sudo cp "$SERVICE_FILE.tmp" "$DEST_SERVICE"
rm "$SERVICE_FILE.tmp"
else
sudo cp "$SERVICE_FILE" "$DEST_SERVICE"
fi
sudo systemctl daemon-reload
echo "✓ Service file installed"
echo
read -p "Enable and start service now? (y/n): " start_choice
if [[ "$start_choice" =~ ^[Yy]$ ]]; then
sudo systemctl enable --now smart-brightnessd
echo "✓ Service enabled and started!"
echo
echo "Check status with: sudo systemctl status smart-brightnessd"
echo "View logs with: journalctl -u smart-brightnessd -f"
else
echo "Service installed but not started."
echo
echo "To start later:"
echo " sudo systemctl enable --now smart-brightnessd"
fi
echo
echo "=========================================="
echo " Installation Complete!"
echo "=========================================="
echo
echo "Configuration:"
echo " User config: $USER_CONFIG_DIR/config.toml"
echo " System config: $SYSTEM_CONFIG_DIR/config.toml"
echo
echo "Daemon mode: $DAEMON_MODE"
echo
echo "Useful commands:"
echo " smart-brightness --help - Show help"
echo " smart-brightness --calibrate - Run calibration"
echo " sudo systemctl status smart-brightnessd - Check service status"
echo " journalctl -u smart-brightnessd -f - View logs"
echo
echo "Enjoy Smart Brightness! 🌟"