#!/bin/sh
which uv >/dev/null 2>&1 || exit
PYTHON_VER=3.12
VENV=venv-linux-amd-$PYTHON_VER
OPTIONS="--enable-manager"
OPTIONS="$OPTIONS --preview-method auto"
OPTIONS="$OPTIONS --disable-dynamic-vram"
OPTIONS="$OPTIONS --cache-none"
OPTIONS="$OPTIONS --listen"
#OPTIONS="$OPTIONS --lowvram"
#OPTIONS="$OPTIONS --cpu"
while :; do
	git pull --rebase
	if [ -d $VENV ]; then
		uv venv $VENV -p $PYTHON_VER --allow-existing
	else
		uv venv $VENV -p $PYTHON_VER
	fi
	. $VENV/bin/activate
	# uv pip install pip
	uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm7.2
	# uv pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/rocm7.2
	uv pip install -r requirements.txt
	uv pip install -r manager_requirements.txt
	uv pip install matrix-nio
	find custom_nodes -mindepth 1 -maxdepth 1 -type d|while read n; do
		if [ -d $n ]; then
			echo $n
			cd $n
			[ -d .git ] && git pull --rebase
			[ -r requirements.txt ] && uv pip install -r requirements.txt
			cd -
		fi
	done
	python main.py $OPTIONS
	# RDNA 2
	#HSA_OVERRIDE_GFX_VERSION=10.3.0 TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1 python main.py --use-pytorch-cross-attention $OPTIONS
	# RDNA 3
	#HSA_OVERRIDE_GFX_VERSION=11.0.0 TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1 python main.py --use-pytorch-cross-attention $OPTIONS
	# Newer
	#TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1 python main.py --use-pytorch-cross-attention $OPTIONS
done
