#!/bin/bash
# ─────────────────────────────────────────────────────────────
#  Email Enricher — macOS / Linux launcher
#  Place this file in the project root, then run it from Terminal:
#     chmod +x start.sh    # one-time, makes the file executable
#     ./start.sh
#  First run installs npm dependencies automatically.
# ─────────────────────────────────────────────────────────────

set -e

# Run from the folder this script lives in
cd "$(dirname "$0")"

# Sanity check — make sure we're sitting next to server.js
if [ ! -f "server.js" ]; then
  echo ""
  echo "[Error] server.js not found in this folder."
  echo "Put start.sh in the project root (the folder that has server.js and package.json),"
  echo "then run it again."
  echo ""
  exit 1
fi

# First run: install dependencies
if [ ! -d "node_modules" ]; then
  echo "Installing dependencies (one-time setup)..."
  if ! npm install; then
    echo ""
    echo "[Error] npm install failed. Make sure Node.js is installed: https://nodejs.org"
    echo ""
    exit 1
  fi
fi

# Raise V8 heap so big XLSX downloads don't run out of memory
export NODE_OPTIONS="--max-old-space-size=1200"

echo ""
echo "Starting Email Enricher on http://localhost:3000"
echo "Press Ctrl+C to stop."
echo ""
exec node server.js
