#!/usr/bin/env perl

while (<>) {
    # {} around upper case letters in titles
    if (/\ttitle = \{[^\{]/) { s/[A-Z]/\{$&\}/g; }
    if (/\tbooktitle = \{[^\{]/) { s/[A-Z]/\{$&\}/g; }
    if (/\tjournal = \{[^\{]/) { s/[A-Z]/\{$&\}/g; }
    # Unescape & in urls
    if (/\turl = \{/) { s/\\&/&/g; }
    # fix \{} in urls
    if (/\turl = \{/) { s/\\~\{\}/~/g; }
    # fix \_ in urls and DOIs
    if (/\turl = \{/) { s/\\_/_/g; }
# new \doi is not verbatim-like
#    if (/\tdoi = \{/) { s/\\_/_/g; }
    # remove DOI URL prefix from doi field
    if (/\tdoi = \{/) { s/http:\/\/dx\.doi\.org\///g; }
    # Some author names which CiteULike messes up
    if (/\t(author|editor) = \{[^\{]/) { 
      s/El-Dada/El Dada/g;
      s/Peyton-Jones/Peyton Jones/g;
      s/Mc([a-z])/Mc\u\1/g;
      s/Mac(c)(artney)/Mac\u\1\2/g;
      s/D'haro/d'Haro/g; 
      s/Meza-Moreno/Meza Moreno/g; 
    }
    # Don't include Amazon URLs
    if (/\turl = \{http:\/\/www.amazon/) { $_ = ''; }

    print;
}
