#!/usr/bin/env bash
# Set bash to 'debug' mode, it will exit on :
# -e 'error', -u 'undefined variable', -o ... 'error in pipeline', -x 'print commands',
set -e
set -u
set -o pipefail

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


stage=1
stop_stage=100000
data_url=www.openslr.org/resources/12
#train_set="train_360"
train_dev="dev"

log "$0 $*"
. utils/parse_options.sh

if [ $# -eq 3 ]; then
  LIBRISPEECH=$1
  LIBRISPEECH_anon=$2
  anon_data_suffix=$3
fi

if [ $# -eq 1 ]; then
  LIBRISPEECH=$1
fi

echo $*

#. ./db.sh
. ./path.sh
. ./cmd.sh


#if [ $# -ne 0 ]; then
#    log "Error: No positional arguments are required."
#    exit 2
#fi

if [ -z "${LIBRISPEECH}" ]; then
    log "Fill the value of 'LIBRISPEECH' of db.sh"
    exit 1
fi

if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
    if [ ! -e "${LIBRISPEECH}/LibriSpeech/LICENSE.TXT" ]; then
	echo "stage 1: Data Download to ${LIBRISPEECH}"
	for part in dev-clean test-clean dev-other test-other train-clean-360; do
            local/download_and_untar.sh ${LIBRISPEECH} ${data_url} ${part}
	done
    else
        log "stage 1: ${LIBRISPEECH}/LibriSpeech/LICENSE.TXT is already existing. Skip data downloading"
    fi
fi

if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
    log "stage 2: Data Preparation"
    
    for part in dev-clean test-clean dev-other test-other train-clean-360; do
        # use underscore-separated names in data directories.
	local/data_prep.sh ${LIBRISPEECH}/LibriSpeech/${part} data/${part//-/_}
    done
    # anon dir and suffix is not empty
    if [ $# -eq 3 ]; then
	mkdir -p data/train_clean_360_${anon_data_suffix}
	cp data/train_clean_360/* data/train_clean_360_${anon_data_suffix}
	find ${LIBRISPEECH_anon}/ -type f -name *wav > data/train_clean_360_${anon_data_suffix}/wav.txt
	python local/data_prep_anon.py data/train_clean_360_${anon_data_suffix}/wav.txt data/train_clean_360_${anon_data_suffix}/wav.scp
	
	utils/fix_data_dir.sh data/train_clean_360_${anon_data_suffix}
    fi
fi

if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
    log "stage 3: combine all development sets"
    utils/combine_data.sh --extra_files utt2num_frames data/${train_dev} data/dev_clean data/dev_other
fi

if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
    # use external data
    if [ ! -e data/local/other_text/librispeech-lm-norm.txt.gz ]; then
	log "stage 4: prepare external text data from http://www.openslr.org/resources/11/librispeech-lm-norm.txt.gz"
        wget http://www.openslr.org/resources/11/librispeech-lm-norm.txt.gz -P data/local/other_text/
    fi
    if [ ! -e data/local/other_text/text ]; then
	# provide utterance id to each texts
	# e.g., librispeech_lng_00003686 A BANK CHECK
	zcat data/local/other_text/librispeech-lm-norm.txt.gz | \
	    awk '{ printf("librispeech_lng_%08d %s\n",NR,$0) } ' > data/local/other_text/text
    fi
fi

log "Successfully finished. [elapsed=${SECONDS}s]"
