#!/bin/bash
set -e

echo "Patching NoteGraph Limit..."
if [ -d "/opt/NoteGraph" ]; then
  cd /opt/NoteGraph
  cat << 'EOF' > docker-compose.yml
services:
  silverbullet:
    image: zefhemel/silverbullet:latest
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 256M
    environment:
      SB_USER: "${SB_USER:-admin:changeme}"
    volumes:
      - ./notes:/space
    ports:
      - "3000:3000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 30s
      timeout: 5s
      retries: 3

  git-sync:
    image: alpine/git:latest
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 64M
    volumes:
      - ./notes:/space
      - ./.git:/repo-git:ro
    entrypoint: /bin/sh
    command: |
      -c '
      cd /space
      git init 2>/dev/null || true
      git config user.name "NoteGraph Auto-Sync"
      git config user.email "notegraph@andreknie.de"
      while true; do
        sleep 300
        cd /space
        git add -A
        if ! git diff --cached --quiet 2>/dev/null; then
          git commit -m "auto: sync notes"
        fi
      done
      '

  ingestion:
    image: notegraph-ingestion
    build:
      context: ./ingestion
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 256M
    ports:
      - "8001:8001"
    volumes:
      - ./notes:/app/notes
      - ./ingestion/inbox:/app/inbox
    env_file:
      - .env
    environment:
      NOTES_DIR: /app/notes
      INBOX_DIR: /app/inbox
    depends_on:
      - silverbullet
EOF
  docker compose up -d
  echo "NoteGraph patched!"
else
  echo "Directory /opt/NoteGraph not found."
fi

echo "Patching OrgMyLife Limit..."
if [ -d "/opt/OrgMyLife" ]; then
  cd /opt/OrgMyLife
  cat << 'EOF' > docker-compose.yml
version: "3.8"

services:
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    command: postgres -c shared_buffers=32MB -c max_connections=20
    deploy:
      resources:
        limits:
          memory: 256M
    environment:
      POSTGRES_DB: orgmylife
      POSTGRES_USER: orgmylife
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U orgmylife"]
      interval: 5s
      timeout: 3s
      retries: 5

  app:
    build: .
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 256M
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgresql://orgmylife:${POSTGRES_PASSWORD:-changeme}@db:5432/orgmylife
      NEXTCLOUD_URL: ${NEXTCLOUD_URL}
      NEXTCLOUD_USERNAME: ${NEXTCLOUD_USERNAME}
      NEXTCLOUD_PASSWORD: ${NEXTCLOUD_PASSWORD}
      IMAP_SERVER: ${IMAP_SERVER}
      IMAP_PORT: ${IMAP_PORT:-993}
      EMAIL_USERNAME: ${EMAIL_USERNAME}
      EMAIL_PASSWORD: ${EMAIL_PASSWORD}
      EMAIL_WEBMAIL_URL: ${EMAIL_WEBMAIL_URL:-}
      GMAIL_USERNAME: ${GMAIL_USERNAME:-}
      GMAIL_PASSWORD: ${GMAIL_PASSWORD:-}
      API_SECRET: ${API_SECRET:-}
      APP_USERNAME: ${APP_USERNAME:-admin}
      APP_PASSWORD: ${APP_PASSWORD:-changeme}
    volumes:
      - ./BACKLOG.md:/app/BACKLOG.md:ro
    command: >
      sh -c "python -c 'from app.db.session import engine, Base; from app.models import *; Base.metadata.create_all(bind=engine)' &&
             uvicorn app.main:app --host 0.0.0.0 --port 8000"

volumes:
  pgdata:
EOF
  docker compose up -d
  echo "OrgMyLife patched!"
else
  echo "Directory /opt/OrgMyLife not found."
fi

echo "All done!"
