#!/usr/bin/env bash
# One-shot setup: pull the requested GGUF and register it locally as `bookbuilder`
# with sampling defaults tuned to prevent both repetition loops and synonym-chain
# runaway.
#
# Run: bash setup_ollama.sh [quant]
# Optional [quant]: Q4_K_M | Q5_K_M | Q8_0 | F16 (default F16 — cleanest decoding)

set -euo pipefail

QUANT="${1:-F16}"
REPO="hf.co/Fordentinc/book-builder-bookwriter-v1"
LOCAL_NAME="bookbuilder"

echo "==> Pulling $REPO:$QUANT (this may take several minutes; F16 is ~15 GB)"
ollama pull "$REPO:$QUANT"

TMPDIR="$(mktemp -d)"
cat > "$TMPDIR/Modelfile" <<EOF
FROM $REPO:$QUANT
TEMPLATE """{{ .Prompt }}"""
SYSTEM ""
PARAMETER temperature 0.8
PARAMETER top_p 0.92
PARAMETER top_k 40
PARAMETER min_p 0.05
PARAMETER repeat_penalty 1.08
PARAMETER repeat_last_n 128
PARAMETER num_ctx 8192
PARAMETER num_predict 2500
PARAMETER stop "### Chapter"
PARAMETER stop "Chapter 2:"
PARAMETER stop "### Bible"
PARAMETER stop "### Genre"
EOF

echo "==> Registering local model 'bookbuilder' from $REPO:$QUANT"
ollama create "$LOCAL_NAME" -f "$TMPDIR/Modelfile"

rm -rf "$TMPDIR"

echo ""
echo "==> Done. Now run:"
echo "    ollama run bookbuilder < your_bible.txt"
echo ""
echo "Format reminder for your_bible.txt — every line must be left-justified, no leading spaces:"
echo "    ### Bible"
echo "    Title: ..."
echo "    Author: ..."
echo "    Genre: ..."
echo "    Publisher: ..."
echo "    Synopsis: ..."
echo "    [blank line]"
echo "    ### Genre"
echo "    [genre]"
echo "    [blank line]"
echo "    [blank line]"
echo "    [blank line]"
echo "    ### Chapter"
echo "    Chapter 1: <title>"
echo "    [blank line]"
echo ""
echo "Bibles with leading whitespace WILL produce off-topic generations."
