#!/bin/bash
set -e
# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
#   TRUTH      First principles, math & science, open source code freely distributed
#   FREEDOM    Voluntary partnerships, freedom from tyranny & corporate control
#   HARMONY    Minimal waste, self-renewing systems with diverse thriving connections
#   LOVE       Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by archiving the web, preserving digital knowledge before it disappears.
# Code is seeds to sprout on any abandoned technology.

# 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
