#!/usr/bin/python
# -*- coding: utf-8 -*-

import cgi, cgitb, os, sys
import json

cgitb.enable()
#cgi.test()



dirName = "/usr/share/apache2/htdocs/preset"
if not os.path.exists(dirName):
    # os.makedirs(dirName)
    # os.system("mkdir " + dirName);
    # os.system("chmod 777 " + dirName);
    
    print "Content-type:text/html\r\n\r\n"
    print("Preset Directory Does Not Exist")
else:
    os.chdir(dirName)
    files = {}
    id = 0
    for file in os.listdir("."):
        files[id] = file
        id += 1
        
    print 'Content-Type: application/json\n\n'
    print json.dumps(files)

#param_file_out = '/usr/share/apache2/htdocs/config/fileList.json'
#fout = open(param_file_out,'w')

#out = json.dumps(files)
#out = out.replace(',',',\n\t')
#out = out.replace('{','{\n\t')
#out = out.replace('}','\n}\n')
#out = out.replace(':',' :')

#with fout as json_file:
#	json_file.write(out)
#json_file.close()
