#!/bin/sh
# Post-installation script for the Phage .deb package.
# Runs as root, so it can provision the privileged helper (systemd service).

set -e

# Make binaries executable (should already be, but ensure).
chmod +x /usr/bin/phage 2>/dev/null || true
chmod +x /usr/bin/phage-gui 2>/dev/null || true
chmod +x /usr/bin/phage-helper 2>/dev/null || true

# Update desktop database for the .desktop file.
if command -v update-desktop-database > /dev/null 2>&1; then
    update-desktop-database /usr/share/applications/ 2>/dev/null || true
fi

# Refresh the icon cache so the Phage icon appears immediately.
if command -v gtk-update-icon-cache > /dev/null 2>&1; then
    gtk-update-icon-cache -f -t /usr/share/icons/hicolor 2>/dev/null || true
fi

# Provision the DNS resolver and the privileged helper service (ports 80/443,
# auto-start on boot). Non-interactive; idempotent.
if [ -x /usr/bin/phage ]; then
    echo "Provisioning Phage background service (ports 80/443)..."
    /usr/bin/phage install --yes 2>&1 || \
        echo "Phage: could not provision the background service automatically; run 'sudo phage install'."
fi

exit 0
