/*
 * grunt-pot
 * https://github.com/stephenharris/grunt-potW
 *
 * Copyright (c) 2013 Stephen Harris
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  grunt.registerMultiTask('pot', 'Scan files and create a .pot file using xgettext', function() {

	var dest = false, inputFiles = "", headerOptions ="";

	var pkg = grunt.file.readJSON('package.json');

    var options = this.options({
		dest: false,
		overwrite: true,
		keywords: false,
		language: false,
		encoding: false,
		text_domain: 'messages',
		package_version: pkg.version,
		package_name: pkg.name,
		msgid_bugs_address: false,
		omit_header: false,
		copyright_holder: false,
		comment_tag: '/',
		msgmerge: false,
	});

	grunt.verbose.writeflags(options, 'Pot options');

	var potFile = options.dest;
	//If destination is a directory, build a file based on text domain
	if( options.dest && detectDestType(options.dest) === 'directory' ) {
		potFile = options.dest.replace(/\/$/, "") + "/"+options.text_domain+".pot";
        }

	if( !grunt.file.exists(potFile) ){
		grunt.file.write(potFile);
	}

	grunt.log.writeln('Destination: ' + potFile);

	//Set join mode
	var join = ( !options.overwrite ? " --join-existing" : "" );

	//Implode keywards
	var keywords = ( options.keywords ? " --keyword=" + options.keywords.join( " --keyword=" ) : "" );

	//Set input files language, if specified
	var language = ( options.language ? " --language="+options.language : "" );

	//Set input files encoding, if required
	var encoding = ( options.encoding ? " --from-code="+options.encoding : "" );

	//Generate header
	if( options.package_version ){
		headerOptions += " --package-version="+options.package_version;
	}

	if( options.package_name ){
		headerOptions += " --package-name="+options.package_name;
	}

	if( options.msgid_bugs_address ){
		headerOptions += " --msgid-bugs-address="+ options.msgid_bugs_address;
	}

	if( options.omit_header ){
		headerOptions += " --omit-header";
	}

	if( options.copyright_holder ){
		headerOptions += " --copyright-holder='"+options.copyright_holder+"'";
	}

	if( options.comment_tag ){
		headerOptions += " --add-comments='"+options.comment_tag+"'";
	}

	//Generate list of files to scan
	this.files.forEach(function(file) {
		inputFiles +=  " " + file.src[0];
	});

	//Compile and run command
	var exec = require('child_process').exec;
	var command = 'xgettext' + join + ' --default-domain=' + options.text_domain + ' -o '+potFile + language + encoding + keywords + headerOptions + inputFiles;
	var done = grunt.task.current.async(); 
	
	grunt.verbose.writeln('Executing: ' + command);
	
	exec( command, 
		function(error, stdout, stderr){

			grunt.verbose.writeln('stderr: ' + stderr);

			//If msmerge-ing then assume .po files are in same location has .pot
			var poFiles = options.dest.replace(/\/$/, "") + "/*.po";

			var poFilePaths = grunt.file.expand( poFiles );

			if( options.msgmerge ){
				var count = poFilePaths ? poFilePaths.length : 0;
				grunt.verbose.writeln( count + " .po files found for msgmerge" );
			}

			if( poFilePaths && options.msgmerge ){

				poFilePaths.forEach( function( poFile ) {
					exec( 'msgmerge -U ' + poFile +' ' + potFile, function(error, stdout, stderr) {});
				});

			}

			done( error ); //error will be null if command executed without errors. 
		}
  	);
	
  });

  var detectDestType = function(dest) {
    if (grunt.util._.endsWith(dest, '/')) {
      return 'directory';
    } else {
      return 'file';
    }
  };

};

@@ -2733,29 +2733,85 @@
 %0A%09%09%09
-//If msmerge-ing then
+if( options.msgmerge )%7B%0A%0A%09%09%09%09//If msmerge-ing then use specified directory or
  ass
@@ -2853,24 +2853,25 @@
 has .pot%0A%09%09%09
+%09
 var poFiles 
@@ -2871,16 +2871,46 @@
 oFiles =
+ ( options.msgmerge === true ?
  options
@@ -2943,19 +2943,69 @@
  %22/*.po%22
+ : options.msgmerge.replace(/%5C/$/, %22%22) + %22/*.po%22 )
 ;%0A
-%0A
+%09
 %09%09%09var p
@@ -3052,35 +3052,8 @@
 );%0A%0A
-%09%09%09if( options.msgmerge )%7B%0A
 %09%09%09%09
@@ -3172,16 +3172,17 @@
 rge%22 );%0A
+%0A
 %09%09%09%7D%0A%0A%09%09

Allow msgmerge to be a string - the path to the directory where the `.po` files live to msgmerged.