import java.io.*;
import java.util.*;

/** Diff
  * Implemente la gestion des fichiers de config (de log et de config)
  * @version        0.1 Feb 1997 
  * @author         Ebele Nicolas, Mallet Frederic
  */
public class Diff {
/** le noms des fichiers de config */
  String NomsFic[];
/** le nom du fichier de log */
  String LogFic;

/** Constructeur 
  *@param LogFic le nom du fichier de log
  *@NomsFic les noms de fichier de config
   */ 
  public Diff(String LogFic, String[] NomsFic)
  {
    this.LogFic = LogFic;
    this.NomsFic = NomsFic;
  }

/** Execute le test des fichiers
  *@param pas de parametres
  *@return Hashtable contenant le resultats des operations
  */ 
  public Hashtable makeDiff()
  {
    Hashtable Executed = new Hashtable();
    int i;
    String Lect = "";
    Hashtable Res = new Hashtable();

// Ouverture du fichier de log
    FileInputStream fin;
    DataInputStream LogInput;
    try {
      fin =  new FileInputStream(LogFic);
      LogInput = new DataInputStream(fin);

// Lecture du fichier de log
      Date date = new Date();
      for (String lu=LogInput.readLine(); lu!=null; lu=LogInput.readLine()) {
        lu = lu.substring(lu.lastIndexOf('|')+1);
        if (lu.startsWith("AT"))
          date = new Date(lu.substring(3, lu.indexOf("->")));
        else {
          Vector tmp = (Vector)Executed.get(date);
	  if (tmp == null)
            tmp = new Vector();
           tmp.addElement(lu);
           Executed.put(date, tmp);
        }
      }
    } catch (FileNotFoundException e) {
// aucun fichier de Log n'a encore ete cree, rien n'a ete execute
    } catch (IOException e) {
      Res.put("Erreur", "Lecture du fichier de log.");
    }



// lecture des fichiers de config 
    for(i=0;i<NomsFic.length;i++) {
      FileInputStream fou;
      DataInputStream myOutput;
      try {
	fou =  new FileInputStream(NomsFic[i]);
	myOutput = new DataInputStream(fou);
      } catch (FileNotFoundException e) {
        Res.put("Erreur", "lecture fichier de commandes " +NomsFic[i]);
	continue;
      }
      
      try {	
        Date date = new Date();
        Vector action;
        String state="-> Unknown command";
        for (String lu=myOutput.readLine(); lu!=null; lu=myOutput.readLine())
          if (lu.startsWith("AT"))
            date = new Date(lu.substring(3));
          else {
            action = (Vector)Executed.get(date);
            if (action == null)
              state = "-> Not yet executed";
            else
              for (Enumeration en=action.elements(); en.hasMoreElements(); ) {
                String tmp=en.nextElement().toString();
                if (tmp.startsWith(lu))
                  state = tmp.substring(tmp.indexOf("->"));
              }
            action = (Vector)Res.get(date);
            if (action==null)
              action = new Vector();
            action.addElement(lu+state);
            Res.put(date, action);
          }	    
      } catch (IOException e) {
        Res.put("Erreur", "lecture fichier de commandes " + NomsFic[i]);
        return Res;
      }
    }
    return Res;
  }
}
