#!/bin/bash

set -e
set -o nounset
set -o pipefail

apt update
apt install -y less vim unzip

cd /workspace

git clone https://github.com/vladmandic/automatic
mkdir -p automatic/models/Stable-diffusion \
    automatic/models/LyCORIS \
    automatic/models/Lora \
    automatic/models/VAE \
    automatic/models/embeddings \
    automatic/extensions/

# Download models

pushd /workspace/automatic/models/Stable-diffusion/
curl -# -O -L -J "https://huggingface.co/bluepen5805/blue_pencil/resolve/main/blue_pencil-v9.safetensors"
curl -# -O -L -J "https://civitai.com/api/download/models/81207"
curl -# -O -L -J "https://civitai.com/api/download/models/64480"
popd
pushd /workspace/automatic/models/VAE
curl -# -O -L -J "https://civitai.com/api/download/models/80518"
curl -# -O -L -J "https://civitai.com/api/download/models/26689"
popd
pushd /workspace/automatic/models/LyCORIS
curl -# -O -L -J "https://civitai.com/api/download/models/55199"
popd
pushd /workspace/automatic/models/embeddings
curl -# -O -L -J "https://civitai.com/api/download/models/76712"

cp ~/*.json /workspace/automatic/
cp ~/*.toml /workspace

# Extensions
pushd /workspace/automatic/extensions/
git clone https://github.com/hako-mikan/sd-webui-regional-prompter
git clone https://github.com/hnmr293/sd-webui-cutoff
git clone https://github.com/ctwrs/a1111-sd-webui-tagcomplete
git clone https://github.com/ashen-sensored/sd_webui_SAG
git clone https://github.com/Bing-su/adetailer
git clone https://github.com/adieyal/sd-dynamic-prompts
popd

# ControlNet


cd /workspace
curl -L -O -J https://github.com/rapiz1/rathole/releases/download/v0.4.8/rathole-x86_64-unknown-linux-gnu.zip
unzip rathole-x86_64-unknown-linux-gnu.zip

nohup /workspace/rathole -c client-local.toml &

cd /workspace/automatic
python launch.py