/* LanguageTool, a natural language style checker 
 * Copyright (C) 2011 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.dev;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

import org.languagetool.JLanguageTool;
import org.languagetool.Language;
import org.languagetool.tools.StringTools;

/**
 * Command line tool to list activity for grammar files. Requires a typical developer setup,
 * i.e. a local "svn" command and the sources checked out from SVN. 
 * 
 * @author Daniel Naber
 */
public final class RuleActivityOverview {

  private static final int PAST_DAYS = 365/2;

  public static void main(final String[] args) throws Exception {
    final RuleActivityOverview prg = new RuleActivityOverview();
    prg.run();
  }
  
  private RuleActivityOverview() {
    // no constructor
  }
  
  private void run() throws IOException, InterruptedException {

    System.out.println("Commits per language in the last " + PAST_DAYS + " days");
    System.out.println("Date: " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    
    final List<String> sortedLanguages = new ArrayList<>();
    for (Language element : Language.REAL_LANGUAGES) {
      sortedLanguages.add(element.getName());
    }
    Collections.sort(sortedLanguages);

    final Calendar today = GregorianCalendar.getInstance();
    final Calendar past = GregorianCalendar.getInstance();
    past.add(Calendar.DAY_OF_MONTH, -PAST_DAYS);
    
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    final String todayString = dateFormat.format(today.getTime());
    final String pastString = dateFormat.format(past.getTime());
    
    final Runtime runtime = Runtime.getRuntime();
    for (final String langName : sortedLanguages) {
      final Language lang = Language.getLanguageForName(langName);
      final File xmlFile = new File(".", JLanguageTool.getDataBroker().getRulesDir() + File.separator + lang.getShortName() + File.separator + "grammar.xml");
      final String command = "svn log -q -r {" + pastString + "}:{" + todayString + "} src/" + xmlFile;
      final Process process = runtime.exec(command);
      final InputStream inputStream = process.getInputStream();
      final String output = StringTools.readStream(inputStream, "utf-8");
      process.waitFor();
      final int commits = getCommits(output);
      System.out.println(commits + "\t" + langName);
    }
  }

  private int getCommits(String svnOutput) {
    int count = 0;
    try (Scanner scanner = new Scanner(svnOutput)) {
      while (scanner.hasNextLine()) {
        final String line = scanner.nextLine();
        if (line.matches("^r\\d+.*")) {
          count++;
        }
      }
    }
    return count;
  }

}

@@ -1195,47 +1195,8 @@
 r;%0A%0A
-import org.languagetool.JLanguageTool;%0A
 impo
@@ -1225,16 +1225,16 @@
 nguage;%0A
+
 import o
@@ -2228,68 +2228,8 @@
 );%0A%0A
-    final Calendar today = GregorianCalendar.getInstance();%0A
     
@@ -2417,75 +2417,8 @@
 %22);%0A
-    final String todayString = dateFormat.format(today.getTime());%0A
     
@@ -2648,24 +2648,204 @@
 (langName);%0A
+      String langCode = lang.getShortName();%0A      List%3CString%3E ruleFileNames = lang.getRuleFileNames();%0A      int commits = 0;%0A      for (String ruleFileName : ruleFileNames) %7B%0A  
       final 
@@ -2873,130 +2873,111 @@
 le(%22
-.%22, JL
+l
 anguage
-Tool.getDataBroker().getRulesDir() + File.separator + lang.getShortName() + File.separator + %22grammar.xml%22
+tool-language-modules/%22 + langCode%0A                + %22/src/main/resources/%22 + ruleFileName
 );%0A
+  
     
@@ -3006,23 +3006,24 @@
  = %22
-svn
+git
  log -
-q -r %7B
+-after=
 %22 + 
@@ -3040,36 +3040,9 @@
  + %22
-%7D:%7B%22 + todayString + %22%7D src/
+ 
 %22 + 
@@ -3050,16 +3050,18 @@
 mlFile;%0A
+  
       fi
@@ -3107,24 +3107,26 @@
 and);%0A      
+  
 final InputS
@@ -3173,24 +3173,26 @@
 am();%0A      
+  
 final String
@@ -3253,16 +3253,18 @@
 ;%0A      
+  
 process.
@@ -3280,25 +3280,17 @@
 ;%0A      
-final int
+ 
  commits
@@ -3290,16 +3290,17 @@
 commits 
++
 = getCom
@@ -3313,16 +3313,24 @@
 utput);%0A
+      %7D%0A
       Sy
@@ -3367,24 +3367,87 @@
 %22 + langName
+ + (lang.isVariant() ? %22 (including the parent language)%22 : %22%22)
 );%0A    %7D%0A  %7D
@@ -3672,25 +3672,27 @@
 ine.
-matches(%22%5Er%5C%5Cd+.*
+startsWith(%22commit 
 %22)) 

make the commit statistics work with git