with Subtypes;
use Subtypes;
with Structure;
with Pathnames;
with Object_Info;
with Directory_Tools;
package body Consistency is

    procedure Assert_Catalogs_Match (This_Catalog : in Structure.Catalog;
                                     This_Catalog_Name : in Full_Name) is
    begin
        if (Structure.Full_Location_Of (This_Catalog) /= This_Catalog_Name) then
            raise Catalog_Mismatch;
        end if;
    end Assert_Catalogs_Match;

    procedure Assert_Versions_Match (This_Catalog : in Structure.Catalog;
                                     This_Version : in Full_Name) is
    begin
        if (Pathnames.Full_Version_Name
               (This_Catalog => Structure.Full_Location_Of (This_Catalog),
                This_Version => Structure.Version_Of (This_Catalog)) /=
            This_Version) then
            raise Version_Mismatch;
        end if;
    end Assert_Versions_Match;

    procedure Assert_Is_Simple_World (This_Catalog : in Structure.Catalog) is
        --
        The_Catalog : Object_Info.Object :=
           Structure.Actual_Catalog_For (This_Catalog);
        --
    begin
        if (Object_Info.Any.Is_Bad (The_Catalog)) then
            raise No_Such_Catalog;
        end if;
--
-- ***** COMMENTED OUT UNTIL CUSTOMER CATALOG IS CHANGED INTO A WORLD.
--
        -- if (not Object_Info.Any.Is_Simple_World (The_Catalog)) then
        --     raise Not_A_Simple_World;
        -- end if;
    end Assert_Is_Simple_World;

    procedure Assert_Is_Directory (This_Section : in Structure.Section) is
        --
        The_Section : Object_Info.Object :=
           Structure.Actual_Section_For (This_Section);
        --
    begin
        if (Object_Info.Any.Is_Bad (The_Section)) then
            raise No_Such_Section;
        end if;
        if (not Object_Info.Any.Is_Directory (The_Section)) then
            raise Not_A_Directory;
        end if;
    end Assert_Is_Directory;

    procedure Assert_Is_Major_Class_World (This_Item : in Structure.Item) is
        --
        The_Item : Object_Info.Object := Structure.Actual_Item_For (This_Item);
        --
    begin
        if (Object_Info.Any.Is_Bad (The_Item)) then
            raise No_Such_Item;
        end if;
        if (not Object_Info.Any.Is_World (The_Item)) then
            raise Not_Major_Class_World;
        end if;
    end Assert_Is_Major_Class_World;

    procedure Assert_Is_Subsystem (This_Item : in Structure.Item) is
        --
        The_Item : Object_Info.Object := Structure.Actual_Item_For (This_Item);
        --
    begin
        if (Object_Info.Any.Is_Bad (The_Item)) then
            raise No_Such_Item;
        end if;
        if (not Object_Info.Any.Is_Subsystem (The_Item)) then
            raise Not_A_Subsystem;
        end if;
    end Assert_Is_Subsystem;

    procedure Assert_Has_Boilerplate_File
                 (This_Free_Form_Item : in Structure.Item) is
        --
        The_Proprietary_Boilerplate_File : Object_Info.Object;
        The_Public_Domain_Boilerplate_File : Object_Info.Object;
        --
    begin
        The_Proprietary_Boilerplate_File :=
           Directory_Tools.Naming.Resolution
              (Pathnames.Free_Form_Proprietary_Boilerplate_File
                  (Structure.Full_Location_Of (This_Free_Form_Item)));
        The_Public_Domain_Boilerplate_File :=
           Directory_Tools.Naming.Resolution
              (Pathnames.Free_Form_Public_Domain_Boilerplate_File
                  (Structure.Full_Location_Of (This_Free_Form_Item)));
        if (Structure.Is_Proprietary (This_Free_Form_Item)) then
            if (Object_Info.Any.Is_Bad (The_Proprietary_Boilerplate_File)) then
                if (Object_Info.Any.Is_Good
                       (The_Public_Domain_Boilerplate_File)) then
                    raise Has_Wrong_Kind_Of_Boilerplate_File;
                else
                    raise Has_No_Boilerplate_File;
                end if;
            end if;
        else
            if (Object_Info.Any.Is_Bad
                   (The_Public_Domain_Boilerplate_File)) then
                if (Object_Info.Any.Is_Good
                       (The_Proprietary_Boilerplate_File)) then
                    raise Has_Wrong_Kind_Of_Boilerplate_File;
                else
                    raise Has_No_Boilerplate_File;
                end if;
            end if;
        end if;
    end Assert_Has_Boilerplate_File;

    procedure Assert_Has_Boilerplate_File (This_View : in Object_Info.Any.View;
                                           This_Item : in Structure.Item) is
        --
        The_Proprietary_Boilerplate_File : Object_Info.Object;
        The_Public_Domain_Boilerplate_File : Object_Info.Object;
        --
    begin
        The_Proprietary_Boilerplate_File :=
           Directory_Tools.Naming.Resolution
              (Pathnames.Proprietary_Boilerplate_File_For
                  (Directory_Tools.Naming.Full_Name (This_View)));
        The_Public_Domain_Boilerplate_File :=
           Directory_Tools.Naming.Resolution
              (Pathnames.Public_Domain_Boilerplate_File_For
                  (Directory_Tools.Naming.Full_Name (This_View)));
        if (Structure.Is_Proprietary (This_Item)) then
            if (Object_Info.Any.Is_Bad (The_Proprietary_Boilerplate_File)) then
                if (Object_Info.Any.Is_Good
                       (The_Public_Domain_Boilerplate_File)) then
                    raise Has_Wrong_Kind_Of_Boilerplate_File;
                else
                    raise Has_No_Boilerplate_File;
                end if;
            end if;
        else
            if (Object_Info.Any.Is_Bad
                   (The_Public_Domain_Boilerplate_File)) then
                if (Object_Info.Any.Is_Good
                       (The_Proprietary_Boilerplate_File)) then
                    raise Has_Wrong_Kind_Of_Boilerplate_File;
                else
                    raise Has_No_Boilerplate_File;
                end if;
            end if;
        end if;
    end Assert_Has_Boilerplate_File;

    procedure Assert_Has_Read_Me_File
                 (This_Free_Form_Item : in Structure.Item) is
        --
        Read_Me_Files : Object_Info.Objects :=
           Directory_Tools.Naming.Resolution
              (Pathnames.All_Read_Me_Files_In
                  (Structure.Full_Location_Of (This_Free_Form_Item)));
        --
    begin
        Directory_Tools.Object.Reset (Read_Me_Files);
        if (Directory_Tools.Object.Done (Read_Me_Files)) then
            raise Has_No_Read_Me_File;
        end if;
    end Assert_Has_Read_Me_File;

    procedure Assert_Has_Read_Me_File (This_View : in Object_Info.Any.View) is
        --
        Read_Me_File : Object_Info.Object :=
           Directory_Tools.Naming.Resolution
              (Pathnames.Read_Me_File_For
                  (Directory_Tools.Naming.Full_Name (This_View)));
        --
    begin
        if (Directory_Tools.Object.Is_Bad (Read_Me_File)) then
            raise Has_No_Read_Me_File;
        end if;
    end Assert_Has_Read_Me_File;

    procedure Assert_Model_Allowable (This_Model : in Model;
                                      This_Catalog : in Structure.Catalog) is
        --
        Models : Structure.Object_List :=
           Structure.Allowable_Models_For (This_Catalog);
        --
        Match_Found : Boolean := False;
        --
    begin
        Structure.Reset (Models);
        while (not (Structure.Done (Models))) loop
            if (Structure.Name_Of (Structure.Current_Object (Models)) =
                Directory_Tools.Naming.Full_Name (This_Model)) then
                Match_Found := True;
                exit;
            end if;
            Structure.Next (Models);
        end loop;
        if (not Match_Found) then
            raise Invalid_Model;
        end if;
    end Assert_Model_Allowable;

end Consistency;