fs = require('fs')
coffee = require('coffee-script')

# loads the file and returns the primary class from it
# the primary class is the class whose name matches the filename
# the matching process is to remove all '_' and '-' from the filename
# and look for class in the source file that matches (ignoring case)
load = (path, file)->	
	source = fs.readFileSync("#{path}/#{file}", 'utf8')
	className = file.replace('-', '').replace('_', '').replace('.coffee', '').toLowerCase()
	
	match = source.match(new RegExp("class (#{className})\\s", 'i'))
	className = match[1]

	cls = coffee.eval(source + "\n\n#{className}")
	return cls

exports.loadServices = (app, path)->
	services = {}
	for file in fs.readdirSync(path)
		cls = load(path, file)
		services[cls.name] = new cls(app)
	services

