# BamBuddy Systemd Service Template
#
# INSTALLATION:
# 1. Copy this file to /etc/systemd/system/bambuddy.service
# 2. Replace placeholders:
#    - INSTALL_PATH: Where BamBuddy is installed (e.g., /opt/bambuddy)
#    - SERVICE_USER: User to run as (e.g., bambuddy)
#    - DATA_DIR: Data directory (e.g., /opt/bambuddy/data)
#    - LOG_DIR: Log directory (e.g., /opt/bambuddy/logs)
# 3. Run: sudo systemctl daemon-reload
# 4. Run: sudo systemctl enable bambuddy
# 5. Run: sudo systemctl start bambuddy
#
# Or use the install script: ./install/install.sh
#

[Unit]
Description=BamBuddy - Bambu Lab Print Management
Documentation=https://github.com/maziggy/bambuddy
After=network.target

[Service]
Type=simple
User=SERVICE_USER
Group=SERVICE_USER
WorkingDirectory=INSTALL_PATH

# Environment file (optional - created by install script)
EnvironmentFile=-INSTALL_PATH/.env

# Use virtual environment
Environment="PATH=INSTALL_PATH/venv/bin:/usr/local/bin:/usr/bin:/bin"

# Server configuration
ExecStart=INSTALL_PATH/venv/bin/uvicorn backend.app.main:app --host 0.0.0.0 --port ${PORT:-8000}

# Restart policy
Restart=on-failure
RestartSec=5

# Graceful shutdown
TimeoutStopSec=10

# Kill zombie ffmpeg processes (timelapse processing)
ExecStartPre=-/usr/bin/pkill -9 -f "ffmpeg.*bambuddy"
ExecStopPost=-/usr/bin/pkill -9 -f "ffmpeg.*bambuddy"

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=bambuddy

# Security hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=DATA_DIR LOG_DIR INSTALL_PATH

[Install]
WantedBy=multi-user.target
