/*
 * grunt-wp-i18n
 * https://github.com/cedaro/grunt-wp-i18n
 *
 * Copyright (c) 2015 Cedaro, LLC
 * Licensed under the MIT license.
 */

'use strict';

exports.init = function( grunt ) {
	var exports = {},
		path = require( 'path' );

	/**
	 * Uses gettext msgmerge to merge a .pot file into a .po
	 *
	 * @param from string File to merge from (generally a .pot file)
	 * @param to string File to merge to (generally a .po file)
	 * @param callback function Callback to call after being done
	 */
	exports.msgMerge = function( from, to, callback ) {
		grunt.util.spawn( {
			cmd:  'msgmerge',
			args: [ '--update', '--backup=none', to, from ]
		}, function( error, result, code ) {

			if ( error ) {
				grunt.log.error( 'msgmerge error:' + error );
			} else {
				grunt.log.ok( 'POT file merged into ' + path.relative( process.cwd(), to ) );
			}

			callback();
		} );
	};

	/**
	 * Searches around a .pot file for .po files
	 *
	 * @param potFile string The .pot file to search around
	 * @param type string Type of project, either wp-plugin or wp-theme
	 * @return string[] poFiles around the given potFile
	 */
	exports.searchPoFiles = function( potFile, type ) {
		var searchPath = path.join( path.dirname( potFile ), '*.po' );

		return grunt.file.expand( searchPath );
	};

	return exports;
};
