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
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
#!/bin/bash
# GPU Monitoring System Setup Script

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_DIR="/home/crhon/gpu_monitoring_system"
SERVICE_NAME="gpu-monitoring"
LOG_FILE="/var/log/gpu_monitoring_setup.log"

# Logging function
log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE"
}

warn() {
    echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$LOG_FILE"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE"
    exit 1
}

info() {
    echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$LOG_FILE"
}

# Check if running as root for system operations
check_root() {
    if [[ $EUID -eq 0 ]]; then
        return 0
    else
        return 1
    fi
}

# Install system dependencies
install_system_deps() {
    log "Installing system dependencies..."
    
    # Update package list
    sudo apt update
    
    # Install required packages
    sudo apt install -y \
        python3 \
        python3-pip \
        python3-venv \
        python3-pyqt5 \
        python3-pyqt5.qtopengl \
        python3-matplotlib \
        python3-flask \
        python3-flask-cors \
        python3-psutil \
        python3-numpy \
        python3-pandas \
        sqlite3
    
    log "System dependencies installed successfully"
}

# Setup Python virtual environment
setup_venv() {
    log "Setting up Python virtual environment..."
    
    cd "$INSTALL_DIR"
    
    # Create virtual environment
    python3 -m venv venv
    
    # Activate virtual environment
    source venv/bin/activate
    
    # Upgrade pip
    pip install --upgrade pip
    
    # Install Python requirements
    pip install -r requirements.txt
    
    log "Virtual environment setup complete"
}

# Create necessary directories
create_directories() {
    log "Creating necessary directories..."
    
    mkdir -p "$INSTALL_DIR/data"
    mkdir -p "$INSTALL_DIR/logs"
    mkdir -p "$INSTALL_DIR/config"
    mkdir -p "$INSTALL_DIR/backups"
    mkdir -p "$INSTALL_DIR/scripts"
    
    log "Directories created successfully"
}

# Setup systemd service
setup_systemd_service() {
    if ! check_root; then
        warn "Systemd service installation requires root privileges. Skipping..."
        return 1
    fi
    
    log "Setting up systemd service..."
    
    # Copy service file
    sudo cp "$INSTALL_DIR/systemd/$SERVICE_NAME.service" "/etc/systemd/system/$SERVICE_NAME.service"
    
    # Reload systemd
    sudo systemctl daemon-reload
    
    # Enable service
    sudo systemctl enable "$SERVICE_NAME"
    
    log "Systemd service installed and enabled"
}

# Setup autostart for desktop environments
setup_autostart() {
    log "Setting up desktop autostart..."
    
    # Create autostart directory
    mkdir -p "$HOME/.config/autostart"
    
    # Create desktop entry for overlay monitor
    cat > "$HOME/.config/autostart/gpu-monitor-overlay.desktop" << EOF
[Desktop Entry]
Type=Application
Name=GPU Monitor Overlay
Comment=GPU monitoring overlay for desktop
Exec=python3 $INSTALL_DIR/gpu_monitor_desktop.py --display overlay
Icon=video-display
Terminal=false
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
EOF
    
    # Create desktop entry for web interface
    cat > "$HOME/.config/autostart/gpu-monitor-web.desktop" << EOF
[Desktop Entry]
Type=Application
Name=GPU Monitor Web
Comment=GPU monitoring web interface
Exec=python3 $INSTALL_DIR/web_interface.py
Icon=video-display
Terminal=false
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
EOF
    
    log "Desktop autostart configured"
}

# Setup permissions
setup_permissions() {
    if ! check_root; then
        warn "Permission setup requires root privileges. Skipping..."
        return 1
    fi
    
    log "Setting up permissions..."
    
    # Set proper permissions for GPU access
    sudo usermod -a -G video "$USER"
    sudo usermod -a -G render "$USER"
    
    # Set permissions for log directory
    sudo mkdir -p /var/log
    sudo touch /var/log/gpu_fan_control.log
    sudo chown "$USER":"$USER" /var/log/gpu_fan_control.log
    sudo chmod 644 /var/log/gpu_fan_control.log
    
    log "Permissions configured"
}

# Create convenience scripts
create_scripts() {
    log "Creating convenience scripts..."
    
    # Create start script
    cat > "$INSTALL_DIR/scripts/start.sh" << 'EOF'
#!/bin/bash
cd "$(dirname "$0")/.."
source venv/bin/activate
python3 gpu_fan_controller.py --daemon
EOF
    
    # Create stop script
    cat > "$INSTALL_DIR/scripts/stop.sh" << 'EOF'
#!/bin/bash
pkill -f gpu_fan_controller.py
EOF
    
    # Create status script
    cat > "$INSTALL_DIR/scripts/status.sh" << 'EOF'
#!/bin/bash
echo "=== GPU Monitoring System Status ==="
echo "Service Status:"
if systemctl is-active --quiet gpu-monitoring 2>/dev/null; then
    echo "  ✓ Service is running"
else
    echo "  ✗ Service is not running"
fi

echo ""
echo "Process Status:"
if pgrep -f gpu_fan_controller.py > /dev/null; then
    echo "  ✓ Fan controller process is running"
else
    echo "  ✗ Fan controller process is not running"
fi

echo ""
echo "Web Interface:"
if pgrep -f web_interface.py > /dev/null; then
    echo "  ✓ Web interface is running"
    echo "  URL: http://localhost:5000"
else
    echo "  ✗ Web interface is not running"
fi

echo ""
echo "Log Files:"
echo "  /var/log/gpu_fan_control.log"
echo "  $HOME/gpu_monitoring_system/logs/"
EOF
    
    # Create test script
    cat > "$INSTALL_DIR/scripts/test.sh" << 'EOF'
#!/bin/bash
cd "$(dirname "$0")/.."
source venv/bin/activate

echo "=== Testing GPU Monitoring System ==="
echo ""

echo "1. Testing GPU Detection..."
python3 -c "
from gpu_monitoring import GPUManager
manager = GPUManager()
if manager.initialize():
    print('✓ GPU detection successful')
    gpus = manager.get_gpu_list()
    print(f'  Found {len(gpus)} GPU(s): {gpus}')
else:
    print('✗ GPU detection failed')
"

echo ""
echo "2. Testing Fan Control..."
python3 -c "
from gpu_fan_controller import FanController
controller = FanController()
if controller.initialize():
    print('✓ Fan control initialization successful')
    profiles = controller.get_profiles()
    print(f'  Available profiles: {list(profiles.keys())}')
else:
    print('✗ Fan control initialization failed')
"

echo ""
echo "3. Testing Web Interface..."
python3 -c "
import sys
sys.path.append('.')
try:
    from web_interface import app
    print('✓ Web interface import successful')
except Exception as e:
    print(f'✗ Web interface import failed: {e}')
"

echo ""
echo "=== Test Complete ==="
EOF
    
    # Make scripts executable
    chmod +x "$INSTALL_DIR/scripts/"*.sh
    
    log "Convenience scripts created"
}

# Create backup script
create_backup_script() {
    log "Creating backup script..."
    
    cat > "$INSTALL_DIR/scripts/backup.sh" << 'EOF'
#!/bin/bash
cd "$(dirname "$0")/.."

BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "Creating backup in $BACKUP_DIR..."

# Backup configuration
cp -r config "$BACKUP_DIR/"

# Backup database
if [ -f "data/gpu_monitoring.db" ]; then
    cp data/gpu_monitoring.db "$BACKUP_DIR/"
fi

# Backup logs (last 7 days)
find logs/ -name "*.log" -mtime -7 -exec cp {} "$BACKUP_DIR/" \;

# Create backup info
cat > "$BACKUP_DIR/backup_info.txt" << BACKUP_INFO
Backup created: $(date)
System: $(uname -a)
GPU Monitoring System Version: 1.0
BACKUP_INFO

echo "Backup created successfully in $BACKUP_DIR"
EOF
    
    chmod +x "$INSTALL_DIR/scripts/backup.sh"
    
    log "Backup script created"
}

# Setup cron jobs for maintenance
setup_cron_jobs() {
    log "Setting up maintenance cron jobs..."
    
    # Create cron job for database cleanup
    (crontab -l 2>/dev/null; echo "0 2 * * * $INSTALL_DIR/scripts/backup.sh") | crontab -
    
    # Create cron job for log rotation
    (crontab -l 2>/dev/null; echo "0 0 * * 0 find $INSTALL_DIR/logs -name '*.log' -mtime +7 -delete") | crontab -
    
    log "Cron jobs configured"
}

# Main installation function
main() {
    echo "=========================================="
    echo "GPU Monitoring System Setup"
    echo "=========================================="
    echo ""
    
    # Check if running from correct directory
    if [[ ! -f "$SCRIPT_DIR/requirements.txt" ]]; then
        error "Please run this script from the GPU monitoring system directory"
    fi
    
    # Initialize log file
    echo "Setup started at $(date)" > "$LOG_FILE"
    
    log "Starting installation process..."
    
    # Create directories
    create_directories
    
    # Install system dependencies
    install_system_deps
    
    # Setup virtual environment
    setup_venv
    
    # Setup systemd service (if root)
    if check_root; then
        setup_systemd_service
        setup_permissions
    fi
    
    # Setup autostart
    setup_autostart
    
    # Create scripts
    create_scripts
    create_backup_script
    
    # Setup cron jobs
    setup_cron_jobs
    
    log "Installation completed successfully!"
    echo ""
    echo "=========================================="
    echo "Installation Summary"
    echo "=========================================="
    echo "✓ System dependencies installed"
    echo "✓ Python virtual environment created"
    echo "✓ Configuration files created"
    echo "✓ Systemd service configured (if root)"
    echo "✓ Desktop autostart configured"
    echo "✓ Convenience scripts created"
    echo "✓ Backup system configured"
    echo ""
    echo "Next steps:"
    echo "1. Review configuration in config/"
    echo "2. Test the system: ./scripts/test.sh"
    echo "3. Start the service: sudo systemctl start $SERVICE_NAME"
    echo "4. View status: ./scripts/status.sh"
    echo ""
    echo "Log file: $LOG_FILE"
    echo "=========================================="
}

# Handle command line arguments
case "${1:-}" in
    "install")
        main
        ;;
    "uninstall")
        if check_root; then
            log "Removing systemd service..."
            sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true
            sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true
            sudo rm -f "/etc/systemd/system/$SERVICE_NAME.service"
            sudo systemctl daemon-reload
        fi
        
        log "Removing autostart entries..."
        rm -f "$HOME/.config/autostart/gpu-monitor-*.desktop"
        
        log "Removing cron jobs..."
        crontab -l 2>/dev/null | grep -v "$INSTALL_DIR" | crontab -
        
        log "Uninstallation completed"
        ;;
    "test")
        cd "$INSTALL_DIR"
        source venv/bin/activate
        ./scripts/test.sh
        ;;
    "status")
        cd "$INSTALL_DIR"
        ./scripts/status.sh
        ;;
    *)
        echo "Usage: $0 {install|uninstall|test|status}"
        echo ""
        echo "Commands:"
        echo "  install   - Install the GPU monitoring system"
        echo "  uninstall - Remove the GPU monitoring system"
        echo "  test      - Test the system components"
        echo "  status    - Show system status"
        exit 1
        ;;
esac