#!/bin/sh
set -e

case "$1" in
    configure)
        # Create cards user/group if they don't exist
        if ! getent group cards >/dev/null 2>&1; then
            addgroup --system cards
        fi
        if ! getent passwd cards >/dev/null 2>&1; then
            adduser --system --ingroup cards --no-create-home --disabled-password cards
        fi

        # Copy default config if not present
        if [ ! -f /etc/cards/config.json ]; then
            cp /etc/cards/cards.json.example /etc/cards/config.json
            chown cards:cards /etc/cards/config.json
            chmod 640 /etc/cards/config.json
        fi

        # Enable and restart service
        if [ -d /run/systemd/system ]; then
            systemctl daemon-reload >/dev/null 2>&1 || true
            systemctl enable cards >/dev/null 2>&1 || true
            systemctl restart cards >/dev/null 2>&1 || true
        fi
        ;;
esac
