#!/usr/bin/python
# -*- coding: utf-8 -*-

"""

Convert pdftable converted CSV files of the post journal from SiOA to
a unified database with date, doc #, name, author and application.

"""

import csv
import sys
import os

fieldseen = {}

debug = False

order = ['Doc #',
         'Endret dato',
         'Tittel',
         'Saksbehandler',
         'Applikasjon',
         'List Title']

def log(msg):
    if debug:
        print(msg)

def main():
    data = []
    for filename in sys.argv[1:]:
        log("Reading " + filename)
        with open(filename, 'r') as csvfile:
            csvdata = csv.reader(csvfile, delimiter=',', quotechar='"')
            nextline = csvdata.next()
            if '' == nextline[0]:
                title = nextline[1]
                header = csvdata.next()
            else:
                title = os.path.basename(filename).replace('.csv', '')
                header = nextline
            log(title)
            log(header)
            for row in csvdata:
                log(row)
                import re
                if ( ('' == row[0] and '' == row[2])
                     or re.match(r'\d+:\d+:\d+', row[0]) ):
                    log("== continuation data row")
                    log(dataline)
                    dataline['Tittel'] += row[1]
                    dataline['Endret dato'] += row[0]
                    fieldseen['Tittel'] = 1
                    fieldseen['Endret dato'] = 1
                    log(dataline)
                    continue
                elif '' == row[2]:
                    # Contuation header row
                    header[0] += row[0]
                    log("header continuation")
                    continue
                elif ('SiOs postliste' in row[2]
                      or 'Endret dato' in row[0]):
                    # Handle several pages, skip new header
                    log("new page")
                    continue
                else:
                    log("** data line")
                    dataline = { 'List Title' : title }
                    fieldseen['List Title'] = 1
                    for i in range(0, len(row)):
                        if -1 != header[i].find('Dok #'):
                            (num, doctitle) = row[i].split(" ", 1)
                            dataline['Doc #'] = num
                            dataline['Tittel'] = doctitle
                            fieldseen['Doc #'] = 1
                            fieldseen['Tittel'] = 1
                        else:
                            # Hack to work around inconsistent lists
                            if 'Sist endret dato' == header[i] or\
                               'Sist endret d' == header[i]:
                                header[i] = 'Endret dato'
                            if 'Forfatternavn' == header[i]:
                                header[i] = 'Saksbehandler'
                            fieldseen[header[i]] = 1
                            dataline[header[i]] = row[i]
                log(dataline)
                if 'Doc #' not in dataline:
                    sys.stderr.write("error:" + str(dataline) + "\n")
                else:
                    data.append(dataline)

    print("\t".join(order))
    for entry in sorted(data, key=lambda x: x['Doc #']):
        row = []
        log(str(entry))
        for field in order:
            if field in entry:
                if -1 != entry[field].find('"'):
                    entry[field] = '"%s"' % entry[field].replace('"', '""')
                row.append(entry[field])
        print("\t".join(row))
    return data

if __name__ == '__main__':
    data = main()
#    print fieldseen.keys()
