#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VENV_PYTHON="${ROOT_DIR}/.venv/bin/python"

if [[ ! -x "${VENV_PYTHON}" ]]; then
  echo "error: ${VENV_PYTHON} does not exist. Create the web UI virtualenv first." >&2
  exit 1
fi

"${VENV_PYTHON}" -m pip install \
  --index-url https://download.pytorch.org/whl/cpu \
  "torch==2.7.1+cpu" \
  "torchaudio==2.7.1+cpu"

"${VENV_PYTHON}" -m pip install "setuptools<81"

"${VENV_PYTHON}" -m pip install \
  txtsplit \
  cached_path \
  "transformers==4.46.3" \
  "num2words==0.5.12" \
  "unidic_lite==1.0.8" \
  "mecab-python3==1.0.9" \
  fugashi \
  "pykakasi==2.2.1" \
  "g2p_en==2.1.0" \
  "anyascii==0.3.2" \
  "jamo==0.4.1" \
  "gruut[de,es,fr]==2.2.3" \
  "librosa==0.9.1" \
  "pydub==0.25.1" \
  "eng_to_ipa==0.0.2" \
  "inflect==7.0.0" \
  "unidecode==1.3.7" \
  "pypinyin==0.50.0" \
  "cn2an==0.5.22" \
  "jieba==0.42.1" \
  soundfile \
  tqdm

"${VENV_PYTHON}" -m pip install --no-deps "git+https://github.com/myshell-ai/MeloTTS.git"

"${VENV_PYTHON}" - <<'PY'
import os
import nltk

download_dir = os.path.expanduser("~/nltk_data")
for package in ("averaged_perceptron_tagger", "averaged_perceptron_tagger_eng", "cmudict"):
    nltk.download(package, download_dir=download_dir)
PY
