go.adb


------------------------------------------------------------------------------
--
--  procedure Go (body)
--
--  This program decodes a request for a step in the Web tour, and returns
--  the corresponding document.
--
--  To run this program directly (without an HTTP server), set the
--  environment variable REQUEST_METHOD to "GET" and the variable
--  QUERY_STRING to "step=3&lang=fo" (or something similar).
--
------------------------------------------------------------------------------
--  Update information:
--
--  1996.07.15 (Jacob Sparre Andersen)
--    Written. Based on Get (1996.06.10) for TTI.
--
--  1996.07.22 (Jacob Sparre Andersen)
--    Changed package Debugged to package Debugging.
--    Inserted exception reporting facilities.
--    Changed package Debugging to package Debugged.
--
--  1996.08.06 (Jacob Sparre Andersen)
--    Using package CGI.Log.
--
--  (Insert additional update information above this line.)
------------------------------------------------------------------------------

with Ada.Characters.Handling;
with Ada.Strings;
with Ada.Text_IO;

with CGI;
with CGI.Flags;
with CGI.Log;
with Debugged;
with Trim_Special_Characters;
with UStrings;

procedure Go is

   use Debugged;

   ---------------------------------------------------------------------------
   --  function Image:

   function Image (Item : in CGI.Flags.Language_Type) return String is

   begin --  Image
      Message_Line ("Go.Image (""" & Item (1) & Item (2) & """): begin ...");

      return (1 => Item (1), 2 => Item (2));
   exception
      when others =>
         Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                               "Go.Image: Unexpected exception.");
         raise;
   end Image;

   ---------------------------------------------------------------------------
   --  type Step_Type:

   type Step_Type is new Integer range -1 .. Integer'Last;

   Start_Document : constant Step_Type :=  0;
   End_Document   : constant Step_Type := -1;

   ---------------------------------------------------------------------------
   --  function To_Step:

   function To_Step (Step : in String) return Step_Type is

      use Ada.Characters.Handling;

   begin --  To_Step
      Message_Line ("Go.To_Step (""" & Step & """): begin ...");

      if To_Lower (Step) = "start" then
         return Start_Document;
      elsif To_Lower (Step) = "end" then
         return End_Document;
      else
         return Step_Type'Value (Step);
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                               "Go.To_Step: Unexpected exception.");
         raise;
   end To_Step;

   ---------------------------------------------------------------------------
   --  function Image:

   function Image (Step : in Step_Type) return String is

      use Ada.Strings;

   begin --  Image
      Message_Line ("Go.Image (" & Step_Type'Image (Step) & "): begin ...");

      case Step is
         when Start_Document =>
            return "start";
         when End_Document =>
            return "end";
         when others =>
            return Trim_Special_Characters (Step_Type'Image (Step), Both);
      end case;
   exception
      when others =>
         Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                               "Go.Image: Unexpected exception.");
         raise;
   end Image;

   ---------------------------------------------------------------------------
   --  function Document_File_Name:
   --
   --  Returns a document file name for the specified step in the tour.

   function Document_File_Name
     (Step     : in Step_Type;
      Language : in CGI.Flags.Language_Type) return String is

      use CGI.Flags;

   begin --  Document_File_Name
      Message_Line ("Go.Document_File_Name: begin ...");

      if Step = Start_Document then
         return "start." & Image (Language) & ".html";
      elsif Step = End_Document then
         return "end." & Image (Language) & ".html";
      else
         if HTML_Version = Netscape then
            return "step-" & Image (Step) & "-netscape." & Image (Language) &
                     ".html";
         else
            return "step-" & Image (Step) & "-normal." & Image (Language) &
                     ".html";
         end if;
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                               "Go.Document_File_Name: Unexpected exception.");
         raise;
   end Document_File_Name;

   ---------------------------------------------------------------------------
   --  procedure Echo_Document:
   --
   --  Writes a CGI document header and the specified document to
   --  Standard_Output.

   procedure Echo_Document
     (Step     : in     Step_Type;
      Language : in     CGI.Flags.Language_Type) is

      use Ada.Text_IO;
      use UStrings;

      Document : File_Type;
      Line     : UString;

   begin --  Echo_Document
      Message_Line ("Go.Echo_Document: begin ...");

      Message_Line ("Go.Echo_Document: Document file name: """ &
                      Document_File_Name (Step, Language) & """.");

      Open (File => Document,
            Name => Document_File_Name (Step, Language),
            Mode => In_File);

      Put_Line ("Content-type: text/html");
      New_Line;

      while not End_Of_File (Document) loop
         Get_Line (File => Document, Item => Line);
         Put_Line (Line);
      end loop;

      Close (File => Document);

      Message_Line ("Go.Echo_Document: end.");
   exception
      when others =>
         Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                               "Go.Echo_Document: Unexpected exception.");
         raise;
   end Echo_Document;

   ---------------------------------------------------------------------------

   use Ada.Text_IO;
   use CGI.Log;

   Log_Selection : constant Information_Selection_Type :=
     (Request_Method | Auth_Type => False,
      others                     => True);

   Language_Priorities : CGI.Flags.Language_List_Type :=
     CGI.Flags.Language_Priority_List;

   Detected_Language : CGI.Flags.Language_Type := CGI.Flags.English;

begin --  Go
   Message_Line ("Go: begin");

   if Language_Priorities'Length >= 1 then
      Message_Line ("Go: Detected preferred language.");

      Detected_Language := Language_Priorities (Language_Priorities'First);
   else
      Message_Line ("Go: No valid language selection.");

      Detected_Language := CGI.Flags.English;
   end if;

   if CGI.Is_Index then
      Message_Line ("Go: IS_INDEX query.");

      Echo_Document (Step     => To_Step (CGI.Value ("isindex")),
                     Language => Detected_Language);
   elsif CGI.Key_Exists ("step") then
      Echo_Document (Step     => To_Step (CGI.Value ("step")),
                     Language => Detected_Language);
   else
      Message_Line ("Go: No valid query.");

      Put_Line ("Location: go.cgi?step=start" &
                                "&lang=" & Image (Detected_Language));
      New_Line;
   end if;

   CGI.Log.Put (Log_Selection);

   Message_Line ("Go: end");
exception
   when others =>
      Put_Line ("Location: error.html");
      New_Line;
      CGI.Log.Put (Comment => "An error occurred.");
end Go;

Typeset with Ada_To_HTML (Jacob and Jesper)