#!/bin/bash
set -e

# Post-installation script for adastra-storage

INSTALL_DIR="/opt/adastra-storage"
SERVICE_USER="adastra"
SERVICE_GROUP="adastra"

echo "Adastra Storage: Post-installation setup..."

# Create service user if it doesn't exist
if ! id "$SERVICE_USER" &>/dev/null; then
    echo "Creating service user: $SERVICE_USER"
    useradd -r -s /bin/false -d "$INSTALL_DIR" "$SERVICE_USER" || true
fi

# Set ownership
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR" || true

# Create data directory
mkdir -p "$INSTALL_DIR/data"
chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR/data"

# Enable and start systemd service
if systemctl is-enabled adastra-storage.service >/dev/null 2>&1; then
    echo "Service already enabled"
else
    systemctl daemon-reload
    systemctl enable adastra-storage.service
    echo "Service enabled. Start with: systemctl start adastra-storage"
fi

# Set permissions for ZFS commands (if needed)
# Note: Service user may need sudo access or be in appropriate groups
usermod -aG disk "$SERVICE_USER" || true

echo "Adastra Storage installation complete!"
echo "Default admin credentials: username=admin, password=admin"
echo "Please change the default password after first login!"
echo ""
echo "Start the service: systemctl start adastra-storage"
echo "Check status: systemctl status adastra-storage"
echo "View logs: journalctl -u adastra-storage -f"

exit 0

