#!/usr/bin/env bash

# fix segmentation fault reported in https://github.com/k2-fsa/icefall/issues/674
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

set -eou pipefail

stage=0
stop_stage=0

. shared/parse_options.sh || exit 1

# All files generated by this script are saved in "data".
# You can safely remove "data" and rerun this script to regenerate it.
mkdir -p data

log() {
  # This function is from espnet
  local fname=${BASH_SOURCE[1]##*/}
  echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}


if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
  log "stage 0: Download whisper-large-v2 aishell 1 fbank feature from huggingface"

  # pip install huggingface_hub['cli']
  # for aishell 1
  huggingface-cli download --repo-type dataset --local-dir data yuekai/aishell_whisper_fbank_lhotse

fi

if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
  log "stage 1: Download whisper-large-v2 multi-hans-zh fbank feature from huggingface"

  # for multi-hans-zh
  huggingface-cli download --repo-type dataset --local-dir data/fbank yuekai/wenetspeech_whisper_fbank_lhotse
  huggingface-cli download --repo-type dataset --local-dir data/fbank yuekai/multi_hans_zh_whisper_fbank_lhotse
  huggingface-cli download --repo-type dataset --local-dir data/fbank yuekai/alimeeting_aishell4_training_whisper_fbank_lhotse
  mkdir data/fbank/wenetspeech
  mv data/fbank/cuts_L_fixed.jsonl.gz      data/fbank/wenetspeech/
  mv data/fbank/cuts_DEV_fixed.jsonl.gz    data/fbank/wenetspeech/
  mv data/fbank/cuts_TEST_MEETING.jsonl.gz data/fbank/wenetspeech/
  mv data/fbank/cuts_TEST_NET.jsonl.gz     data/fbank/wenetspeech/
  mv data/fbank/L_split_100                data/fbank/wenetspeech/
  mv data/fbank/feats_DEV.lca              data/fbank/wenetspeech/
  mv data/fbank/feats_TEST_MEETING.lca     data/fbank/wenetspeech/
  mv data/fbank/feats_TEST_NET.lca         data/fbank/wenetspeech/
fi

if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  log "stage 2: Download whisper-large-v2 speechio test sets fbank feature from huggingface"

  # for speechio test sets
  mkdir data_speechio
  huggingface-cli download --repo-type model --local-dir data_speechio yuekai/icefall_asr_speechio
  mv data_speechio/fbank/* data/fbank
  rm -rf data_speechio
fi
