/**
 *
 * modify file
 *
 *   --harmony_destructuring --harmony_array_includes
 *
 * Created by kimown on 16-7-24.
 */
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');

const themeDirName = process.argv[2] ? process.argv[2] : 'hexo-theme-landscape-tmp';

const themeDirPath = path.join(__dirname, themeDirName);

const cssDirPath = path.join(themeDirPath, 'source', 'css');

const _partialPath=path.join(themeDirPath,'layout','_partial');


process.on('message', function (m) {
    console.log("fs:" + m);
});

/**
 * modify   _variables.styl
 */

let _variabesPath = path.join(cssDirPath, '_variables.styl');
let _variablesOption = {
    fileName: '_variables.styl',
    soruceCode: fs.readFileSync(_variabesPath).toString(),
    lineNumber: 9,
    expectSourceCode: 'color-background = #eee',
    myExpectSourceCode: 'color-background = #171f26'
}

let _variablesSourceCodeReplaceAfter = replaceWithSpecifyLine(_variablesOption);
fs.writeFileSync(_variabesPath, _variablesSourceCodeReplaceAfter);


/**
 *
 * modify style.styl
 *
 */
let stylePath = path.join(cssDirPath, 'style.styl');
let styleOption = {
    fileName: 'style.styl',
    soruceCode: fs.readFileSync(stylePath).toString(),
    lineNumber: 64,
    expectSourceCode: '  background: color-background',
    myExpectSourceCode: ''
}

let styleSourceCodeReplaceAfter = replaceWithSpecifyLine(styleOption);
fs.writeFileSync(stylePath, styleSourceCodeReplaceAfter);



/**
 *
 * modify header.ejs
 *
 */
let headerEjsPath = path.join(_partialPath, 'header.ejs');
let headerEjsOption = {
    fileName: 'header.ejs',
    soruceCode: fs.readFileSync(headerEjsPath).toString(),
    lineNumber: 1,
    expectSourceCode: '  <div id="banner"></div>',
    myExpectSourceCode: ''
}

let headerEjsSourceCodeReplaceAfter = replaceWithSpecifyLine(headerEjsOption);
fs.writeFileSync(headerEjsPath, headerEjsSourceCodeReplaceAfter);






console.log('change theme file successfully,exiting sub process!!');
process.send({'ok': true});
process.exit(0);


/**
 * only replace specify line
 * now,node 4.* did not support destructure
 */
function replaceWithSpecifyLine(option) {
    let {fileName,soruceCode,lineNumber,expectSourceCode,myExpectSourceCode }=option;
    let sourceCodeAr = soruceCode.split('\n');
    let soruceCodeSpecifyLine = sourceCodeAr[lineNumber];
    try {
        assert.ok([expectSourceCode, myExpectSourceCode].includes(soruceCodeSpecifyLine),
            `${fileName} encounter a problem!!WARNING: the old way changing code is outdated!!`
        );
    } catch (e) {
        process.send({'ok': false});
    }
    sourceCodeAr[lineNumber] = myExpectSourceCode;
    return sourceCodeAr.join('\n');
}

@@ -897,17 +897,17 @@
 71f26'%0A%7D
-%0A
+;
 %0Alet _va
@@ -1054,104 +1054,76 @@
 %0A%0A%0A/
-**%0A *%0A * modify sty
+/ modify  _variab
 le
+s
 .styl
-%0A *%0A */%0Alet stylePath = path.join(cssDirPath, 'style.styl');%0Alet style
+  clear banner picture%0Alet _variablesBanner
 Opti
@@ -1144,21 +1144,26 @@
 eName: '
-sty
+_variab
 le
+s
 .styl',%0A
@@ -1190,29 +1190,33 @@
 eadFileSync(
-style
+_variabes
 Path).toStri
@@ -1241,10 +1241,10 @@
 er: 
-64
+38
 ,%0A  
@@ -1268,38 +1268,40 @@
 e: '
-  background: color-background
+banner-url = %22images/banner.jpg%22
 ',%0A 
@@ -1331,19 +1331,31 @@
 ''%0A%7D
+;
 %0A%0Alet 
-style
+_variablesBanner
 Sour
@@ -1398,21 +1398,32 @@
 ifyLine(
-style
+_variablesBanner
 Option);
@@ -1444,24 +1444,39 @@
 ync(
-stylePath, style
+_variabesPath, _variablesBanner
 Sour
@@ -1497,18 +1497,16 @@
 fter);%0A%0A
-%0A%0A
 /**%0A *%0A 
@@ -1514,26 +1514,26 @@
  modify 
-header.ejs
+style.styl
 %0A *%0A */%0A
@@ -1536,25 +1536,21 @@
  */%0Alet 
-headerEjs
+style
 Path = p
@@ -1562,50 +1562,44 @@
 oin(
-_partialPath, 'header.ejs');%0Alet headerEjs
+cssDirPath, 'style.styl');%0Alet style
 Opti
@@ -1624,18 +1624,18 @@
 e: '
-header.ejs
+style.styl
 ',%0A 
@@ -1661,33 +1661,29 @@
 eadFileSync(
-headerEjs
+style
 Path).toStri
@@ -1704,17 +1704,18 @@
 Number: 
-1
+64
 ,%0A    ex
@@ -1737,31 +1737,36 @@
  '  
-%3Cdiv id=%22banner%22%3E%3C/div%3E
+background: color-background
 ',%0A 
@@ -1798,25 +1798,21 @@
 %0A%7D%0A%0Alet 
-headerEjs
+style
 SourceCo
@@ -1851,25 +1851,21 @@
 ifyLine(
-headerEjs
+style
 Option);
@@ -1886,32 +1886,24 @@
 ync(
-headerEjsPath, headerEjs
+stylePath, style
 Sour
@@ -1927,18 +1927,16 @@
 r);%0A%0A%0A%0A%0A
-%0A%0A
 console.

modify css