#!/bin/bash
set -e

# Start cron daemon
cron

# Configure SSH for git — copy mounted key so we can fix permissions
if [ -f /root/.ssh/gitea_ed25519 ]; then
  cp /root/.ssh/gitea_ed25519 /tmp/gitea_ed25519
  chmod 600 /tmp/gitea_ed25519
  export GIT_SSH_COMMAND="ssh -i /tmp/gitea_ed25519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  echo "SSH key configured for Gitea access"
else
  echo "WARNING: No SSH key found at /root/.ssh/gitea_ed25519 — Gitea scan will use HTTPS"
fi

# Run initial Gitea scan
echo "Running initial Gitea scan..."
cd /app && bun run src/aggregator/gitea-scanner.ts --verbose 2>&1 || echo "Gitea scan completed with errors (non-fatal)"

# Start aggregator
echo "Starting aggregator..."
exec bun run src/aggregator/index.ts --port 6420 --paths "/projects/websites,/projects/apps,/projects/gitea"
