################################################################################
# plrefgen.py (v1 --- 4/16/2012)
# Author: Peter-Michael Osera (posera[at]cis[dot]upenn[dot]edu)
#
# Generates crossref entries for a number of PL conferences.  The resulting
# bibtex file is intended to be used to refer to conference proceedings in a
# standardized way, e.g.,
#
# @inproceedings{example00,
#   title = 'An example paper',
#   author = 'John Doe',
#   crossref = icfp:2010
# }
#
# The resulting bibtex file contains one entry for each conference-year pair
# of the form
#
# <conference>:<year>
#
# which can then be referenced in your actual bibliography.
################################################################################

import datetime

ordinal_suffixes = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']

def get_ordinal_suffix(n):
    ones = n % 10
    tens = n // 10 % 10
    if tens == 1:
        return 'th'
    else:
        return ordinal_suffixes[ones]

def get_ordinal(n):
    return str(n) + get_ordinal_suffix(n)

def get_booktitle(prefix, nth, suffix):
    return prefix + ' ' + nth + ' ' + suffix

def print_entry(key, booktitle, year):
    print('@conference{' + key + ':' + str(year) + ',')
    print('  booktitle = \"' + booktitle + '\",')
    print('  year = ' + str(year) + ',')
    print('}')

def print_header(key):
    print('%%%%% ' + key + ' %%%%%')

def print_preamble():
    now = datetime.datetime.now()
    print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    print('% PL conference cross-references')
    print('% (Automatically generated by plrefgen.py on ' + now.strftime('%Y-%m-%d %H:%M'))
    print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    print()

###### haskell ######
haskell_data = [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                2010, 2011]

def print_haskell():
    print_header('haskell')
    for n in range(len(haskell_data)):
        year = haskell_data[n]
        prefix = 'Proceedings of the'
        suffix = '{ACM} {SIGPLAN} {W}orkshop on {H}askell'
        print_entry('haskell', get_booktitle(prefix, get_ordinal(n+1), suffix), year)

###### icfp ######
icfp_data = [1996, 1997, 1998, 1999,
             2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
             2010, 2011]

def print_icfp():
    print_header('icfp')
    for n in range(len(icfp_data)):
        year = icfp_data[n]
        prefix = 'Proceedings of the'
        suffix = '{ACM} {SIGPLAN} {I}nternational {C}onference on {F}unctional {P}rogramming'
        print_entry('icfp', get_booktitle(prefix, get_ordinal(n+1), suffix), year)

###### icse ######
icse_data = [1976, 1978, 1789,
             1981, 1982, 1984, 1985, 1987, 1988, 1989,
             1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
             2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
             2010, 2011]

def print_icse():
    print_header('icse')
    for n in range(len(icse_data)):
        year = icse_data[n]
        prefix = 'Proceedings of the'
        suffix = '{I}nternational {C}onference {O}n {S}oftware {E}ngineering'
        # Odd, first entry for icse (1976) starts at the 2nd conference...
        print_entry('icse', get_booktitle(prefix, get_ordinal(n+2), suffix), year)

###### oopsla ######
oopsla_data = [1986, 1987, 1988, 1989,
               1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
               2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
               2010, 2011]

def print_oopsla():
    print_header('oopsla')
    for n in range(len(oopsla_data)):
        year = oopsla_data[n]
        prefix = 'Proceedings of the'
        suffix = 'on {O}bject-oriented {P}rogramming {S}ystems, {L}anguages, and {A}pplications ({OOPSLA})'
        if year >= 1996:
            suffix = '{ACM} {SIGPLAN} ' + suffix
        print_entry('oopsla', get_booktitle(prefix, get_ordinal(n+1), suffix), year)

###### pldi ######
pldi_data = [1988, 1989,
             1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
             2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
             2010, 2011]

def print_pldi():
    print_header('pldi')
    for n in range(len(pldi_data)):
        year = pldi_data[n]
        prefix = 'Proceedings of the'
        suffix = '{ACM} {SIGPLAN} {C}onference on {P}rogramming {L}anguage {D}esign and {I}mplementation {(PLDI)}'
        print_entry('pldi', get_booktitle(prefix, str(year), suffix), year)

##### plpv #####
plpv_data = [2007, 2009, 2010, 2011, 2012]

def print_plpv():
    print_header('plpv')
    for n in range(len(plpv_data)):
        year = plpv_data[n]
        prefix = 'Proceedings of the'
        suffix = '{ACM} {SIGPLAN} {W}orkshop on {P}rogramming {L}anguages {M}eets {P}rogram {V}erification'
        print_entry('plpv', get_booktitle(prefix, get_ordinal(n+1), suffix), year)

###### popl ######
popl_data = [1973, 1975, 1976, 1977, 1978, 1979,
             1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989,
             1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
             2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
             2010, 2011, 2012]

def print_popl():
    print_header('popl')
    for n in range(len(popl_data)):
        year = popl_data[n]
        if year == 2006:
            prefix = 'Conference record of the'
        else:
            prefix = 'Proceedings of the'
        suffix = '{ACM} {SIGACT-SIGPLAN} {S}ymposium on {P}rinciples of {P}rogramming {L}anguages {(POPL)}'
        if year < 1975 or year > 2007:
            suffix = '{A}nnual ' + suffix
        print_entry('popl', get_booktitle(prefix, get_ordinal(n+1), suffix), year)

def main():
    print_preamble()
    print_icse()
    print_haskell()
    print_icfp()
    print_oopsla()
    print_pldi()
    print_plpv()
    print_popl()

if __name__ == "__main__":
    main()
