package com.sass_lang;

import org.jruby.embed.ScriptingContainer;

import javax.servlet.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class SassCompilingFilter implements Filter {
    private static final int DWELL = 5000;
    protected static final String DEFAULT_TEMPLATE_LOCATION = "WEB-INF" + File.separator + "sass";
    protected static final String DEFAULT_CSS_LOCATION = "stylesheets";
    protected static final String DEFAULT_CACHE_LOCATION = "WEB-INF" + File.separator + ".sass-cache";

    private String templateLocation;
    private String cssLocation;
    private String cacheLocation;
    private long lastRun;

    public void init(FilterConfig filterConfig) throws ServletException {
        String root = new File(filterConfig.getServletContext().getRealPath("/")).getAbsolutePath();
        templateLocation = fullPath(root, filterConfig.getInitParameter("templateLocation"), DEFAULT_TEMPLATE_LOCATION);
        cssLocation = fullPath(root, filterConfig.getInitParameter("cssLocation"), DEFAULT_CSS_LOCATION);
        cacheLocation = fullPath(root, filterConfig.getInitParameter("cacheLocation"), DEFAULT_CACHE_LOCATION);
    }

    private String fullPath(String root, String directory, String defaultDirectory) {
        return root + File.separator + (directory == null ? defaultDirectory : directory);
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        long now = Clock.now().getTime();
        
        if (now - lastRun >= DWELL) {
            lastRun = now;
            StringWriter raw = new StringWriter();
            PrintWriter script = new PrintWriter(raw);

            script.println("  require 'rubygems'                                  ");
            script.println("  require 'sass/plugin'                               ");
            script.println("  Sass::Plugin.options.merge!(                        ");
            script.println("    :template_location => '" + templateLocation + "', ");
            script.println("    :css_location => '" + cssLocation + "',           ");
            script.println("    :cache_store => nil,                              ");
            script.println("    :cache_location => '" + cacheLocation + "'        ");
            script.println("  )                                                   ");
            script.println("  Sass::Plugin.check_for_updates                      ");
            script.flush();

            ScriptingContainer ruby = new ScriptingContainer();
            ruby.runScriptlet(raw.toString());
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
    }
}

@@ -586,90 +586,29 @@
 ing 
-templateLocation;%0A    private String cssLocation;%0A    private String cacheLocation
+checkForUpdatesScript
 ;%0A  
@@ -815,16 +815,23 @@
         
+String 
 template
@@ -943,16 +943,23 @@
         
+String 
 cssLocat
@@ -1056,16 +1056,23 @@
         
+String 
 cacheLoc
@@ -1163,24 +1163,134 @@
 _LOCATION);%0A
+        %0A        checkForUpdatesScript = checkForUpdatesScript(templateLocation, cssLocation, cacheLocation);%0A
     %7D%0A%0A    p
@@ -1737,24 +1737,329 @@
  = now;%0A    
+        ScriptingContainer ruby = new ScriptingContainer();%0A            ruby.runScriptlet(checkForUpdatesScript);%0A        %7D%0A%0A        filterChain.doFilter(servletRequest, servletResponse);%0A    %7D%0A%0A    private String checkForUpdatesScript(String templateLocation, String cssLocation, String cacheLocation) %7B%0A
         Stri
@@ -2101,20 +2101,16 @@
         
-  
-  
 PrintWri
@@ -2141,28 +2141,24 @@
 iter(raw);%0A%0A
-    
         scri
@@ -2219,36 +2219,32 @@
             %22);%0A
-    
         script.p
@@ -2309,36 +2309,32 @@
     %22);%0A        
-    
 script.println(%22
@@ -2391,36 +2391,32 @@
     %22);%0A        
- 
-   
 script.println(%22
@@ -2473,36 +2473,32 @@
 %22', %22);%0A        
-  
-  
 script.println(%22
@@ -2555,36 +2555,32 @@
     %22);%0A        
-   
- 
 script.println(%22
@@ -2629,36 +2629,32 @@
             %22);%0A
-    
         script.p
@@ -2719,36 +2719,32 @@
     %22);%0A        
-    
 script.println(%22
@@ -2805,28 +2805,24 @@
 %22);%0A        
- 
-   
 script.print
@@ -2887,28 +2887,24 @@
 %22);%0A        
- 
-   
 script.flush
@@ -2920,182 +2920,28 @@
     
-    ScriptingContainer ruby = new ScriptingContainer();%0A            ruby.runScriptlet(raw.toString());%0A        %7D%0A%0A        filterChain.doFilter(servletRequest, servletResponse
+return raw.toString(
 );%0A 

build the script at init time