///////////////////////////////////////////////////////////////////////////////
//                              Lisaac Example                               //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program 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 General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name    := MAILER;
     
  - author  := "Benoit Sonntag";
  
  - comment := "Extract mail adress for Matthieu.";
  
Section Inherit
  
  - parent_object:OBJECT := OBJECT;
      
Section Public
  
  - buffer:STRING := STRING.create 2048;
  
  - mail:HASHED_SET[STRING] := HASHED_SET[STRING].create;
  
  - extract_mail <-
  ( + idx,begin,end:INTEGER;
    + n:STRING;
    
    idx := buffer.lower;
    {idx <= buffer.upper}.while_do {
      idx := buffer.index_of '@' since idx;
      (idx <= buffer.upper).if {
	begin := idx - 1;
	{
	  (begin >= buffer.lower) && {
	    (buffer.item begin.is_letter_or_digit) || 
	    {buffer.item begin = '_'} ||
	    {buffer.item begin = '.'} 
	  }
	}.while_do {
	  begin := begin - 1;
	};
	begin := begin + 1;
	//
	end := idx + 1;
	{
	  (end <= buffer.upper) && {
	    (buffer.item end.is_letter_or_digit) || 
	    {buffer.item end = '_'} ||
	    {buffer.item end = '.'} 
	  }
	}.while_do {
	  end := end + 1;
	};
	end := end - 1;
	//
	((end - begin) > 3).if { 
	  n := buffer.substring begin to end;
	  mail.add n;
	};
	idx := end + 1;
      };
    };    
  );
   
  - main <-
  ( + e:ENTRY;
    + file:STD_FILE;
    + dir:DIRECTORY;
    
    //
    // Parse command line option.
    //
    (COMMAND_LINE.count != 2).if {      
      "Extract mail for Matthieu (by Sonntag Benoit)\n\
      \Usage  : mailer <directory>\n\
      \Output : mail.txt\n".print;
      die_with_code exit_failure_code;
    };
    
    //
    // Open directory.
    //
    dir := FILE_SYSTEM;    
    e := dir.get (COMMAND_LINE.item 1);
    ((e != NULL) && {e.is_directory}).if {      
      e := e.open;
      (e != NULL).if {
	dir.close;
	dir ?= e;
      } else {
	'`'.print;
	COMMAND_LINE.item 2.print;
	"\': Permission denied.\n".print;
	die_with_code exit_failure_code;
      };
    } else {
      '`'.print;
      COMMAND_LINE.item 2.print;
      "\': No such directory.\n".print;
      die_with_code exit_failure_code;
    };
    
    //
    // Select all text file 
    //
    (dir.lower).to (dir.upper) do { i:INTEGER;
      e := dir.item i;
      ((e.is_file) && {e.name.has_suffix ".txt"}).if {
	"Parse : ".print;
	e.path.print;	
	e := e.open;
	(e = NULL).if {
	  " : Not open.".print;          
	} else {
	  file ?= e;          
	  buffer.clear;
	  buffer.set_capacity (file.size.to_integer);
	  file.read buffer size (file.size);
	  extract_mail;
	  file.close;
	  " \t OK".print;
	};
	'\n'.print;
      };
    };
    
    "Result : ".print;
    mail.count.print;
    " found.\n".print;
    
    //
    // Save file.
    //    
    (mail.count != 0).if {
      e := dir.make_file "mail.txt";
      (e = NULL).if {      
	"`mail.txt' : File exists, cannot create file.\n".print;
	die_with_code exit_failure_code;
      };
      file ?= e.open;
      buffer.clear;
      (mail.lower).to (mail.upper - 1) do { j:INTEGER;
	buffer.append (mail.item j);
	buffer.add_last ';';
	buffer.add_last ' ';            
      };
      buffer.append (mail.last);
      file.write buffer from 1 size (buffer.count);
      file.close;          
      "Write `mail.txt' ok !\n".print;
    };
  );
