#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
ROOT="$PWD"
mkdir -p logs
exec > >(tee -a logs/setup.log) 2>&1
python -c 'import sys; assert sys.version_info >= (3, 10), "Python >=3.10 required"; import torch; print(torch.__version__, torch.version.cuda); assert torch.cuda.is_available(), "Use the NVIDIA GPU image"'
python -m venv --system-site-packages .venv
source .venv/bin/activate
if [[ ! -d sd-scripts ]]; then
  git init sd-scripts
  git -C sd-scripts remote add origin https://github.com/kohya-ss/sd-scripts.git
  git -C sd-scripts fetch --depth 1 origin 37a1cbbc5725ed2a3575506e7bd2001c9908ac92
  git -C sd-scripts checkout --detach FETCH_HEAD
fi
test "$(git -C sd-scripts rev-parse HEAD)" = 37a1cbbc5725ed2a3575506e7bd2001c9908ac92
cd sd-scripts
python -m pip install -r requirements.txt 'peft==0.14.0' 'bitsandbytes==0.45.5'
python -m pip check
cd "$ROOT"
python -m pip freeze > logs/pip-freeze.txt
python portrait.py preflight
