#!/bin/sh
set -e

# Set ownership on data directories
chown -R prism:prism /var/lib/prism 2>/dev/null || true
chown -R prism:prism /var/log/prism 2>/dev/null || true

# Reload systemd to pick up the unit file
if command -v systemctl >/dev/null 2>&1; then
    systemctl daemon-reload 2>/dev/null || true

    # deb: $1 = "configure" on fresh install, "configure" on upgrade too
    # rpm: $1 = 1 on fresh install, 2+ on upgrade
    case "${1}" in
        configure|1)
            # Fresh install — enable but don't start (user must configure first)
            systemctl enable prism 2>/dev/null || true
            echo ""
            echo "PRISM installed successfully."
            echo ""
            echo "  1. Edit /etc/prism/config.toml"
            echo "  2. Ensure Chrome/Chromium is installed"
            echo "  3. Start with: systemctl start prism"
            echo ""
            ;;
        2|[3-9]*)
            # Upgrade — restart if running
            systemctl try-restart prism 2>/dev/null || true
            ;;
    esac
fi
