# -*- coding: utf-8 -*-
"""
ATSARGIAI 
Sis skriptas yra quick and dirty sprendimas, potencialiai gali ka nors sugadinti. 
Visada redaguokite tik pirmini BIB faila, ir naudokite si skripta tik tam kad is jo sugeneruoti patvarkyta versija


Pasalina number atributa is citavimu (suvienodinti formatavimui), pabando pataisyti en dash puslapiu diapazonose (is - i --)
ir galima prideti keliu kitu dalyku pataisyma


"""

fn = 'ThesisTemplate_bib.bib'

fno = 'ThesisTemplate_bib_denum.bib'

#Also optimise pages with ndash


with open(fn, 'r', encoding = 'utf-8') as f:
    BIB0 = f.read()
    
    
BIB0L = BIB0.split('\n')
BIB1L = []


for jj, bl in enumerate(BIB0L):
    blls = bl.lower().strip()
    bllsns = blls.replace(' ','')
    if not 'number={' in bllsns:
        if 'pages={' in bllsns:
            # Fix for proper en-dash interval
            bls1 = bl.split('{')
            bls2 = bls1[1].split('}')
            pgstro = bls2[0]
#            if pgstro.strip()[-1] == '-':
#            print(pgstro)
            pgstr = pgstro.replace(' ','').replace('--','-').replace('-','--')
#            print(pgstr)
            bln = bls1[0] + '{' + pgstr + '}' + bls2[1]
#            print(bl)
#            print(bln)
#            print(' ')
            BIB1L.append(bln)
        elif 'title={' in bllsns:
            #Keeping Kerr capitalized by converting to {K}err
            bls1 = bl.split('= {')
            bls2 = bls1[1][:-2]
            pgstro = bls2
            pgstr = pgstro.replace(' kerr',' {K}err').replace(' Kerr',' {K}err')
            bln = bls1[0] + '= {' + pgstr + '},'
            BIB1L.append(bln)
        else:
            BIB1L.append(bl)
            
#        
BIB1 = '\n'.join(BIB1L)

with open(fno, 'w', encoding = 'utf-8') as f:
    f.write(BIB1)
    
    
    
    
    