package com.reason.bs;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.KillableColoredProcessHandler;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessListener;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.vfs.VirtualFile;
import com.reason.ide.RmlNotification;
import org.jetbrains.annotations.Nullable;

public class BsCompiler {

    private KillableColoredProcessHandler m_bsb;
    private GeneralCommandLine m_commandLine;
    private ProcessListener m_outputListener;

    BsCompiler(VirtualFile baseDir, String bsbPath) {
        m_commandLine = new GeneralCommandLine(bsbPath, "-color", "-make-world").
                withWorkDirectory(baseDir.getCanonicalPath());
        recreate();
    }

    @Nullable
    public ProcessHandler getHandler() {
        return m_bsb;
    }

    // Wait for the tool window to be ready before starting the process
    public void startNotify() {
        if (m_bsb != null && !m_bsb.isStartNotified()) {
            try {
                m_bsb.startNotify();
            } catch (Throwable e) {
                // already done ?
            }
        }
    }

    @Nullable
    public ProcessHandler recreate() {
        try {
            killIt();
            m_bsb = new KillableColoredProcessHandler(m_commandLine);
            if (m_outputListener != null) {
                m_bsb.addProcessListener(m_outputListener);
            }
            return m_bsb;
        } catch (ExecutionException e) {
            Notifications.Bus.notify(new RmlNotification("Bsb", "Can't run bsb\n" + e.getMessage(), NotificationType.ERROR));
        }

        return null;
    }

    void killIt() {
        if (m_bsb != null) {
            m_bsb.killProcess();
            m_bsb = null;
        }
    }

    public void addListener(ProcessListener outputListener) {
        m_outputListener = outputListener;
        if (m_bsb != null) {
            m_bsb.addProcessListener(outputListener);
        }
    }
}

@@ -646,17 +646,75 @@
 dLine m_
-c
+fullCommandLine;%0A    private GeneralCommandLine m_standardC
 ommandLi
@@ -762,16 +762,55 @@
 istener;
+%0A    private boolean m_firstRun = true;
 %0A%0A    Bs
@@ -853,24 +853,83 @@
  bsbPath) %7B%0A
+        String canonicalPath = baseDir.getCanonicalPath();%0A
         m_co
@@ -926,17 +926,21 @@
       m_
-c
+fullC
 ommandLi
@@ -979,20 +979,26 @@
 ath, %22-c
-olor
+lean-world
 %22, %22-mak
@@ -1007,33 +1007,114 @@
 world%22).
-%0A                
+withWorkDirectory(canonicalPath);%0A        m_standardCommandLine = new GeneralCommandLine(bsbPath).
 withWork
@@ -1123,28 +1123,17 @@
 rectory(
-baseDir.getC
+c
 anonical
@@ -1136,18 +1136,16 @@
 icalPath
-()
 );%0A     
@@ -1712,17 +1712,58 @@
 ndler(m_
-c
+firstRun ? m_fullCommandLine : m_standardC
 ommandLi
@@ -1863,32 +1863,32 @@
 utputListener);%0A
-
             %7D%0A  
@@ -1877,32 +1877,64 @@
 ;%0A            %7D%0A
+            m_firstRun = false;%0A
             retu

use full and standard cli