var Promise = require("promised-io/promise").Promise;
var commandline = require('../modules/commandline');
var Command = commandline.Command;
var log = require('../modules/log');

var processes = {};


function ignoreLatexmkRunLog() {
    "use strict";
    var ignoreNext = false;
    return function(output) {
        if(ignoreNext) {
            ignoreNext = false;
            return false;
        } else {
            var isLatexMkLog = output.output === 'Latexmk: Run number 1 of rule \'pdflatex\'';
            ignoreNext = isLatexMkLog;
            return !isLatexMkLog;
        }
    };
}

function compile(repoDir) {
    "use strict";
		log('Compiling PDF', repoDir);
		processes[repoDir] = true;
        var compilePromise = new Promise();

        var removeLastSuccessful = new Command('rm dippa_last_successful.pdf', repoDir);
        var copyLastSuccessful = new Command('mv dippa.pdf dippa_last_successful.pdf', repoDir);
        var removeTemp = new Command('rm tmp.pdf', repoDir);
        var latexmk = new Command('latexmk -silent -pdf -r ../../../latexmkrc -jobname=tmp dippa', repoDir);
        var copy = new Command('cp tmp.pdf dippa.pdf', repoDir);

        commandline.runAll([removeLastSuccessful, copyLastSuccessful, removeTemp, latexmk, copy]).then(function(output) {
            var outputFiltered = output.filter(ignoreLatexmkRunLog());
            outputFiltered.forEach(function(outputItem) {
                log(['[latex compile]', outputItem.output].join(' '));
            });
            log('Compiling done', repoDir);
            processes[repoDir] = false;
            compilePromise.resolve(outputFiltered);
        }, function(e) {
            log.error('Compile failed');
            log.error(e);
        });

        return compilePromise;
}

function isCompiling(repoDir) {
    "use strict";
	return !!processes[repoDir];
}

module.exports = {
	compile: compile,
	isCompiling: isCompiling
};

log('PDF compiler initialized');
@@ -171,16 +171,40 @@
 s/log');
+%0Avar fs = require('fs');
 %0A%0Avar pr
@@ -765,24 +765,177 @@
 Promise();%0A%0A
+        if(fs.exists('dippa.pdf')) %7B%0A            log('Last compilation was unsuccessful');%0A            log('Not updating the last successful file');%0A    
         var 
@@ -1003,32 +1003,36 @@
 pdf', repoDir);%0A
+    
         var copy
@@ -1104,32 +1104,42 @@
 pdf', repoDir);%0A
+        %7D%0A
         var remo

Update last successful only if it can be updated