package org.batfish.job;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.batfish.main.ConfigurationFormat;

public final class Format {

   public static final String BATFISH_FLATTENED_JUNIPER_HEADER = "####BATFISH FLATTENED JUNIPER CONFIG####\n";

   public static final String BATFISH_FLATTENED_VYOS_HEADER = "####BATFISH FLATTENED VYOS CONFIG####\n";

   public static ConfigurationFormat identifyConfigurationFormat(String fileText) {
      if (fileText.contains("IOS XR")) {
         return ConfigurationFormat.CISCO_IOS_XR;
      }
      char firstChar = fileText.trim().charAt(0);
      Matcher setMatcher = Pattern.compile("\nset ").matcher(fileText);
      Matcher flattenedJuniperMatcher = Pattern.compile(
            Pattern.quote(BATFISH_FLATTENED_JUNIPER_HEADER)).matcher(fileText);
      if (fileText.contains("set system config-management commit-revisions")) {
         return ConfigurationFormat.FLAT_VYOS;
      }
      else if (firstChar == '#'
            || (fileText.contains("system") && fileText.contains("{")
                  && fileText.contains("}")
                  && fileText.contains("config-management") && fileText
                     .contains("commit-revisions"))) {
         return ConfigurationFormat.VYOS;
      }
      else if (firstChar == '!') {
         Matcher aristaMatcher = Pattern.compile("boot system flash.*\\.swi")
               .matcher(fileText);
         if (fileText.contains("set prompt")) {
            return ConfigurationFormat.VXWORKS;
         }
         else if (aristaMatcher.find()) {
            return ConfigurationFormat.ARISTA;
         }
         else {
            String[] lines = fileText.split("\\n");
            for (String line : lines) {
               String trimmedLine = line.trim();
               if (trimmedLine.length() == 0 || trimmedLine.startsWith("!")) {
                  continue;
               }
               if (line.startsWith("version")) {
                  return ConfigurationFormat.CISCO;
               }
               else {
                  return ConfigurationFormat.CISCO; // may change in future
               }
            }
         }
      }
      else if (fileText.contains("set hostname")) {
         return ConfigurationFormat.JUNIPER_SWITCH;
      }
      else if (flattenedJuniperMatcher.find(0)
            || fileText.contains("set system host-name")
            || (fileText.contains("apply-groups") && setMatcher.find(0))) {
         return ConfigurationFormat.FLAT_JUNIPER;
      }
      else if (firstChar == '#'
            || (fileText.contains("version") && fileText.contains("system")
                  && fileText.contains("{") && fileText.contains("}")
                  && fileText.contains("host-name") && fileText
                     .contains("interfaces"))) {
         return ConfigurationFormat.JUNIPER;
      }
      return ConfigurationFormat.UNKNOWN;
   }

   private Format() {
   }

}

@@ -654,18 +654,21 @@
 ompile(%22
-%5Cn
+(?m)%5E
 set %22).m
@@ -818,24 +818,258 @@
 (fileText);%0A
+      Matcher aristaMatcher = Pattern.compile(%22(?m)%5Eboot system flash.*%5C%5C.swi%22)%0A            .matcher(fileText);%0A      Matcher ciscoLike = Pattern.compile(%0A            %22(?m)(%5Eboot system flash.*$)%7C(%5Einterface .*$)%22).matcher(fileText);%0A
       if (fi
@@ -1518,266 +1518,8 @@
   %7D%0A
-      else if (firstChar == '!') %7B%0A         Matcher aristaMatcher = Pattern.compile(%22boot system flash.*%5C%5C.swi%22)%0A               .matcher(fileText);%0A         if (fileText.contains(%22set prompt%22)) %7B%0A            return ConfigurationFormat.VXWORKS;%0A         %7D%0A   
     
@@ -1549,27 +1549,24 @@
 r.find()) %7B%0A
-   
          ret
@@ -1607,551 +1607,124 @@
     
-   %7D%0A  
+%7D%0A
       
- 
 else 
-%7B%0A            String%5B%5D lines = fileText.split(%22%5C%5Cn%22);%0A            for (String line : lines) %7B%0A               String trimmedLine = line.trim();%0A               if (trimmedLine.length() == 0 %7C%7C trimmedLine.startsWith(%22!%22)) %7B%0A                  continue;%0A               %7D%0A               if (line.startsWith(%22version%22)) %7B%0A                  return ConfigurationFormat.CISCO;%0A               %7D%0A               else %7B%0A                  return ConfigurationFormat.CISCO; // may change in future%0A               %7D%0A            %7D%0A         %7D
+if (firstChar == '!' && fileText.contains(%22set prompt%22)) %7B%0A         return ConfigurationFormat.VXWORKS;
 %0A   
@@ -2361,32 +2361,32 @@
 nterfaces%22))) %7B%0A
-
          return 
@@ -2418,24 +2418,201 @@
 ER;%0A      %7D%0A
+      else if (aristaMatcher.find()) %7B%0A         return ConfigurationFormat.ARISTA;%0A      %7D%0A      else if (ciscoLike.find()) %7B%0A         return ConfigurationFormat.CISCO;%0A      %7D%0A
       return

update format detection for cisco