#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
#from myModule import myModule
import json
import csv
import os
import datetime
#===============================================================================
def createDirectories(datafile, cfgfile, level1="CVE_ENT", level2="CVE_MUN"):
	with open(cfgfile) as cfg_file:
		cfg = json.load(cfg_file)
	with open(datafile) as csvfile:
		data = csv.DictReader(csvfile, delimiter=',')
		for row in data:

			mun = row[level2]
			ent = row[level1]
			dirname = ent.zfill(2)+"-"+mun.zfill(3)
			print(ent)
#			for k in cfg.keys():
#				p[k]=cfg[k]
			print(os.getcwd())
			cfg["projectDir"]=os.getcwd()+"/"+dirname+"/"
#			cfg["endDate"]=datetime.datetime.now().strftime("%Y%m%d")
			cfg["wkt"]=row["WKT"]
			cfg["wkt2"]=row["WKT2"]
			if not os.path.exists(dirname):
				os.mkdir(dirname)
			with open(dirname+'/findProducts.json', 'w') as outfile:
    				json.dump(cfg, outfile)
#===============================================================================
def main(argv):

        if len(sys.argv) != 3 and len(sys.argv) != 5:
                print(len(sys.argv))
                print ("Usage: "+argv[0] + " <csv> <cfg>")
        elif(len(sys.argv) == 3 ):
                createDirectories(argv[1], argv[2])
        else:
                createDirectories(argv[1], argv[2], argv[3], argv[4])



if __name__ == "__main__":
	main(sys.argv)
