-- main is main file of main project written with Ada.
-- Main is Copyright (C) 2026 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 := "2026-09-17 16:58:22"
-- Version := "0.37.24a"
with Gnat.Sockets;
use Gnat.Sockets;
with gnat.Directory_Operations.Iteration;
use Gnat.Directory_Operations;
with Gnat.CGI;
with Gnat.CGI.Cookie;
use Gnat;

with Ada.Text_Io;
use Ada.Text_Io;

procedure Webspir is
   
   Add  : Gnat.sockets.Sock_Addr_Type;
   Socket   : Socket_Type;
   Channel  : Gnat.sockets.Stream_Access;
   procedure Table_Action
     (Item  :        String;
      Index :        Positive;
      Quit  : in out Boolean) is
   begin
      
      Put_Line("  <option value=""" & Base_Name(Item, ".tar.gz") & """>" & Base_Name(Item, ".tar.gz") & "</option>");
	       end Table_Action;
   procedure Iteration is new Iteration.Wildcard_Iterator(Table_Action);
begin
   if not CGI.Ok then
      raise Program_Error;
   end if;
   if Cgi.Argument_Count = 0 then 
      if Gnat.CGI.Cookie.Count = 1 and then
	Gnat.CGI.Cookie.Value("logname")'Length /= 0 then
	 Cgi.Put_Header;
	 
	 
	 Put_Line("<!DOCTYPE html>");
	 Put_Line("<html>");
	 Put_Line(" <head>");
	 Put_Line("   <meta charset=""UTF-8"">");
	 Put_Line("   <title>Audits</title>");
	 Put_Line(" </head>");
	 Put_Line(" <body>");
	 Put_Line(" <table align=""center"" width=""66%"">");
	 Put_Line(" <tr><td>");
	 Put_Line(" <h1>Quality report</h1>");
	 Put_Line(" <form action=""/cgi-bin/webspir"" Method=""post"">");
	 Put_Line(" <h3>Name</h3>");
	 Put_Line(" <select name=""element"" id=""element"">");
	 Iteration("/var/www/html/Projects/" & "*-*.*.*.tar.gz");
	 Put_Line(" </select>");
	 Put_Line(" <h3>Installation</h3>");
	 Put_Line(" <input type=""radio"" id=""installation"" name=""installation"" value=""beginner"" />beginner");
	 Put_Line(" <input type=""radio"" id=""installation"" name=""installation"" value=""advanced"" />advanced");
	 Put_Line(" <input type=""radio"" id=""installation"" name=""installation"" value=""expert"" />expert");
	 Put_Line(" <h3>Utilisation</h3>");
	 Put_Line(" <input type=""radio"" id=""utilisation"" name=""utilisation"" value=""easy"" />easy");
	 Put_Line(" <input type=""radio"" id=""utilisation"" name=""utilisation"" value=""difficult"" />difficult");
	 
	 Put_Line(" <h3>Ergonomy</h3>");
	 Put_Line(" <input type=""radio"" id=""ergonomy"" name=""ergonomy"" value=""simple"" />simple");
	 Put_Line(" <input type=""radio"" id=""ergonomy"" name=""ergonomy"" value=""detailed"" />detailed");
	 Put_Line(" <input type=""radio"" id=""ergonomy"" name=""ergonomy"" value=""complex"" />complex");
	 Put_Line(" <h3>Rank</h3>");
	 Put_Line(" <select name=""point"" id=""point"">");
	 for I in 1..10 loop
	    Put_Line("  <option value=""" & Integer'Image(I)(2..Integer'Image(I)'Length) & """>" & Integer'Image(I)(2..Integer'Image(I)'Length) & "</option>");
	 end loop;
	 Put_Line(" </select> / 10 points");
	 Put_Line(" <br>");
	 Put_Line(" <br>");
	 Put_Line("  <input type=""reset"" value=""reset"">");
	 Put_Line("  <input type=""submit"" value=""send"">");
	 Put_Line(" </form>");
	 Put_Line(" <a href=""/" & Gnat.CGI.Cookie.Value("logname") & "/gate.html"">return</a>");
	 Put_Line(" </td></tr>");
	 Put_Line(" </table>");
	 
	 Put_Line(" </body>");
	 Put_Line(" </html>");
      else
	 Put_Line("<!DOCTYPE html>");
	 Put_Line("<html>");
	 Put_Line(" <head>");
	 Put_Line("   <meta charset=""UTF-8"">");
	 Put_Line("   <title>Audits</title>");
	 Put_Line(" </head>");
	 Put_Line(" <body>");
	 Put_Line("  You must be logged.");
	 Put_Line(" </body>");
	 Put_Line(" </html>");
      end if;
   elsif Cgi.Argument_Count = 5 then
     if Gnat.CGI.Cookie.Count = 1 and then
	Gnat.CGI.Cookie.Value("logname")'Length /= 0 then
	 Add.Addr :=
	   Gnat.Sockets.Addresses (Gnat.Sockets.Get_Host_By_Name ("localhost"), 1);
	 
	 Add.Port := 1989;
	 Gnat.sockets.Create_Socket (Socket);
	 delay 0.1;
	 Gnat.Sockets.Set_Socket_Option
	   (Socket,
	    Gnat.Sockets.Socket_Level,
	    (Gnat.Sockets.Reuse_Address, True));      
	 delay 0.1;      
	 Gnat.sockets.Connect_Socket (Socket, Add);
	 delay 0.1;
	 Channel := Gnat.sockets.Stream (Socket);
	 String'Output (Channel, "Audits");
	 String'Output (Channel, Gnat.CGI.Cookie.Value("logname"));
	 String'Output (Channel, Gnat.CGI.Value("element"));
	 String'Output (Channel, Gnat.CGI.Value("installation"));
	 String'Output (Channel, Gnat.CGI.Value("utilisation"));
	 String'Output (Channel, Gnat.CGI.Value("ergonomy"));
	 String'Output (Channel, Gnat.CGI.Value("point"));
	 Gnat.Sockets.Close_Socket(Socket);
	 
	 
	 Cgi.Put_Header;
	 Put_Line("<html>");
	 Put_Line(" <head>");
	 Put_Line("   <meta charset=""UTF-8"">");
	 Put_Line("   <title>Audits</title>");
	 Put_Line(" </head>");
	 Put_Line(" <body>");
	 Put_Line(" <table align=""center"" width=""66%"">");
	 Put_Line(" <tr><td>");
	 Put_Line(" <h1>Quality report</h1>");
	 Put_Line("  Thank you!");
	 Put_Line("  <br><br>");
	 Put_Line(" <a href=""/cgi-bin/webspir"">return</a>");
	 Put_Line(" </td></tr>");
	 Put_Line(" </table>");
	 Put_Line(" </body>");
	 Put_Line(" </html>");
      else
	 Put_Line("<!DOCTYPE html>");
	 Put_Line("<html>");
	 Put_Line(" <head>");
	 Put_Line("   <meta charset=""UTF-8"">");
	 Put_Line("   <title>Audits</title>");
	 Put_Line(" </head>");
	 Put_Line(" <body>");
	 Put_Line("  You must be logged.");
	 Put_Line(" </body>");
	 Put_Line(" </html>");
     end if;
   else
      Cgi.Put_Header;
	 Put_Line("<html>");
	 Put_Line(" <head>");
	 Put_Line("   <meta charset=""UTF-8"">");
	 Put_Line("   <title>Audits</title>");
	 Put_Line(" </head>");
	 Put_Line(" <body>");
	 Put_Line(" <table align=""center"" width=""66%"">");
	 Put_Line(" <tr><td>");
	 Put_Line(" <h1>Quality report</h1>");
	 Put_Line("  Une erreur s'est produite lors du traitement.");
	 Put_Line("  <br><br>");
	 Put_Line(" <a href=""/cgi-bin/webspir"">return</a>");
	 Put_Line(" </td></tr>");
	 Put_Line(" </table>");
	 Put_Line(" </body>");
	 Put_Line(" </html>");
   end if;
end Webspir ; 
