with Io;
with Log;
with Links;
with Names;
with Errors;
with Syntax;
with Archive;
with Compose;
with Library;
with Profile;
with Subtypes;
use Subtypes;
with Make_Tape;
with Arguments;
with Pathnames;
with Structure;
with Debug_Tools;
with Object_Info;
with Compilation;
with Directory_Tools;
with Check_Consistency;
with Structure_Utilities;
with Low_Level_File_Operations;
procedure Build_Release (Of_Catalog : in Simple_Name := "<DEFAULT>";
                         This_Version : in Simple_Name := "<DEFAULT>";
                         Check_Consistency : in Boolean := True;
                         Make_Tape : 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);
    --
    package Error is
       new Errors  
              (Old_Response_Profile, New_Response_Profile, "Build_Release");
    --
    procedure Create_Workspace_For (This_Version : in Full_Name) is
    begin
        Log.Put_Line ("Destroying any pre-existing temporary workspace");
        Library.Delete (Existing => Pathnames.Workspace_For (This_Version),
                        Limit => "<WORLDS>",
                        Response => "PERSEVERE,<PROFILE>");
        Log.Put_Line ("Previous workspace destroyed", Profile.Positive_Msg);
        Log.Put_Line ("Creating temporary workspace");
        Library.Create (Name => Pathnames.Workspace_For (This_Version),
                        Kind => Library.World,
                        Model => "",
                        Response => "PROPAGATE,<PROFILE>");
        Log.Put_Line ("Workspace created", Profile.Positive_Msg);
        --
    exception
        when others =>
            Error.Report ("Workspace creation failed, quitting",
                          Nested => True);
            --
    end Create_Workspace_For;
    --
    procedure Check_Consistency_Of (This_Catalog : in Full_Name;
                                    This_Version : in Simple_Name) is
    begin
        Standard.Check_Consistency (This_Catalog, This_Version,
                                    Response => "PROPAGATE,<PROFILE>");
    exception
        when others =>
            Error.Report ("Consistency check failed, quitting", Nested => True);
            --
    end Check_Consistency_Of;
    --
    procedure Generate_Load_Proc_For  
                 (This_Version : in Full_Name) is separate;
    --
    procedure Generate_Browser_Version_Of_Data_File_For
                 (This_Version : in Full_Name) is separate;
    --
    procedure Generate_Archive_Of (This_Version : in Full_Name) is separate;
    --
    procedure Generate_Tape_For (This_Catalog : in Full_Name;
                                 This_Version : in Simple_Name) is
    begin
        Standard.Make_Tape (This_Catalog, This_Version,
                            Response => "PROPAGATE,<PROFILE>");
    exception
        when others =>
            Error.Report ("Tape generation failed, quitting", Nested => True);
            --
    end Generate_Tape_For;
    --
    procedure Clean_Up (This_Version : in Full_Name) is
    begin
        Log.Put_Line ("Cleaning up");
        Library.Delete (Existing => Pathnames.Workspace_For (This_Version),
                        Limit => "<ALL_WORLDS>",
                        Response => "PERSEVERE,<PROFILE>");
        Log.Put_Line ("Done cleaning", Profile.Positive_Msg);
    end Clean_Up;
    --
begin
    Profile.Set (New_Response_Profile);
    Log.Put_Line ("[Build_Release (Of_Catalog => """ &
                  Of_Catalog & """, This_Version => """ &
                  This_Version & """, Check_Consistency => " &
                  Boolean'Image (Check_Consistency) &
                  ", Make_Tape => " & Boolean'Image (Make_Tape) &
                  ", Response => """ & Response & """)]",
                  Profile.Auxiliary_Msg);
    declare  
        The_Catalog : constant Full_Name := Arguments.Catalog_From (Of_Catalog);
        The_Catalog_Simple : constant Simple_Name :=
           Arguments.Simple_Catalog_From (Of_Catalog);
        The_Version_Simple : constant Full_Name :=
           Arguments.Simple_Version_From (Of_Catalog, This_Version);
        The_Version : constant Full_Name :=
           Pathnames.Full_Version_Name (The_Catalog, The_Version_Simple);
    begin
        if (Check_Consistency) then
            Check_Consistency_Of (The_Catalog, The_Version_Simple);
        end if;
        Create_Workspace_For (The_Version);
        Generate_Load_Proc_For (The_Version);
        Generate_Browser_Version_Of_Data_File_For (The_Version);
        Generate_Archive_Of (The_Version);
        if (Make_Tape) then
            Generate_Tape_For (The_Catalog_Simple, The_Version_Simple);
        end if;
        Clean_Up (The_Version);
    end;  
    Log.Put_Line ("[end of Build_Release operation--no errors(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 Error.Propagate =>
        raise;
    when Error.Quit =>
        null;
    when others =>
        Error.Report ("EXCEPTION: " & Debug_Tools.Get_Exception_Name,
                      This_Type => Profile.Exception_Msg);
        --
end Build_Release;