#!/usr/bin/env python
import os
import sys
import json
import cgi
import cgitb
from configuration import Configuration
import ConfigParser
cgitb.enable() 
fs = cgi.FieldStorage()

sys.stdout.write("Content-Type: application/json")

sys.stdout.write("\n")
sys.stdout.write("\n")
PROJECT_LOCATION = os.getcwd()

result = {}
result['success'] = True
result['message'] = "The command Completed Successfully"
result['keys'] = ",".join(fs.keys())

path = os.path.join(PROJECT_LOCATION,'synfig-scene-builder.config')
config= ConfigParser.RawConfigParser()
config.read(path)
for k in fs.keys():
    groups = k.split('[')
    key = str(groups[2])[:-1]
    value = fs.getvalue(k)     
    config.set('General',key,value)

with open(path, 'wb') as configfile:
    config.write(configfile)

sys.stdout.write(json.dumps(result,indent=1))
sys.stdout.write("\n")

sys.stdout.close()
