#!/usr/bin/python

import re
import sys
import requests

reformattedAttribute = ""

outputList = {}
templateOrder = ['image', 'website', 'title', 'author', 'author1', 'author2', 'author3', 'author4', 'author5', 'author6', 'author7', 'author8', 'author9', 'author10', 'author11', 'author12', 'author13', 'author14', 'author15', 'author16', 'author17', 'author18', 'author19', 'author20', 'author21', 'author22', 'author23', 'author24', 'author25', 'author26', 'author27', 'author28', 'author29', 'author30', 'author31', 'author32', 'author33', 'author34', 'author35', 'author36', 'author37', 'author38', 'author39', 'author40', 'publisher', 'pubcode', 'price', 'released', 'expected', 'type', 'binding', 'pages', 'isbn', 'rules', 'series', 'chapters', 'follows', 'precedes', 'awards', 'errata', 'enhance', 'gallery']

def startLine(thisAttribute):
    return {
        'image'    : '| image     = ',
        'website'  : '| website   = ',
        'title'    : '| title     = ',
        'author'   : '| author    = ',
        'author1'  : '| author1   = ',
        'author2'  : '| author2   = ',
        'author3'  : '| author3   = ',
        'author4'  : '| author4   = ',
        'author5'  : '| author5   = ',
        'author6'  : '| author6   = ',
        'author7'  : '| author7   = ',
        'author8'  : '| author8   = ',
        'author9'  : '| author9   = ',
        'author10' : '| author10  = ',
        'author11' : '| author11  = ',
        'author12' : '| author12  = ',
        'author13' : '| author13  = ',
        'author14' : '| author14  = ',
        'author15' : '| author15  = ',
        'author16' : '| author16  = ',
        'author17' : '| author17  = ',
        'author18' : '| author18  = ',
        'author19' : '| author19  = ',
        'author20' : '| author20  = ',
        'author21' : '| author21  = ',
        'author22' : '| author22  = ',
        'author23' : '| author23  = ',
        'author24' : '| author24  = ',
        'author25' : '| author25  = ',
        'author26' : '| author26  = ',
        'author27' : '| author27  = ',
        'author28' : '| author28  = ',
        'author29' : '| author29  = ',
        'author30' : '| author30  = ',
        'author31' : '| author31  = ',
        'author32' : '| author32  = ',
        'author33' : '| author33  = ',
        'author34' : '| author34  = ',
        'author35' : '| author35  = ',
        'author36' : '| author36  = ',
        'author37' : '| author37  = ',
        'author38' : '| author38  = ',
        'author39' : '| author39  = ',
        'author40' : '| author40  = ',
        'publisher': '| publisher = ',
        'pubcode'  : '| pubcode   = ',
        'price'    : '| price     = ',
        'released' : '| released  = ',
        'expected' : '| expected  = ',
        'type'     : '| type      = ',
        'binding'  : '| binding   = ',
        'pages'    : '| pages     = ',
        'isbn'     : '| isbn      = ',
        'rules'    : '| rules     = ',
        'series'   : '| series    = ',
        'chapters' : '| chapters  = ',
        'follows'  : '| follows   = ',
        'precedes' : '| precedes  = ',
        'awards'   : '| awards    = ',
        'errata'   : '| errata    = ',
        'enhance'  : '| enhance   = ',
        'gallery'  : '| gallery   = '
    }.get(thisAttribute, 'ERROR')

def dedupeLinks(thisValue):
    leftOfLink = re.search('(?<=\[\[).*(?=\|)',thisValue)
    rightOfLink = re.search('(?<=\|).*(?=\]\])',thisValue)
    if leftOfLink and rightOfLink:
        if leftOfLink.group(0) == rightOfLink.group(0):
            return "[[" + re.search('(?<=\[\[).*(?=\|)',thisValue).group(0) + "]]" + '\n'
    return

def valueFixer(thisAttribute,thisValue):
    if thisAttribute == 'image':
        return fixImage(thisValue)
    if thisAttribute == 'website':
        return fixWebsite(thisValue)
    if thisAttribute == 'title':
        return fixTitle(thisValue)
    if thisAttribute == 'author':
        return fixAuthor(thisValue)
    if re.search('author\d+',thisAttribute):
        return fixAuthorn(thisValue)
    if thisAttribute == 'publisher':
        return fixPublisher(thisValue)
    if thisAttribute == 'pubcode':
        return fixPubcode(thisValue)
    if thisAttribute == 'price':
        return fixPrice(thisValue)
    if thisAttribute == 'released':
        return fixReleased(thisValue)
    if thisAttribute == 'expected':
        return fixExpected(thisValue)
    if thisAttribute == 'type':
        return fixType(thisValue)
    if thisAttribute == 'binding':
        return fixBinding(thisValue)
    if thisAttribute == 'pages':
        return fixPages(thisValue)
    if thisAttribute == 'isbn':
        return fixISBN(thisValue)
    if thisAttribute == 'rules':
        return fixRules(thisValue)
    if thisAttribute == 'series':
        return fixSeries(thisValue)
    if thisAttribute == 'chapters':
        return fixChapters(thisValue)
    if thisAttribute == 'follows':
        return fixFollows(thisValue)
    if thisAttribute == 'precedes':
        return fixPrecedes(thisValue)
    if thisAttribute == 'awards':
        return fixAwards(thisValue)
    if thisAttribute == 'errata':
        return fixErrata(thisValue)
    if thisAttribute == 'enhance':
        return fixEnhance(thisValue)
    if thisAttribute == 'gallery':
        return fixGallery(thisValue)
    return 'ERROR'
    
def fixTitleLink(thisValue):
    if not dedupeLinks(thisValue):
        return thisValue
    else:
        return "''" + dedupeLinks(thisValue) + "''"
    
def fixImage(thisValue):
    return thisValue

def fixWebsite(thisValue):
    r = requests.get(thisValue,allow_redirects=False)
    if r.status_code == 301:
        return "http://paizo.com" + r.headers['location'] + '\n'
    else:
        return thisValue

def fixTitle(thisValue):
    return thisValue

def fixAuthor(thisValue):
    return thisValue

def fixAuthorn(thisValue):
    return thisValue

def fixPublisher(thisValue):
    if re.search('(?<=\[\[)Paizo Publishing(?=\|)',thisValue) or re.search('(?<=\|)Paizo Publishing(?=\]\])',thisValue) or re.search('(?<=\[\[)Paizo Publishing(?=\]\])',thisValue):
        return "[[Paizo Publishing, LLC]]\n"
    else:
        return thisValue

def fixPubcode(thisValue):
    return thisValue

def fixPrice(thisValue):
    return thisValue

def fixReleased(thisValue):
    return thisValue

def fixExpected(thisValue):
    return thisValue

def fixType(thisValue):
    return thisValue

def fixBinding(thisValue):
    return thisValue

def fixPages(thisValue):
    return thisValue

def fixISBN(thisValue):
    return thisValue

def fixRules(thisValue):
    return thisValue

def fixSeries(thisValue):
    return thisValue

def fixChapters(thisValue):
    return thisValue

def fixFollows(thisValue):
    return fixTitleLink(thisValue)

def fixPrecedes(thisValue):
    return fixTitleLink(thisValue)

def fixAwards(thisValue):
    return thisValue

def fixErrata(thisValue):
    return thisValue

def fixEnhance(thisValue):
    return thisValue

def fixGallery(thisValue):
    return thisValue

print("{{Adventure")

with open(sys.argv[1]) as template:
    for line in template:
        thisLine = line.partition("=")
        if thisLine[1] and thisLine[0].lstrip(' |').rstrip(' ') in templateOrder:
            thisAttribute = thisLine[0].lstrip(' |').rstrip(' ')
            thisValue = thisLine[2].lstrip(' |').rstrip(' ')
            outputLine = startLine(thisAttribute) + valueFixer(thisAttribute,thisValue)
            outputList[thisAttribute] = outputLine

if re.search('20[14-15]',outputList['released']):
    print outputList['publisher']
else:
    print "[[Paizo Inc.]]"
            
for attribute in templateOrder:
    if attribute in outputList:
        print outputList[attribute],

print("}}")

