#!/usr/bin/env bash

set -eou pipefail

stage=-1
stop_stage=100

# We assume dl_dir (download dir) contains the following
# directories and files. If not, they will be downloaded
# by this script automatically.
#
#  - $dl_dir/aidatatang_200zh
#      You can find "corpus" and "transcript" inside it.
#      You can download it at
#       https://openslr.org/62/

dl_dir=$PWD/download

. 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]}) $*"
}

log "dl_dir: $dl_dir"

if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
  log "Stage 0: Download data"

  if [ ! -f $dl_dir/aidatatang_200zh/transcript/aidatatang_200_zh_transcript.txt ]; then
    lhotse download aidatatang-200zh $dl_dir
  fi
fi

if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
  log "Stage 1: Prepare manifest"
  # We assume that you have downloaded the aidatatang_200zh corpus
  # to $dl_dir/aidatatang_200zh
  if [ ! -f data/manifests/.aidatatang_200zh_manifests.done ]; then
    mkdir -p data/manifests
    lhotse prepare aidatatang-200zh $dl_dir data/manifests
    touch data/manifests/.aidatatang_200zh_manifests.done
  fi
fi

if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  log "Stage 2: Process aidatatang_200zh"
  if [ ! -f data/fbank/.aidatatang_200zh_fbank.done ]; then
    mkdir -p data/fbank
    ./local/compute_fbank_aidatatang_200zh.py
    touch data/fbank/.aidatatang_200zh_fbank.done
  fi
fi
