#!/bin/bash
# this script file transforms papers.bib into papers.json
# using the "Babel" service

echo "Copying papers.bib to the server (for backup)"
cp papers.bib /Volumes/UAlberta/public_html/papers/

echo "Preprocessing file papers.bib"

# preprocess the bib file by handling the curly braces
# which Babel cannot handle
./preBabel.awk papers.bib > p2.bib

# upload to web
cp p2.bib /Volumes/UAlberta/public_html/papers/

echo "Converting papers.bib with the Babel service"

# now convert with ..
# http://simile.mit.edu/babel/
# and save the result as papers.json.orig

wget "http://simile.mit.edu/babel/translator?reader=bibtex&writer=bibtex-exhibit-json&mimetype=default&url=http://www.ualberta.ca/~szepesva/papers/p2.bib" -O papers.json.orig

echo "Please verify the result!"

# show the resulting file
more papers.json.orig

# the keywords in the resulting file are like "kw1, kw2"
# instead of [ "kw1", "kw2" ]
# So we postprocess the result to result in the final
# file:

echo "Postprocessing.."

./postBabel.awk  papers.json.orig >  papers.json

echo "Copying to server.. (old version is backed up as papers.json.bak)"

# copy the resulting file to the server
cp /Volumes/UAlberta/public_html/papers/papers.json /Volumes/UAlberta/public_html/papers/papers.json.bak
cp papers.json /Volumes/UAlberta/public_html/papers/

echo "Cleaning up.."

# finally remove the junk files
rm p2.bib
rm papers.json.orig
rm /Volumes/UAlberta/public_html/papers/p2.bib

