#!/usr/bin/env sh


#git clone https://github.com/comfyanonymous/ComfyUI

download_controlnets(){
    cd /home/$USER/files/models/stable-diffusion/controlnet
    wget https://huggingface.co/h94/IP-Adapter/resolve/main/models/ip-adapter-plus_sd15.bin
    wget https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/ip-adapter_sdxl.bin
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_seg.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_scribble.pth
    wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint.pth
    wget https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/coadapter-color-sd15v1.pth
    wget https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/coadapter-sketch-sd15v1.pth
    wget https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/coadapter-depth-sd15v1.pth
    wget https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank128/control-lora-canny-rank128.safetensors
    wget https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank128/control-lora-depth-rank128.safetensors

    wget https://huggingface.co/huchenlei/animal_openpose/blob/main/control_sd15_animal_openpose.pth
}


comfy_models_symlink(){
    MODELS_PATH="/home/$USER/files/models/stable-diffusion/"
    cd /home/$USER/code/stable-diffusion/ComfyUI/models
    rm -r checkpoints
    rm -r loras/
    rm -r vae
    rm -r controlnet/
    rm -r embeddings
    ln -s $MODELS_PATH"lora" ./loras
    ln -s $MODELS_PATH"models" ./checkpoints
    ln -s $MODELS_PATH"VAE" ./vae
    ln -s $MODELS_PATH"embeddings" ./embeddings
    ln -s $MODELS_PATH"controlnet" ./controlnet
    ln -s "home/$USER/Pictures/generations" ../output
    ln -s /home/tekakutli/code/stable-diffusion/custom_nodes /home/tekakutli/code/stable-diffusion/ComfyUI/custom_nodes
}

comfy_krita(){
    cd /home/$USER/code/stable-diffusion/ComfyUI/custom_nodes
    git clone https://github.com/Acly/comfyui-tooling-nodes
    cd ~/.local/share/krita/pykrita
    # download https://github.com/Acly/krita-ai-diffusion/releases/tag/v1.0.1
    # extract in ~/.local/share/krita/pykrita
    # move clip_vision models to ./ComfyUI/models/clip_vision/SD1.5/pytorch_model.bin
}

masactrl_install(){
    cd /home/$USER/code/stable-diffusion
    git clone https://github.com/ljzycmd/T2I-Adapter-w-MasaCtrl
    ln -fns "$(realpath /home/$USER/files/models/stable-diffusion/controlnet)" ./T2I-Adapter-w-MasaCtrl/models
}

# masactrl_install(){
#     cd /home/$USER/code/stable-diffusion
#     git clone https://github.com/TencentARC/T2I-Adapter
#     git clone https://github.com/TencentARC/MasaCtrl
#     cd T2I-Adapter
#     python -m venv .
#     pip install -r requirements.txt
#     cd ../MasaCtrl
#     python -m venv .
#     pip install -r requirements.txt
#     cd ..

#     rm T2I-Adapter/ldm/models/diffusion/ddim.py
#     rm -rf T2I-Adapter/models
#     ln -fns "$(realpath ./MasaCtrl/masactrl)"  ./T2I-Adapter/
#     ln -fs "$(realpath ./MasaCtrl/masactrl_w_adapter/masactrl_w_adapter.py)"  ./T2I-Adapter/
#     ln -fs "$(realpath ./MasaCtrl/masactrl_w_adapter/ddim.py)"  ./T2I-Adapter/ldm/models/diffusion/ddim.py
#     ln -fns /home/$USER/files/models/stable-diffusion/controlnet ./T2I-Adapter/models
# }


pre_install_amd(){
    #cant remember what is what
    pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/rocm5.2
    yay -S python-pyproject-hooks playerctl
    yay -S vulkan-radeon libva-mesa-driver mesa-vdpau rocminfo miopen-hip miopen-hip miopengemm lshw
    #related
    yay -S virtualenv venv

    # THEN FOLLOW
    # https://rentry.org/sdamd
}


sudo_this(){
    echo "export HSA_OVERRIDE_GFX_VERSION=10.3.0" >> ~/.bashrc
    sudo_arch_repos_fix
}
sudo_arch_repos_fix(){
    #
    cat <<EOT >> /etc/pacman.conf
[arch4edu]
Server = https://mirrors.tuna.tsinghua.edu.cn/arch4edu/$arch
## or other mirrors on https://github.com/arch4edu/mirrorlist/blob/master/mirrorlist.arch4edu
EOT
}

lora_install_fix(){
    pip install albumentations einops
}
