#!/bin/bash
set -e

# Clone or update neopig from git (idempotent)
if [ -d "neopig" ]; then
    cd neopig
    git fetch origin
    git reset --hard origin/main
else
    git clone https://git.unturf.com/engineering/unturf/pig.py.git neopig
    cd neopig
fi

# Create virtualenv and install dependencies
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# Create data directory for database
mkdir -p data

# Kill any existing neopig server before starting (for redeployments)
pkill -f "serp.py" || true
sleep 1

# Run neopig.py --serve on port 80 with import mode (tar.gz uploads)
NEOPIG_IMPORT=1 NEOPIG_DISABLE_CRAWL=1 python neopig.py --serve --host 0.0.0.0 --port 80
