with Log;
with Queue;
with Errors;
with Profile;
with Printer;
with Subtypes;
use Subtypes;
with Arguments;
with Pathnames;
with Assertions;
with Debug_Tools;
with Format_Booklet;
with Generate_Booklet;
procedure Print_Booklet (For_Catalog : in Simple_Name := "<DEFAULT>";
                         This_Version : in Simple_Name := "<DEFAULT>";
                         Regenerate : in Boolean := True;
                         Reformat : in Boolean := True;
                         Response : in String := "<PROFILE>") is
    --
    Old_Response_Profile : Profile.Response_Profile := Profile.Get;
    New_Response_Profile : Profile.Response_Profile := Profile.Value (Response);
    --
    Was_Regenerated : Boolean := False;
    --
    package Error is
       new Errors  
              (Old_Response_Profile, New_Response_Profile, "Print_Booklet");
    --
    function Has_No_Booklet (This_Version : in Full_Name) return Boolean is
    begin
        Assertions.Assert_Has_No_Booklet (This_Version);
        return (True);
        --
    exception
        when others =>
            return (False);
            --
    end Has_No_Booklet;
    --
    procedure Regenerate_If_Necessary (The_Version : in Full_Name) is
        --
        No_Booklet : constant Boolean := Has_No_Booklet (The_Version);
        --
    begin
        if (Regenerate or else No_Booklet) then
            if (No_Booklet) then
                Log.Put_Line ("Version has no booklet");
            end if;
            begin
                Generate_Booklet (For_Catalog, This_Version,
                                  Response => "PROPAGATE," & Response);
                Was_Regenerated := True;
                --
            exception
                when others =>
                    Error.Report ("Booklet generation failed", Nested => True);
            end;
        end if;
    end Regenerate_If_Necessary;
    --
    function Has_No_Formatted_Booklet
                (This_Version : in Full_Name) return Boolean is
    begin
        Assertions.Assert_Has_No_Formatted_Booklet (This_Version);
        return (True);
        --
    exception
        when others =>
            return (False);
            --
    end Has_No_Formatted_Booklet;
    --
    procedure Reformat_If_Necessary (The_Version : in Full_Name) is
        --
        No_Formatted_Booklet : constant Boolean :=
           Has_No_Formatted_Booklet (The_Version);
        --
    begin
        if (Was_Regenerated or else Reformat or else No_Formatted_Booklet) then
            if (Was_Regenerated) then
                Log.Put_Line ("Formatting regenerated booklet");
            elsif (No_Formatted_Booklet) then
                Log.Put_Line ("Version has no formatted booklet");
            end if;
            begin
                Format_Booklet (For_Catalog, This_Version,
                                Regenerate => False,
                                Response => "PROPAGATE," & Response);
                --
            exception
                when others =>
                    Error.Report ("Booklet formatting failed", Nested => True);
            end;
        end if;
    end Reformat_If_Necessary;
    --
begin
    Profile.Set (New_Response_Profile);
    Log.Put_Line ("[Print_Booklet (For_Catalog => """ & For_Catalog &
                  """, This_Version => """ & This_Version &
                  """, Regenerate => " & Boolean'Image (Regenerate) &
                  ", Reformat => " & Boolean'Image (Reformat) &
                  ", Response => """ & Response & """)]",
                  Profile.Auxiliary_Msg);
    declare
        The_Version : constant Full_Name :=
           Arguments.Version_From (For_Catalog, This_Version);
    begin  
        Regenerate_If_Necessary (The_Version);
        Reformat_If_Necessary (The_Version);
        Log.Put_Line ("Sending formatted booklet to printer");
        Queue.Print (Name => Pathnames.Formatted_Booklet_For (The_Version),
                     Options => Printer.Default_Options_For (The_Version));
    end;
    Log.Put_Line ("[end of Print_Booklet operation--no error(s) detected]");
    Profile.Set (Old_Response_Profile);
    --
exception
    when Arguments.Invalid_Catalog =>
        Error.Report ("Specified catalog is invalid");
    when Arguments.Invalid_Version =>
        Error.Report ("Specified version is invalid");
    when Arguments.Io_Failure =>
        Error.Report ("Can't find default(s)--run ""Catalog.Set_Defaults""");
    when Printer.Io_Failure =>
        Error.Report ("Can't find printer defaults");
    when Error.Propagate =>
        raise;
    when Error.Quit =>
        null;
    when others =>
        Error.Report ("EXCEPTION: " & Debug_Tools.Get_Exception_Name,
                      Profile.Exception_Msg);
        --
end Print_Booklet;
