--  Verada predefined library: Ada.Directories (Ada RM A.16): the host's directories and files,
--  by name, and searches of a directory. The host's files are Text_IO's File_System state, so
--  these operations take their order with Text_IO's from the flow contracts. A procedure that
--  changes the file system propagates Name_Error (no such file or directory, or a name that
--  cannot be one) or Use_Error (the host refuses), as Ada's does, and its Exceptional_Cases
--  say so; a function propagates no exception, so what Ada raises Name_Error for is a
--  precondition (Exists). Verada leaves out Modification_Time, whose Ada.Calendar time depends
--  on the host's zone, and adds Modification_Seconds, the time in seconds since the epoch
--  (1970-01-01 00:00:00 UTC). The name functions (Simple_Name, Containing_Directory,
--  Extension, Base_Name, Compose) work on the text alone, with `/` as the separator, and
--  take any string (an empty name has an empty simple name, and `.` contains it).
with Ada.IO_Exceptions;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with Verada.Storage;

package Ada.Directories is
   Status_Error : exception renames Ada.IO_Exceptions.Status_Error;
   Name_Error   : exception renames Ada.IO_Exceptions.Name_Error;
   Use_Error    : exception renames Ada.IO_Exceptions.Use_Error;
   Device_Error : exception renames Ada.IO_Exceptions.Device_Error;

   --  ---- directory and file operations ----

   function Current_Directory return String
     with Global => Ada.Text_IO.File_System,
          Post => Current_Directory'Result'Length in 1 .. Verada.Storage.Length_Bound - 1;

   procedure Set_Directory (Directory : String)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   procedure Create_Directory (New_Directory : String; Form : String := "")
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   --  (an empty directory only: Delete_Tree removes one with what it holds)
   procedure Delete_Directory (Directory : String)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   --  every directory on the path that is not there yet
   procedure Create_Path (New_Directory : String; Form : String := "")
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   procedure Delete_Tree (Directory : String)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   procedure Delete_File (Name : String)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   --  (Use_Error when New_Name is an existing file)
   procedure Rename (Old_Name, New_Name : String)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   procedure Copy_File (Source_Name, Target_Name : String; Form : String := "")
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   --  ---- file and directory name operations ----

   --  the name made absolute against the current directory, with `.` and `..` resolved (the
   --  file need not exist)
   function Full_Name (Name : String) return String
     with Global => Ada.Text_IO.File_System,
          Post => Full_Name'Result'Length in 1 .. Verada.Storage.Length_Bound - 1;

   --  the name after its last `/` (the name itself when it ends in one or has none)
   function Simple_Name (Name : String) return String
     with Global => null,
          Post => Simple_Name'Result'Length <= Name'Length + 1 and then Simple_Name'Result'First = 1;

   --  the name before its last `/` (`/` for a name at the root, `.` for one with no `/`)
   function Containing_Directory (Name : String) return String
     with Global => null,
          Post => Containing_Directory'Result'Length <= Name'Length + 1 and then Containing_Directory'Result'First = 1;

   --  the simple name's text after its last `.` (empty when there is none)
   function Extension (Name : String) return String
     with Global => null,
          Post => Extension'Result'Length <= Name'Length + 1 and then Extension'Result'First = 1;

   --  the simple name without its extension and the `.` before it
   function Base_Name (Name : String) return String
     with Global => null,
          Post => Base_Name'Result'Length <= Name'Length + 1 and then Base_Name'Result'First = 1;

   --  Containing_Directory, `/`, Name, and `.` and Extension when there is one (each part
   --  shorter than twice the longest string memory holds, so that a name composed of names
   --  can be composed again)
   function Compose (Containing_Directory : String := ""; Name : String; Extension : String := "") return String
     with Global => null,
          Post => Compose'Result'Length <= Containing_Directory'Length + Name'Length + Extension'Length + 2
                  and then Compose'Result'First = 1,
          Pre => Containing_Directory'Length < 2 * Verada.Storage.Length_Bound
                 and then Name'Length < 2 * Verada.Storage.Length_Bound
                 and then Extension'Length < 2 * Verada.Storage.Length_Bound;

   type Name_Case_Kind is (Unknown, Case_Sensitive, Case_Insensitive, Case_Preserving);

   function Name_Case_Equivalence (Name : String) return Name_Case_Kind
     with Global => Ada.Text_IO.File_System,
          Pre => Exists (Name);

   --  ---- file and directory queries ----

   type File_Kind is (Directory, Ordinary_File, Special_File);

   type File_Size is range 0 .. Long_Long_Integer'Last;

   function Exists (Name : String) return Boolean
     with Global => Ada.Text_IO.File_System;

   function Kind (Name : String) return File_Kind
     with Global => Ada.Text_IO.File_System,
          Pre => Exists (Name);

   function Size (Name : String) return File_Size
     with Global => Ada.Text_IO.File_System,
          Pre => Exists (Name);

   --  (Verada's: the time the file was last changed, in seconds since the epoch)
   function Modification_Seconds (Name : String) return Long_Long_Integer
     with Global => Ada.Text_IO.File_System,
          Pre => Exists (Name);

   --  ---- directory searching ----

   --  an entry a search returned (an entry no search has filled: empty names, an ordinary
   --  file of size 0)
   type Directory_Entry_Type is limited private;

   type Filter_Type is array (File_Kind) of Boolean;

   --  a search, not open until Start_Search opens it
   type Search_Type is limited private
     with Default_Initial_Condition => not Is_Open (Search_Type);

   --  (Verada's: the search was started and not ended)
   function Is_Open (Search : Search_Type) return Boolean
     with Global => null;

   --  the entries of Directory whose simple names match Pattern (`*` any characters, `?` one,
   --  `[...]` one of a set; an empty Pattern matches every name) and whose kinds Filter
   --  holds, in the order of their names' bytes, as they were when the search started
   procedure Start_Search (Search : in out Search_Type; Directory : String; Pattern : String;
                           Filter : Filter_Type := (others => True))
     with Global => (In_Out => Ada.Text_IO.File_System),
          Post => Is_Open (Search),
          Exceptional_Cases => (Name_Error | Use_Error => not Is_Open (Search));

   procedure End_Search (Search : in out Search_Type)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Post => not Is_Open (Search);

   function More_Entries (Search : Search_Type) return Boolean
     with Global => Ada.Text_IO.File_System;

   --  the next entry; Status_Error when there is none
   procedure Get_Next_Entry (Search : in out Search_Type; Directory_Entry : out Directory_Entry_Type)
     with Global => (In_Out => Ada.Text_IO.File_System),
          Post => Is_Open (Search) = Is_Open (Search)'Old,
          Exceptional_Cases => (Status_Error => True);

   --  Process called with each entry a search of Directory for Pattern and Filter returns
   procedure Search (Directory : String; Pattern : String; Filter : Filter_Type := (others => True);
                     Process : not null access procedure (Directory_Entry : Directory_Entry_Type))
     with Global => (In_Out => Ada.Text_IO.File_System),
          Exceptional_Cases => (Name_Error | Use_Error => True);

   --  ---- operations on directory entries ----

   function Simple_Name (Directory_Entry : Directory_Entry_Type) return String
     with Global => null,
          Post => Simple_Name'Result'Length < Verada.Storage.Length_Bound
                  and then (Simple_Name'Result'Length = 0 or else Simple_Name'Result'First = 1);

   function Full_Name (Directory_Entry : Directory_Entry_Type) return String
     with Global => null,
          Post => Full_Name'Result'Length < Verada.Storage.Length_Bound
                  and then (Full_Name'Result'Length = 0 or else Full_Name'Result'First = 1);

   function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind
     with Global => null;

   function Size (Directory_Entry : Directory_Entry_Type) return File_Size
     with Global => null;

   --  (Verada's, as for a name)
   function Modification_Seconds (Directory_Entry : Directory_Entry_Type) return Long_Long_Integer
     with Global => null;

private
   type Directory_Entry_Type is limited record
      Simple   : Ada.Strings.Unbounded.Unbounded_String;
      Full     : Ada.Strings.Unbounded.Unbounded_String;
      Kind_Of  : File_Kind := Ordinary_File;
      Size_Of  : File_Size := 0;
      Seconds  : Long_Long_Integer := 0;
   end record;

   --  a handle of the run time's searches (0: not open)
   type Search_Type is limited record
      Handle : Long_Long_Integer := 0;
   end record;

   function Is_Open (Search : Search_Type) return Boolean is (Search.Handle /= 0);
end Ada.Directories;
