#!/bin/bash
set -e

case "$1" in
    configure)
        # Create skyview user and group if they don't exist
        if ! getent group skyview >/dev/null 2>&1; then
            addgroup --system skyview
        fi
        
        if ! getent passwd skyview >/dev/null 2>&1; then
            adduser --system --ingroup skyview --home /var/lib/skyview \
                    --no-create-home --disabled-password skyview
        fi
        
        # Create directories with proper permissions
        mkdir -p /var/lib/skyview
        mkdir -p /var/log/skyview
        chown skyview:skyview /var/lib/skyview
        chown skyview:skyview /var/log/skyview
        chmod 755 /var/lib/skyview
        chmod 755 /var/log/skyview
        
        # Set permissions on config file
        if [ -f /etc/skyview/config.json ]; then
            chown root:skyview /etc/skyview/config.json
            chmod 640 /etc/skyview/config.json
        fi
        
        # Enable and start the service
        systemctl daemon-reload
        systemctl enable skyview.service
        
        echo "SkyView has been installed and configured."
        echo "Edit /etc/skyview/config.json to configure your dump1090 sources."
        echo "Then run: systemctl start skyview"
        ;;
esac

exit 0