#!/bin/bash

if [ $# -ne 4 ]; then
    echo "$0 <langs: en-de> <bpe_dir/> <in_text_dir/> <out_dir/>"
    echo "  - bpe_dir/: the dir with the existing bpe model, codes, vocab"
    echo "  - in_text_dir/: input text dir containing desired text to be processed"
    echo "  - out_dir/: output dir to save the processed text"
    echo "  - required 4 args, given $#"
    exit;
fi
set -e

lang_pair=$1
IFS="-" read -r -a langs <<< ${lang_pair}
echo ${langs[@]}
bpe_dir=$2
in_dir=$3
out_dir=$4

mkdir -pv ${out_dir}

for s in train valid test; do
    for lang in ${langs[@]}; do

        vocab_file=${bpe_dir}/vocab
        #codes_file=${bpe_dir}/mono_pt_codes
        codes_file=${bpe_dir}/codes

        if [ -f ${vocab_file} ] && [ -f ${codes_file} ]; then

            in_tok_file=${in_dir}/${lang}.${s}.tok

            if [ -f ${in_tok_file} ]; then

                out_bpe_file=${out_dir}/${s}.${lang}

                ./tools/fastBPE/fast applybpe ${out_bpe_file} ${in_tok_file} ${codes_file}

                python preprocess.py ${vocab_file} ${out_bpe_file}

                out_par_file=${out_dir}/${s}.${lang_pair}.${lang}.pth
                cp -v ${out_bpe_file}.pth ${out_par_file}

            else
                echo "- FILE NOT FOUND: Input tokenized text - ${in_tok_file} missing."
                exit;
            fi
        else
            echo "- FILE NOT FOUND ERROR: Check if ${vocab_file} and ${codes_file} are present."

        fi

    done
done
