/*
 * grunt-gettext-checker
 *
 *
 * Copyright (c) 2014 Incuna Ltd.
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {

    grunt.registerMultiTask('gettext-checker', 'Compares .pot and .po file translation to check for differences', function () {

        // import Nodejs libs
        var _ = require('lodash');
        var poLib = require('pofile');

        // Get task options and merge with defaults
        var options = this.options({
            potFile: 'i18n/template.pot',
            poFile: 'i18n/en-gb.po'
        });

        var checkFileExists = function (path) {
            if (!grunt.file.isFile(path)) {
                grunt.fail.fatal(path + ' does not exist');
            }
        };

        checkFileExists(options.potFile);
        checkFileExists(options.poFile);

        // load .pot file
        var potFileSource = grunt.file.read(options.potFile);
        // load .po file
        var poFileSource = grunt.file.read(options.poFile);

        var potFile = poLib.parse(potFileSource);
        var poFile = poLib.parse(poFileSource);

        var validatePoFile = function (poObject) {
            if (!poObject.headers) {
                grunt.fail.fatal(options.poFile + ' can not be loaded. Check the file is a valid .po file');
            }
        };

        validatePoFile(potFile);
        validatePoFile(poFile);

        var getPoKeys = function (items) {
            return _.map(items, function (item) {
                return item.msgid;
            });
        }

        // get pot file keys as array
        var potKeys = getPoKeys(potFile.items);
        var poKeys = getPoKeys(poFile.items);

        grunt.log.writeln('Checking po file: ' + options.poFile);

        // find items in template.pot that are not in this po file
        var keyDiff = _.difference(potKeys, poKeys);
        if (keyDiff.length > 0 ) {
            grunt.log.error('The following translation keys in template.pot are not present in ' + options.poFile);
            grunt.log.error(keyDiff);
            grunt.fail.fatal('Check FAILED.');
        }

        // find items in this po file which are not in template.pot
        var keyDiff = _.difference(poKeys, potKeys);
        if (keyDiff.length > 0 ) {
            grunt.log.error('The following translation keys in ' + options.poFile + ' are not present in template.pot');
            grunt.log.error(keyDiff);
            grunt.fail.fatal('Check FAILED.');
        }

        grunt.log.ok('Translation .pot template and all .po file keys match.')

    });
};

@@ -1536,32 +1536,33 @@
    %7D);%0A        %7D
+;
 %0A%0A        // get
@@ -1965,28 +1965,39 @@
 keys in 
-template.pot
+' + options.potFile + '
  are not
@@ -2193,36 +2193,32 @@
 ate.pot%0A        
-var 
 keyDiff = _.diff
@@ -2386,59 +2386,1374 @@
  in 
-template.pot');%0A            grunt.log.error(keyDiff
+' + options.potFile);%0A            grunt.log.error(keyDiff);%0A            grunt.fail.fatal('Check FAILED.');%0A        %7D%0A%0A        grunt.log.ok('Translation .pot template and all .po file keys match.');%0A%0A        // Check if keys in .pot file and .po file are in same order. We use _.find to drop out as soon as we find one non-matching%0A        // to make things a bit faster if we can%0A        var matchingOrder = _.find(potKeys, function (item, index) %7B%0A            if (poKeys%5Bindex%5D !== item) %7B%0A                // The keys don't match, so are out of order%0A                return true;%0A            %7D%0A            return false;%0A        %7D);%0A        // _.find returns undefined if all keys are in matching order, so we error if it is not undefined%0A        if (!_.isUndefined(matchingOrder)) %7B%0A            grunt.log.error(%0A                'The keys in ' + options.potFile + ' and ' + options.poFile + ' are not in the same order.%5Cr%5Cn' +%0A                'If you were not expecting the order to change, please check your generating tool version and ' +%0A                'environment to check it matches the environment used by the project.%5Cr%5Cn' +%0A                'If you have deliberately changed the order of the .pot file, you must update the order of the ' +%0A                '.po files as well - e.g. Using a tool such as poEdit%5C%60s %22Update from template%22 option.'%0A            
 );%0A 
@@ -3799,34 +3799,44 @@
 ED.');%0A        %7D
-%0A%0A
+ else %7B%0A    
         grunt.lo
@@ -3889,24 +3889,51 @@
 le keys 
-match.')
+are in the same order.');%0A        %7D
 %0A%0A    %7D)

Add check for keys being in the same order in .pot and .po files