#!/usr/bin/env python

import sys
import os

if len(sys.argv) == 1:
    print("I need a config.py (filename) to convert.")
    sys.exit(2)

filename = sys.argv[1]

print("Processing:", filename)

yamlout = filename
pos = yamlout.rindex('.')
if pos != -1:
    yamlout = yamlout[0:pos+1] + "yaml"
else:
    print("I can't determine the extension.", filename)
    sys.exit(2)

if os.path.exists(yamlout):
    print("Sorry, the file {0} already exists.".format(yamlout))
    sys.exit(2)

print("Converting {0} to {1}...".format(filename, yamlout))
with open(filename, 'r') as fp:
    with open(yamlout, 'w') as fpout:
        for line in fp:
            line = line.strip()

            if " = " in line:
                var, _, data = line.partition('=')
                var = var.strip().lower()
                line = "{0}: {1}".format(var, data.strip())

            print("{0}".format(line), file=fpout)

print("Done")