------------------------------------------------------------------------------
--                                                                          --
--   Websurf is web index of my projects with Ada programming language.     --
--                                                                          --
--              CGI over Gnu/Linux Debian with Gnu Ada Translator           --
--                             (part of web site)                           --
--                            using existant cookie.                        --
------------------------------------------------------------------------------
-- Date   : 2025-12-03                                                      --
-- Author : Manuel De Girardi                                               --
-- Descr  : HTML table created from repositories : /Projects/Projects.lst --
------------------------------------------------------------------------------
-- Projects.lst is projects index providing time work Skywalker compatible.  --
------------------------------------------------------------------------------
with Ada.Text_Io;
use Ada.Text_Io;
use Ada;
with gnat.CGI;
use Gnat;
with gnat.Directory_Operations.Iteration;
use Gnat.Directory_Operations;
with Gnat.Os_Lib;
use Gnat.Os_Lib;
with System.Os_Constants;
use System.Os_Constants;
with Ada.Strings.Fixed;
use Ada.Strings;
with Gnat.CGI.Cookie;
procedure Websurf is
   
   
   Path : constant String := "/var/www/html/Projects/";
   File : File_Type;
   
   
   ------------------------------------------------------------------------
   -- Line format :                                                      --
   --                                                                    --
   -- Name, Version, Archive, Doc, time cost, target, dep, decrip, graph --
   ------------------------------------------------------------------------
   

   
   
   
   procedure Table_Action
     (Item  :        String;
      Index :        Positive;
      Quit  : in out Boolean) is
      
      
      Name, Version, Source,
	Cost, Descr, Graph : String_Access;
      
      Buffer : String_Access := new String ' ("");
      Set : String_Access := new String ' ("");
      Os  : String_Access := new String ' ("");
      Archive  : String_Access := new String ' ("");
      Projects, List, Versions, Env, Txt : File_Type;
      
      
   begin      
      

      if CGI.Cookie.Value("logname")'Length = 0 then
	 Open(File, In_File, Path & "Projects.lst");
      else
	 Open(File, In_File, "/var/www/html/" & CGI.Cookie.Value("logname") & "/Projects.lst");
      end if;

      while not End_Of_File(File) loop
	 declare
	    Project_Name : constant String := Get_Line(File);
	    Img : constant String := Project_Name(Fixed.Index(Project_Name, " ")+1..Project_Name'Last);
	    String_Long_Float : constant String(1..Img'Length) := Img;
	    Elapsed_Long_Float : constant Long_Long_Float := (Long_Long_Float'Value(String_Long_Float));
	    Days         : constant Natural := Natural(Elapsed_Long_Float / 86400.0);
	    
	    Elapsed : Duration := Duration(Elapsed_Long_Float - (Long_Long_Float(Days) * 86400.0));
	 begin
	    if Base_Name(Item) = Project_Name(1..Fixed.Index(Project_Name, " ")-1) then

	       
	       if Is_Directory(Item) then

		  Open(List, In_File, Item & "/Project.lst");
		  Name := new String ' (Get_Line(List));
		  Close(List);

		  Open(Versions, In_File, Item  & "/Versions.lst");
		  Version := new String ' (Get_Line(Versions));
		  Close(Versions);

		  if CGI.Cookie.Value("logname")'Length = 0 then
		     Source := new String ' ("<a href=""" & "/Projects/" & Base_Name(Item) & "/doc/sources/html""/>HTML</a>");
		  else
		     Source := new String ' ("<a href=""" & "/" & CGI.Cookie.Value("logname") & "/" & Base_Name(Item) & "/doc/sources/html""/>HTML</a>");
		  end if;

		  Cost := new String ' (Long_Integer'Image(Long_Integer(Long_Integer(Days) * 86400 + Long_Integer(Elapsed/3600.0))/3600));

		  if Is_Regular_File(Item & ".zip") then
		     Os := new String ' (Os_Type'Image(Os_Type'Val(0)));
		     if CGI.Cookie.Value("logname")'Length = 0 then
			Archive := new String ' ("<a href=""" & "/Projects/" & Base_Name(Item) & ".zip>MS Windows</a>""");
		     else
			Archive := new String ' ("<a href=""" & "/" & CGI.Cookie.Value("logname") & "/" & Base_Name(Item) & ".zip>MS Windows</a>""");
		     end if;
		  end if;


		  if Is_Regular_File(Item & ".tar.gz") then
		     Buffer := new String ' (Os.all & (Os_Type'Image(Os_Type'Val(1))));
		     Free(Os);
		     Os := new String ' (Buffer.all);
		     Free(Buffer);
		     Buffer := new String ' ("");
		     if CGI.Cookie.Value("logname")'Length = 0 then
			Buffer := new String ' (Archive.all & "<a href=""" & "/Projects/" & Base_Name(Item) & ".tar.gz"">GNU/Linux</a>");
		     else
			Buffer := new String ' (Archive.all & "<a href=""" & "/" & CGI.Cookie.Value("logname") & "/" & Base_Name(Item) & ".tar.gz"">GNU/Linux</a>");
		     end if;
		     Archive := new String ' (Buffer.all);
		     Free(Buffer);
		  end if;
		  if Os = null then
		     Os := new String ' ("not available");
		  end if;

		  Open (Env, In_File, Item & "/Environment.txt");
		  Set := new String ' (Get_Line(Env));
		  Close (Env);

		  Open (Txt, In_File, Item & "/Abstract.txt");
		  Descr := new String ' (Get_Line(Txt));
		  Close (Txt);

		  if CGI.Cookie.Value("logname")'Length = 0 then
		     Graph := new String ' ("<a href=""" & "/Projects/" & Base_Name(Item) & "/web/html/images/graph_dependencies.png"">img</a>");
		  else
		     Graph := new String ' ("<a href=""" & "/" & CGI.Cookie.Value("logname") & "/" & Base_Name(Item) & "/web/html/images/graph_dependencies.png"">img</a>");
		  end if;

		  Put_Line("<tr>");

		  Put_Line(" <td>" & Name.all & "</td>");
		  Put_Line(" <td>" & Version.all & "</td>");
		  Put_Line(" <td>" & Archive.all & "</td>");
		  Put_Line(" <td>" & Source.all & "</td>");
		  Put_Line(" <td>" & Cost.all & "</td>");
		  Put_Line(" <td>" & OS.all & "</td>");
		  Put_Line(" <td>" & Set.all & "</td>");
		  Put_Line(" <td>" & Descr.all & "</td>");
		  Put_Line(" <td>" & Graph.all & "</td>");

		  if CGI.Cookie.Value("logname")'Length = 0 then
		     Put_Line(" <td>" & "<a href=""" & "/Projects/" & Base_Name(Item) & "/log.html"">" & "log</a></td>");
		  else
		     Put_Line(" <td>" & "<a href=""" & "/" & CGI.Cookie.Value("logname") & "/" & Base_Name(Item) & "/log.html"">" & "log</a></td>");
		  end if;		  
		  Put_Line("</tr>");

		  Free(Name);
		  Free(Version);
		  Free(Archive);
		  Free(Source);
		  Free(Cost);
		  Free(Os);
		  Free(Set);
		  Free(Descr);
		  Free(Graph);

	       end if;
	       
	    end if;
	 end;
      end loop;
      Close(File);
   end Table_Action;
   
   procedure Iteration is new Iteration.Wildcard_Iterator(Table_Action);
   
   
 begin
    Cgi.Put_Header;
    Text_Io.Put_Line("<html>");
   Text_Io.Put_Line(" <head>");
   Text_Io.Put_Line("  <meta charset=""UTF-8"">");
   Text_Io.Put_Line("  <title>");
   Text_Io.Put_Line("   Websurf");
   Text_Io.Put_Line("  </title>");
   Text_Io.Put_Line(" </head>");
   Text_Io.Put_Line("<body>");
   
   Text_Io.Put_Line(" <h1>Websurf</h1>");
   Text_Io.Put_Line("This programs are déveloped over Gnu/Linux Debian Buster.");
   Text_Io.Put_Line(" <h2>Abstract</h2>");
   Text_Io.Put_Line("  <b>Websurf</b> is an Web index of some program and projects writted with the programming language Ada.");
   Text_Io.Put_Line(" <h2>Tag set</h2>");
   Text_Io.Put_Line(" <h3>Name</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Name</i> is the simple name of the program.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  String");
   Text_Io.Put_Line(" <h3>Version</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Version</i> is the last implemented release.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  GNU format");
   Text_Io.Put_Line(" <h3>Archive</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Archive</i> is the link to downloadable source with the full documentation.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  Filename : String");
   Text_Io.Put_Line(" <h3>Source</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Source</i> is the link to online source files</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  Dir_Name");
   Text_Io.Put_Line(" <h3>Time count</h3>");
   Text_Io.Put_Line("  <p>The tag <i>time count</i> is alternativle the approximative or the exact time cost of the project.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  hour : Integer");
   Text_Io.Put_Line(" <h3>OS</h3>");
   Text_Io.Put_Line("  <p>The tag <i>OS</i> is the dedied operating system of the project.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  Os_Type");
   Text_Io.Put_Line(" <h3>Set</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Set</i> is the environment dependencies set of the project.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  column separated : String");
   Text_Io.Put_Line(" <h3>Descr</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Descr</i> is the name of the domain.</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  String");
   Text_Io.Put_Line(" <h3>Graph</h3>");
   Text_Io.Put_Line("  <p>The tag <i>Graph</i> is the link to the graph depencies picture (png)</p>");
   Text_Io.Put_Line(" <h4>type</h4>");
   Text_Io.Put_Line("  Filename : String");
   Text_Io.Put_Line(" <h1>Project table</h1>");
   Text_Io.Put_Line("  <table>");
   Text_Io.Put_Line("   <tr><td>Name</td><td>Version</td><td>Archive</td><td>Source</td><td>Time count</td><td>OS</td><td>Set</td><td>Descr</td><td>Graph</td><td>Log</td></tr>");
   if CGI.Cookie.Value("logname")'Length = 0 then
      Iteration(Path & "*-*.*.*");
      --Open(File, In_File, Path & "Projects.lst");
   else
      Iteration("/var/www/html/" & CGI.Cookie.value("logname") & "/*-*.*.*");	 
   end if;
   

   Text_Io.Put_Line("</table>");
   put_line("The graph dependencies are realised with Gnostic.");
   Text_Io.Put_Line("</body>");
   Text_Io.Put_Line("</html>");
end Websurf;
