#!/usr/bin/env bash
set -euo pipefail
# ===== Auto-generated by scripts/gen-pack-installers.mjs — do not edit =====
# Pack: WAN MultiTalk — audio-driven talking avatar  (Linux / RunPod)
# Run from your ComfyUI root (the folder containing custom_nodes/ and models/).
[ -d custom_nodes ] || { echo "[ERROR] run from your ComfyUI root (custom_nodes/ not found)"; exit 1; }
# Resolve the ComfyUI python: its venv first, then portable embed, then \$PYTHON/python3.
# Installing requirements into the wrong interpreter is why nodes silently fail to load.
if [ -n "${PYTHON:-}" ]; then PY="$PYTHON";
elif [ -x ".venv/bin/python" ]; then PY=".venv/bin/python";
elif [ -x "venv/bin/python" ]; then PY="venv/bin/python";
elif [ -x "../python_embeded/python" ]; then PY="../python_embeded/python";
else PY="python3"; fi
echo "using python: $PY"

clone() { # folder url
  if [ ! -d "custom_nodes/$1" ]; then
    echo "  cloning $1"; git clone --depth 1 "$2" "custom_nodes/$1"
    if [ -f "custom_nodes/$1/requirements.txt" ]; then echo "  installing $1 requirements.txt"; "$PY" -m pip install -r "custom_nodes/$1/requirements.txt"; fi
  else echo "  $1 present - skip"; fi
}
grab() { # relpath url
  mkdir -p "$(dirname "$1")"
  if [ ! -f "$1" ]; then echo "  downloading $(basename "$1")"; curl -L -o "$1" "$2"; else echo "  $(basename "$1") present - skip"; fi
}

echo "-------- custom nodes --------"
clone "ComfyUI-WanVideoWrapper" "https://github.com/kijai/ComfyUI-WanVideoWrapper.git"
clone "ComfyUI-KJNodes" "https://github.com/kijai/ComfyUI-KJNodes.git"
clone "ComfyUI-VideoHelperSuite" "https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite.git"
clone "audio-separation-nodes-comfyui" "https://github.com/christian-byrne/audio-separation-nodes-comfyui.git"

echo "-------- pip (manifest extras) --------"
"$PY" -m pip install "imageio-ffmpeg"

echo "-------- models --------"
grab "models/diffusion_models/Wan2.1_14b_Image_to_Video_480p_GGUF_Q8.gguf" "https://huggingface.co/city96/Wan2.1-I2V-14B-480P-gguf/resolve/main/wan2.1-i2v-14b-480p-Q8_0.gguf"
grab "models/diffusion_models/WanVideo_2_1_Multitalk_14B_fp8_e4m3fn.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/WanVideo_2_1_Multitalk_14B_fp8_e4m3fn.safetensors"
grab "models/text_encoders/umt5_xxl_fp16.safetensors" "https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/resolve/main/split_files/text_encoders/umt5_xxl_fp16.safetensors"
grab "models/vae/Wan2_1_VAE_bf16.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Wan2_1_VAE_bf16.safetensors"
grab "models/clip_vision/clip_vision_h.safetensors" "https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/resolve/main/split_files/clip_vision/clip_vision_h.safetensors"
grab "models/loras/Wan21_I2V_14B_lightx2v_cfg_step_distill_lora_rank64.safetensors" "https://huggingface.co/lightx2v/Wan2.1-I2V-14B-480P-StepDistill-CfgDistill-Lightx2v/resolve/main/loras/Wan21_I2V_14B_lightx2v_cfg_step_distill_lora_rank64.safetensors"

echo "DONE. Restart ComfyUI, then load workflow.json."
