#!/usr/bin/env node

const color   = require('cli-color');
const Sitemap = require('sitemap');
const program = require('commander');
const fs      = require('fs');
const Path    = require('path');
const common  = require('./common');
const urls    = require('./config/sitemap_urls');

program
    .version('0.2.0')
    .description('Generate sitemap.xml')
    .parse(process.argv);

const url_prefix = 'https://www.binary.com/';
const filename   = 'sitemap.xml';
let excluded     = 0;

const createSitemap = () => {
    const sitemap = Sitemap.createSitemap({
        hostname : url_prefix,
        cacheTime: 600000,
    });

    common.languages
        .map(lang => lang.toLowerCase())
        .forEach((lang) => {
            urls.forEach((entry) => {
                if (!common.isExcluded(entry[3], lang)) {
                    sitemap.add({
                        url       : `${url_prefix}${lang}/${entry[0]}.html`,
                        changefreq: entry[1],
                        priority  : entry[2],
                    });
                } else {
                    excluded++;
                }
            });
        });

    fs.writeFileSync(Path.join(common.root_path, filename), sitemap.toString());
};

const start = Date.now();
process.stdout.write(common.messageStart(`Generating ${filename}`));
createSitemap();
process.stdout.write(common.messageEnd(Date.now() - start));

// Report details
const langs_count = common.languages.length;
const total_count = langs_count * urls.length - excluded;
console.log(`  ${color.green(total_count)} URL nodes total (${color.cyan(langs_count)} Languages ${color.yellowBright('*')} ${color.cyan(urls.length)} URLs ${color.yellowBright('-')} ${color.cyan(excluded)} Excluded)`); // eslint-disable-line no-console

@@ -382,16 +382,41 @@
 %0A%0Aconst 
+config = %5B%0A    %7B%0A        
 url_pref
@@ -418,17 +418,17 @@
 _prefix 
-=
+:
  'https:
@@ -449,28 +449,159 @@
 om/'
-;%0Aconst filename   =
+,%0A        filename   : 'sitemap.xml',%0A        lang_filter: '%5E(?!id$)',%0A    %7D,%0A    %7B%0A        url_prefix : 'https://www.binary.me/',%0A        filename   :
  'si
@@ -610,34 +610,191 @@
 map.
+id.
 xml'
-;%0Alet excluded     = 0
+,%0A        lang_filter: '%5Eid$',%0A    %7D,%0A%5D;%0Alet excluded;%0A%0Aconst getApplicableLanguages = (lang_filter) =%3E common.languages.filter(lang =%3E new RegExp(lang_filter, 'i').test(lang))
 ;%0A%0Ac
@@ -815,23 +815,46 @@
 emap = (
+conf
 ) =%3E %7B%0A
+    excluded = 0;%0A%0A
     cons
@@ -908,16 +908,21 @@
 tname : 
+conf.
 url_pref
@@ -965,32 +965,56 @@
 );%0A%0A    
-common.languages
+getApplicableLanguages(conf.lang_filter)
 %0A       
@@ -1245,16 +1245,21 @@
    : %60$%7B
+conf.
 url_pref
@@ -1557,16 +1557,21 @@
 t_path, 
+conf.
 filename
@@ -1598,16 +1598,47 @@
 ));%0A%7D;%0A%0A
+config.forEach((conf) =%3E %7B%0A    
 const st
@@ -1651,24 +1651,28 @@
 Date.now();%0A
+    
 process.stdo
@@ -1714,16 +1714,21 @@
 ating $%7B
+conf.
 filename
@@ -1733,16 +1733,21 @@
 me%7D%60));%0A
+%0A    
 createSi
@@ -1752,19 +1752,28 @@
 Sitemap(
-);%0A
+conf);%0A%0A    
 process.
@@ -1826,16 +1826,20 @@
 art));%0A%0A
+    
 // Repor
@@ -1848,16 +1848,20 @@
 details%0A
+    
 const la
@@ -1872,32 +1872,56 @@
 count = 
-common.languages
+getApplicableLanguages(conf.lang_filter)
 .length;
@@ -1921,16 +1921,20 @@
 length;%0A
+    
 const to
@@ -1983,16 +1983,20 @@
 cluded;%0A
+    
 console.
@@ -2203,16 +2203,18 @@
 xcluded)
+%5Cn
 %60); // e
@@ -2243,8 +2243,12 @@
 console%0A
+%7D);%0A

Split sitemap based on the given configuration