#!/bin/bash
# Install source scripts and configure system
set -euo pipefail

echo "Installing source scripts..."

# Install source scripts
install -m 755 /workspace/src/firewall-setup.sh /usr/local/bin/
install -m 755 /workspace/src/security-hardening.sh /usr/local/bin/

# Create VPN configuration apply script
cat >/usr/local/bin/apply-vpn-config.sh <<'EOF'
#!/bin/bash
# Apply VPN configuration and update firewall
set -euo pipefail

# Apply firewall configuration
/usr/local/bin/firewall-setup.sh

# Start WireGuard if configuration exists
if [[ -f "/etc/wireguard/wg0.conf" ]]; then
    systemctl enable wg-quick@wg0
    systemctl start wg-quick@wg0
    echo "WireGuard started successfully."
else
    echo "Warning: WireGuard configuration not found."
fi

echo "VPN configuration applied successfully."
EOF

chmod +x /usr/local/bin/apply-vpn-config.sh

# Create desktop shortcuts
mkdir -p /usr/share/applications

# WireGuard Configuration Editor shortcut
cat >/usr/share/applications/wg-config.desktop <<EOF
[Desktop Entry]
Name=WireGuard Configuration
Comment=Edit WireGuard configuration
Exec=pkexec mousepad /etc/wireguard/wg0.conf
Icon=network-vpn
Terminal=true
Type=Application
Categories=Network;System;
EOF

# VPN Configuration Apply shortcut
cat >/usr/share/applications/apply-vpn.desktop <<EOF
[Desktop Entry]
Name=Apply VPN Configuration
Comment=Apply WireGuard configuration and start VPN
Exec=pkexec /usr/local/bin/apply-vpn-config.sh
Icon=network-vpn
Terminal=true
Type=Application
Categories=Network;System;
EOF

# WireGuard QR Code Import shortcut
cat >/usr/share/applications/scan-wireguard-qr.desktop <<EOF
[Desktop Entry]
Name=Import WireGuard QR Code
Comment=Scan QR code to import WireGuard configuration
Exec=pkexec /usr/local/bin/scan-wireguard-qr.sh
Icon=camera-web
Terminal=true
Type=Application
Categories=Network;System;
EOF

# Create WireGuard configuration directory
mkdir -p /etc/wireguard

# Add football to appropriate groups
usermod -a -G sudo,audio,video,plugdev,input,cdrom,floppy football 2>/dev/null || true

echo "Source scripts installed successfully."
