package org.cobbzilla.util.main;

import org.cobbzilla.util.io.FileUtil;
import org.cobbzilla.util.io.FilesystemWalker;

import java.util.Map;
import java.util.regex.Matcher;

import static org.cobbzilla.util.io.FileUtil.*;

public class FileHeaderMain extends BaseMain<FileHeaderOptions> {

    public static void main (String[] args) { main(FileHeaderMain.class, args); }

    @Override protected void run() throws Exception {
        final FileHeaderOptions opts = getOptions();
        final Map<String, FileHeader> headers = opts.getHeaders();
        new FilesystemWalker()
                .withDir(opts.getDir())
                .withVisitor(file -> {
                    final String ext = FileUtil.extension(file);
                    if (ext.startsWith(".")) {
                        final FileHeader header = headers.get(ext.substring(1));
                        if (header != null) {
                            String contents = toStringOrDie(file);
                            if (contents == null) contents = "";
                            final String prefix;
                            if (header.hasPrefix()) {
                                final Matcher prefixMatcher = header.getPrefixPattern().matcher(contents);
                                if (!prefixMatcher.find()) {
                                    err("prefix not found ("+header.getPrefix().replace("\n", "\\n")+") in file: "+abs(file));
                                    return;
                                }
                                prefix = contents.substring(0, prefixMatcher.start())
                                        + contents.substring(prefixMatcher.start(), prefixMatcher.end());
                                contents = contents.substring(prefixMatcher.end());
                            } else {
                                prefix = "";
                            }
                            final Matcher matcher = header.getPattern().matcher(contents);
                            if (matcher.find()) {
                                contents = prefix + contents.substring(0, matcher.start())
                                        + header.getHeader() + "\n" + contents.substring(matcher.end());
                            } else {
                                contents = prefix + header.getHeader() + "\n" + contents;
                            }
                            out(abs(file));
                            toFileOrDie(file, contents);
                        }
                    }
                }).walk();
    }

}

@@ -721,59 +721,8 @@
 e);%0A
-                    if (ext.startsWith(%22.%22)) %7B%0A    
     
@@ -775,16 +775,35 @@
 ers.get(
+ext.length() %3E 0 ? 
 ext.subs
@@ -810,16 +810,22 @@
 tring(1)
+ : ext
 );%0A     
@@ -831,36 +831,32 @@
                 
-    
 if (header != nu
@@ -857,28 +857,24 @@
  != null) %7B%0A
-    
             
@@ -944,28 +944,24 @@
             
-    
 if (contents
@@ -1001,36 +1001,32 @@
                 
- 
-   
 final String pre
@@ -1030,20 +1030,16 @@
 prefix;%0A
-    
         
@@ -1076,28 +1076,24 @@
 Prefix()) %7B%0A
-    
             
@@ -1207,28 +1207,24 @@
             
- 
-   
 if (!prefixM
@@ -1232,36 +1232,32 @@
 tcher.find()) %7B%0A
-    
                 
@@ -1395,20 +1395,16 @@
         
-    
 return;%0A
@@ -1427,34 +1427,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -1551,20 +1551,16 @@
         
-    
 + conten
@@ -1637,36 +1637,32 @@
                 
-   
- 
 contents = conte
@@ -1713,36 +1713,32 @@
                 
-    
 %7D else %7B%0A       
@@ -1722,36 +1722,32 @@
        %7D else %7B%0A
-    
                 
@@ -1767,28 +1767,24 @@
 refix = %22%22;%0A
-    
             
@@ -1813,36 +1813,32 @@
                 
-    
 final Matcher ma
@@ -1904,28 +1904,24 @@
             
-    
 if (matcher.
@@ -1922,36 +1922,32 @@
 tcher.find()) %7B%0A
-    
                 
@@ -2053,20 +2053,16 @@
         
-    
 + header
@@ -2134,36 +2134,32 @@
                 
-   
- 
 %7D else %7B%0A       
@@ -2143,36 +2143,32 @@
        %7D else %7B%0A
-    
                 
@@ -2257,34 +2257,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -2327,20 +2327,16 @@
         
-    
 toFileOr
@@ -2360,34 +2360,8 @@
 s);%0A
-                        %7D%0A
     

allow processing of files with no extension