-- mycomputer is another attempt of A. i. written with Ada.
-- Mycomputer is Copyright (C) 2024 Manuel De Girardi ; 
--
--   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 2 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, write to the Free Software
--   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
--
-- Date := "2024-03-28 09:06:10"
-- Version := "1.0.0r"
with My.Errors;
with Ada.Wide_Text_Io;
with Ada.Text_Io;
use Ada;
with Ada.Characters.Handling;
use Ada.Characters.Handling;
with Ada.Strings.UTF_Encoding.Strings;
use Ada.Strings.UTF_Encoding.Strings;
with Gnat.Os_Lib;
with Gnat.Command_Line;
use Gnat;

package body My.Options is
   
   use Errors;
   
   function Get_Options_From_Command_Line
     -- To get Options_Kit_array_Type from argument in command_line ;
     
     return Options_Kit_Array_Type is
      
      Options_Kit : constant Options_Kit_Array_Type := Full_Line_Options_Kit;
      
   begin
      -- D'abord je traite les arguments de la ligne de commande ;                 --
  Opt :
      loop	 
	 begin
	    loop
	       begin
		  case Command_Line.Getopt ("v F: h") is
		     
		     when ASCII.NUL => exit Opt;
		     when 'F' =>
			if Command_Line.Full_Switch /= "F" then
			   raise Command_Line.Invalid_Switch;
			end if;
			Options_Kit(Config_Filename).Is_Setted := True;
			Options_Kit(Config_Filename).Value :=
			  new Wide_String ' (To_Wide_String(Decode(Command_Line.Parameter)));
								     	
		     when 'h' =>
			if Command_Line.Full_Switch /= "h" then
			   raise Command_Line.Invalid_Switch;
			end if;
			Options_Kit(Help).Is_Setted := True;
		     when 'v' =>
			if Command_Line.Full_Switch /= "v" then
			   raise Command_Line.Invalid_Switch;
			end if;
			Options_Kit(Version).Is_Setted := True;		  
		     when others =>
			raise Program_Error;         -- cannot occur!
		  end case;
		  
		  
	       exception
		  when Command_Line.Invalid_Switch    => Text_Io.Put_Line ("Invalid Switch " & Command_Line.Full_Switch);
		     
		     raise;
		     
		  when Command_Line.Invalid_Parameter =>
		     
		     raise Spec_Error;
		     		  
	       end;
		  
	    end loop;      
	    
	 exception
	    when Spec_Error =>
	       Text_Io.Put_Line ("No parameter for " & Command_Line.Full_Switch);
	       raise;
	       
	 end;
      end loop opt;
      
      return Options_Kit;
      

      
      
   end Get_Options_From_Command_Line;

   
   
   function Get_Options_From_File
     (Filename : in Wide_String)
     -- To get Options_Kit_array_Type from argument in file ;
     
     return Options_Kit_Array_Type is
      Options_Kit : constant Options_Kit_Array_Type := Full_File_Options_Kit;
      File : Wide_Text_Io.File_Type;
   begin     
      if Os_Lib.Is_Regular_File(To_String(Filename)) then
	 Wide_Text_Io.Open(File, Wide_Text_Io.In_File, To_String(Filename));
	 while not Wide_Text_Io.End_Of_File(File) loop

	    declare
	       Line   : constant Wide_String := Wide_Text_Io.Get_Line(File);
	       Switch : constant Character := To_Character(Line(Line'First));
	       Parameter : access String;
	    begin
	       
	       if Line'Length > 1 then
		  Parameter := new String ' (To_String(Line(Line'First+1..Line'last)));
	       end if;
	       
	       case Switch is
		  
		  when 'F' =>
		     
		     Options_Kit(Config_Filename).Is_Setted := True;
		     Options_Kit(Config_Filename).Value :=
		       new Wide_String ' (To_Wide_String(Decode(Parameter.all)));
		     
		  when 'h' =>
		     
		     Options_Kit(Help).Is_Setted := True;		  
		     
		  when 'v' =>
		     
		     Options_Kit(Version).Is_Setted := True;
		  		  
		  when others =>
		     null;
	       end case;
	       
	    end;
	 end loop;

	 
	 
	 Wide_Text_Io.Close(File);
      end if;
     return Options_Kit;
   end Get_Options_From_File;   

end My.Options ;