 	# Dist
 	grunt.registerTask 'dist', ['prod', 'copy:dist']
 
+	# Tasks for deploy build
+	grunt.registerTask 'gen-commit', ['clean', 'copy:main', 'copy:test', 'coffee', 'less', 'copy:debug',
+										'useminPrepare', 'concat', 'uglify', 'cssmin', 'usemin', 
+										'copy:commit', 'string-replace:commit']
+
 	# Generates version folder
 	grunt.registerTask 'gen-version', ->
-		grunt.log.writeln 'Deploying to environmentName: '.cyan + grunt.config('environmentName').green
-		grunt.log.writeln 'Deploying to buildNumber: '.cyan + grunt.config('buildNumber').green
-		grunt.log.writeln 'Deploying to environmentType: '.cyan + grunt.config('environmentType').green
-		grunt.log.writeln 'Directory: '.cyan + grunt.config('versionName')().green
-		grunt.log.writeln 'Version set to: '.cyan + grunt.config('gitCommit').green
-		grunt.log.writeln 'Rersource token set to: '.cyan + grunt.config('resourceToken').green
-		grunt.log.writeln 'Deploy folder: '.cyan + grunt.config('deployDirectory').green
-		grunt.task.run ['copy:env', 'string-replace:deploy']
+		grunt.log.writeln 'applicationRoot: '.cyan + grunt.config('applicationRoot').green
+		grunt.log.writeln 'environmentName: '.cyan + grunt.config('environmentName').green
+		grunt.log.writeln 'buildNumber: '.cyan + grunt.config('buildNumber').green
+		grunt.log.writeln 'environmentType: '.cyan + grunt.config('environmentType').green
+		grunt.log.writeln 'Version deploy directory: '.cyan + (path.resolve grunt.config('deployDirectory'), grunt.config('versionName')()).green
+		grunt.task.run ['copy:version', 'string-replace:version']
 
 	# Deploy - creates deploy folder structure
 	grunt.registerTask 'deploy', ->
 		commit = grunt.config('gitCommit')
 		deployDir = path.resolve grunt.config('deployDirectory'), commit
 		deployExists = false
-		grunt.log.writeln 'Version deploy dir set to: '.cyan + deployDir.green
+		grunt.log.writeln 'Commit deploy directory: '.cyan + deployDir.green
 		try
 			deployExists = fs.existsSync deployDir
 		catch e


module.exports = (grunt) ->
	pacha = grunt.file.readJSON('tools/pachamama/pachamama.config')[0]
	# Project configuration.
	grunt.initConfig
		resourceToken: process.env['RESOURCE_TOKEN'] or '/io'
		gitCommit: process.env['GIT_COMMIT'] or 'GIT_COMMIT'
		deployDirectory: path.normalize(process.env['DEPLOY_DIRECTORY'] ? 'deploy')
		relativePath: ''
		pkg: grunt.file.readJSON('package.json')
		pacha: pacha
		acronym: pacha.acronym
		environmentName: process.env['ENVIRONMENT_NAME'] or '1-0-0'
		buildNumber: process.env['BUILD_NUMBER'] or '1'
		environmentType: process.env['ENVIRONMENT_TYPE'] or 'stable'
		versionName: -> [grunt.config('acronym'), grunt.config('environmentName'), grunt.config('buildNumber'),

				singleRun: true

		'string-replace':
			# This replacement treats src and href attributes which cannot contain variables in order for
			# the minification process to work. It adds the applicationRoot tag variables before the files address.
			commit:
				files:
					'<%= deployDirectory %>/<%= gitCommit %>/index.html': ['<%= deployDirectory %>/<%= gitCommit %>/index.html']
					'<%= deployDirectory %>/<%= gitCommit %>/index.debug.html': ['<%= deployDirectory %>/<%= gitCommit %>/index.debug.html']

				options:
					replacements: [
						pattern: /src="(\.\.\/)?(?!http|\/|\/\/|\#|\&|\'\&)/ig
						replacement: 'src="' + tagApplicationRoot
					,
						pattern: /href="(\.\.\/)?(?!http|\/|\/\/|\#|\&|\'\&)/ig
						replacement: 'href="' + tagApplicationRoot
					,
						pattern: '<script src="http://localhost:35729/livereload.js"></script>'
						replacement: ''
					]
			# Here, all tag variables are replaced for production-ready endpoints.
			version:
				files:
					'<%= deployDirectory %>/<%= versionName() %>/index.html': ['<%= deployDirectory %>/<%= versionName() %>/index.html']
					'<%= deployDirectory %>/<%= versionName() %>/index.debug.html': ['<%= deployDirectory %>/<%= versionName() %>/index.debug.html']

				options:
					replacements: [
						pattern: new RegExp(tagApplicationRoot, "gi")
						replacement: '<%= applicationRoot %>/'
					]

		connect:
