#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""reverse_splines.py
Reverse splines of a Racer track. (Version 1.0)

2005-05-13 Oliver Pieper, pieper@viaregio.de

Reads the file 'spline.ini' in the current directory and writes the reversed
splines as 'spline.rev.ini'. The script is dumb and expects a "well-formed"
spline.ini, like the ones written by Racer's tracked.
The script was written and tested under Python 2.4 on Windows. Minor changes
might be necessary to get it to run on a different OS or Python version.

License: Do with it as you please, but don't blame me! ;-)"""

INFILE_NAME = 'spline.ini'
OUTFILE_NAME = 'spline.rev.ini'

# read original spline.ini, reverse checkpoints and store in list
infile = file(INFILE_NAME, 'r')
infile.readline()
infile.readline()
num_splines = int(infile.readline().split('=')[1])
infile.readline()
splines = []
for i in range(num_splines):
    infile.readline()
    splines.append([infile.readline()])
    cp0 = infile.readline()
    cp1 = infile.readline()
    splines[len(splines) - 1].append('  cp0=' + cp1[6:])
    splines[len(splines) - 1].append('  cp1=' + cp0[6:])
    splines[len(splines) - 1].append(infile.readline())
    splines[len(splines) - 1].append(infile.readline())
infile.close()
    
# traverse list in reverse order and write new spline.rev.ini
outfile = file('spline.rev.ini', 'w')
outfile.write("""lines
{
  count=%d
}\n""" % num_splines)
splines.reverse()
for i in range(num_splines):
    outfile.write('line%d\n' % i)
    outfile.write(''.join(splines[i]))
outfile.close()

print "done."