#!/usr/bin/env bash

# bash wikilingua_infer_ext.sh mspm4 english ext_finetune english en --summ-langs en_XX

DATAVER="$1"
TRAIN_LG="$2"
NAME="$3"
TEST_LG="$4"
TEST_LG_SYMBOL="$5"

argslist=""
for (( i = 6; i <= $# ; i++ ))
  do
    j=${!i}
    argslist="${argslist} $j "
  done
echo $argslist >&2

cd "$(dirname $0)" || return

echo "Install fairseq" >&2

sudo mkdir -p /usr/lib/python3.7/site-packages/
sudo pip3 install -e fairseq
pip3 install -r requirements.txt -i http://pypi.byted.org/simple/ --trusted-host=pypi.byted.org

export PYROUGE_HOME_DIR=$(pwd)/RELEASE-1.5.5
export PYROUGE_TEMP_PATH=/opt/tiger

pyrouge_set_rouge_path $PYROUGE_HOME_DIR
chmod +x $PYROUGE_HOME_DIR/ROUGE-1.5.5.pl

prefix=hdfs://haruna/home/byte_arnold_hl_mlnlc/user/wuxianze.0

dataset_path=${prefix}/Datasets/multilingual/wikilingua/data-bin/${DATAVER}/${TEST_LG}
checkpoint_path=${prefix}/Workspace/Multilingual/wikilingua/${TRAIN_LG}/checkpoints/${NAME}
tensorboard_logdir=${prefix}/Workspace/Multilingual/wikilingua/${TEST_LG}/logs/${NAME}_${TRAIN_LG}

local_root=~/wikiLingua_${NAME}_${TRAIN_LG}_${DATAVER}_${TEST_LG}
resource_root=${local_root}/resource
output_path=${local_root}/output
model_path=${local_root}/model
mkdir -p ${resource_root}
mkdir -p ${output_path}
mkdir -p ${model_path}

local_dataset_path=${resource_root}/dataset
mkdir -p ${local_dataset_path}
hadoop fs -copyToLocal ${dataset_path}/* ${local_dataset_path}
echo "Download resource from ${dataset_path} to ${local_dataset_path}" >&2

label_path=${prefix}/Datasets/multilingual/wikilingua/${TEST_LG}
hdfs dfs -get ${label_path}/test.${TEST_LG}.label.jsonl ${local_dataset_path}/test.label.jsonl
echo "Download resource from ${label_path}/test.${TEST_LG}.label.jsonl to ${local_dataset_path}/test.label.jsonl" >&2

local_tensorboard_path=${output_path}/tensorboard_logdir
mkdir -p ${local_tensorboard_path}

local_checkpoint_path=${output_path}/checkpoint_path
mkdir -p ${local_checkpoint_path}
hadoop fs -copyToLocal ${checkpoint_path}/checkpoint_best.pt ${local_checkpoint_path}
echo "Load checkpoints from ${checkpoint_path}/checkpoint_best.pt to ${local_checkpoint_path}" >&2

echo "Finish download files" >&2

langs=ar_AR,cs_CZ,de_DE,en_XX,es_XX,et_EE,fi_FI,fr_XX,gu_IN,hi_IN,it_IT,ja_XX,kk_KZ,ko_KR,lt_LT,lv_LV,my_MM,ne_NP,nl_XX,ro_RO,ru_RU,si_LK,tr_TR,vi_VN,zh_CN

echo "Start extraction inference..."

suffix=$(echo "$argslist" | sed -e "s/-//g"  -e "s/  */_/g")

python3 fairseq/inference_ext_summ.py ${local_dataset_path} \
  --path ${local_checkpoint_path}/checkpoint_best.pt \
  --task extractive_summarization \
  --extraction-head-name extraction_head \
  --gen-subset test \
  --source-lang doc --target-lang sum \
  --langs $langs \
  --remove-bpe 'sentencepiece'  \
  --truncate-source \
  --user-dir examples/summarization \
  --max-tokens 2048 \
  $argslist \
  > ${local_tensorboard_path}/"output$suffix"

cat ${local_tensorboard_path}/"output$suffix" | grep -P "^H" | sort -V | cut -f 2- | sed -e  "s/\[[a-z]\{2\}_[A-Z]\{2\}\]//g" > ${local_tensorboard_path}/"test$suffix.hypo"

echo "Load ground truth file from ${prefix}/Datasets/multilingual/wikilingua/${TEST_LG}/test.${TEST_LG}.sum"
hadoop fs -get ${prefix}/Datasets/multilingual/wikilingua/${TEST_LG}/test.${TEST_LG}.sum ${local_dataset_path}

python3 utils/calRouge.py \
  -c ${local_tensorboard_path}/"test$suffix.hypo" \
  -r ${local_dataset_path}/test.${TEST_LG}.sum \
  -l ${TEST_LG_SYMBOL} -d "<q>"

echo "Put ${local_tensorboard_path} to ${tensorboard_logdir}" >&2
hadoop fs -put -f ${local_tensorboard_path}/* ${tensorboard_logdir}/
sleep 600
