#!/usr/bin/env bash
set -euo pipefail
# ===== Auto-generated by scripts/gen-pack-installers.mjs — do not edit =====
# Pack: WAN Animate 2.2 — OFM hub (Uni3C + face-swap)  (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"
clone "ComfyUI-KJNodes" "https://github.com/kijai/ComfyUI-KJNodes"
clone "ComfyUI-WanAnimatePreprocess" "https://github.com/kijai/ComfyUI-WanAnimatePreprocess"
clone "ComfyUI-segment-anything-2" "https://github.com/kijai/ComfyUI-segment-anything-2"
clone "ComfyUI-VideoHelperSuite" "https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite"
clone "ComfyMath" "https://github.com/evanspearman/ComfyMath"
clone "CRT-Nodes" "https://github.com/plugcrypt/CRT-Nodes"

echo "-------- models --------"
grab "models/diffusion_models/WanModel.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy_fp8_scaled/resolve/main/Wan22Animate/Wan2_2-Animate-14B_fp8_scaled_e4m3fn_KJ_v2.safetensors"
grab "models/vae/vae.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Wan2_1_VAE_bf16.safetensors"
grab "models/clip_vision/klip_vision.safetensors" "https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/resolve/main/split_files/clip_vision/clip_vision_h.safetensors"
grab "models/text_encoders/text_enc.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/umt5-xxl-enc-fp8_e4m3fn.safetensors"
grab "models/controlnet/Wan21_Uni3C_controlnet_fp16.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Wan21_Uni3C_controlnet_fp16.safetensors"
grab "models/loras/light.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank64_bf16.safetensors"
grab "models/loras/wan.reworked.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/LoRAs/Wan22_relight/WanAnimate_relight_lora_fp16.safetensors"
grab "models/loras/WanPusa.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Pusa/Wan21_PusaV1_LoRA_14B_rank512_bf16.safetensors"
grab "models/loras/WanFun.reworked.safetensors" "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/LoRAs/Wan22_FunReward/Wan2.2-Fun-A14B-InP-LOW-MPS_resized_dynamic_avg_rank_22_bf16.safetensors"
grab "models/detection/yolov10m.onnx" "https://huggingface.co/Wan-AI/Wan2.2-Animate-14B/resolve/main/process_checkpoint/det/yolov10m.onnx"
grab "models/detection/vitpose_h_wholebody_model.onnx" "https://huggingface.co/Kijai/vitpose_comfy/resolve/main/onnx/vitpose_h_wholebody_model.onnx"
grab "models/detection/vitpose_h_wholebody_data.bin" "https://huggingface.co/Kijai/vitpose_comfy/resolve/main/onnx/vitpose_h_wholebody_data.bin"

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