#!/bin/bash -e
# Create systemd integration for AUTO_SECURITY_UPDATES .env toggle
# Allows users to disable automatic updates via the Dangerous Pi .env file

echo "=== Setting up auto-update toggle service ==="

# Create a oneshot service that checks .env and enables/disables the timer
cat > /etc/systemd/system/dangerous-pi-auto-updates.service << 'SVCEOF'
[Unit]
Description=Dangerous Pi - Configure automatic security updates from .env
After=local-fs.target
Before=apt-daily.timer apt-daily-upgrade.timer

[Service]
Type=oneshot
ExecStart=/bin/bash -c '\
    AUTO_SECURITY_UPDATES=true; \
    [ -f /opt/dangerous-pi/.env ] && . /opt/dangerous-pi/.env; \
    if [ "$AUTO_SECURITY_UPDATES" = "false" ]; then \
        echo "Auto security updates DISABLED by .env"; \
        systemctl stop apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true; \
        systemctl mask apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true; \
    else \
        echo "Auto security updates ENABLED"; \
        systemctl unmask apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true; \
        systemctl start apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true; \
    fi'
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
SVCEOF

systemctl enable dangerous-pi-auto-updates.service

echo "Auto-update toggle service installed."
