#!/usr/bin/env python
# this small utility takes 'README' file from stellarium
# and formats it properly for 'copyright' file in debian directory

from os.path import join, abspath, dirname
import sys

this = dirname(abspath(__file__))
readme = join(this, '..', 'README')

lines = open(readme, 'rb').readlines()

matches = [ (i, l) for i, l in enumerate(lines) if l.startswith('8. FULL REFERENCES') ]
assert len(matches) == 1

lineno, _ = matches[0]

license = []
for line in lines[lineno-1:]:
    line = line.rstrip()
    if line == '': # empty lines
        license.append(' .')
    else:
        license.append('    %s' % line.replace('\t', '    '))

# now find the proper place to inject this

copyright = open('copyright', 'rb').readlines()
copyright = [ l.rstrip() for l in copyright ]
matches = [ (i, l) for i, l in enumerate(copyright) if '8. FULL REFERENCES' in l ]
lineno, _ = matches[0]
copyright = copyright[:lineno-1]

final = copyright + license

open('copyright', 'wb').write('\n'.join(final))


# print copyright

