#!/bin/bash

if [ ! -f story.txt ]; then
    touch story.txt
fi

leafpad story.txt
read -p "Please edit and save a new story. Then press enter to continue."

cd ..
python3 text2annotation.py workspace/story.txt ner_model.pkl rel_model.pkl
cd -

if [ -d ~/brat/data/workspace ]; then
    rm ~/brat/data/workspace/*txt
    rm ~/brat/data/workspace/*ann
else
    mkdir ~/brat/data/workspace
    cp ~/brat/data/training/annotation.conf ~/brat/data/workspace
fi

cp story_processed.txt story_processed.ann ~/brat/data/workspace
cp annotation.conf ~/brat/data/workspace

cd ~/brat

python3 standalone.py &
PID=$!

echo "Please visit http://0.0.0.0:8001/index.xhtml#/workspace/story_processed"
echo "login with user:au1/pass:au1 and make your changes, and then press enter here"
google-chrome --no-sandbox 'http://0.0.0.0:8001/index.xhtml#/workspace/story_processed'
cd -
read
kill $PID

echo "Copying files over to current directory"

cp -f ~/brat/data/workspace/story_processed.txt .
cp -f ~/brat/data/workspace/story_processed.ann .

cd ..
python3 annotation2script.py workspace/story_processed.ann workspace/story_quotes.json
echo "Please edit the transcript .xls file if necessary. If you want to replace the voices, just record your voice and store them"
echo "using the filenames under column 'filename' in transcript.xls. Do the same for sfx.xls. Press enter here when you're done"
libreoffice-calc workspace/transcript.xls
libreoffice-calc workspace/sfx.xls
cd -
read

echo "Generated script is at story_processed_script.txt"
cat story_processed_script.txt
