--  Ada.Directories: a directory made, files copied, renamed and searched (in name order),
--  their kinds and sizes, the name functions, Modification_Seconds, and Name_Error and
--  Use_Error as Ada's.
with Ada.Directories; use Ada.Directories;
with Ada.Text_IO;

procedure Main is
   Root : constant String := Compose (Current_Directory, "verada-directories-test");
   F : Ada.Text_IO.File_Type;
   S : Search_Type;
   E : Directory_Entry_Type;

   procedure Show (Item : Directory_Entry_Type) is
   begin
      if Kind (Item) = Ordinary_File then
         Ada.Text_IO.Put_Line ("  " & Simple_Name (Item) & " " & File_Kind'Image (Kind (Item)) & File_Size'Image (Size (Item)));
      else
         Ada.Text_IO.Put_Line ("  " & Simple_Name (Item) & " " & File_Kind'Image (Kind (Item)));
      end if;
   end Show;

begin
   if Exists (Root) then
      Delete_Tree (Root);
   end if;
   Create_Path (Compose (Root, "sub/deeper"));
   Ada.Text_IO.Create (F, Ada.Text_IO.Out_File, Compose (Root, "a", "txt"));
   Ada.Text_IO.Put_Line (F, "hello");
   Ada.Text_IO.Close (F);
   Copy_File (Compose (Root, "a", "txt"), Compose (Root, "b", "txt"));
   Rename (Compose (Root, "b", "txt"), Compose (Root, "c", "dat"));
   Ada.Text_IO.Put_Line ("size of a.txt:" & File_Size'Image (Size (Compose (Root, "a", "txt"))));
   Ada.Text_IO.Put_Line ("recent: " & Boolean'Image (Modification_Seconds (Compose (Root, "a", "txt")) > 1_700_000_000));

   Ada.Text_IO.Put_Line ("all:");
   Start_Search (S, Root, "");
   while More_Entries (S) loop
      Get_Next_Entry (S, E);
      Show (E);
   end loop;
   End_Search (S);
   Ada.Text_IO.Put_Line ("*.txt and *.dat, files only:");
   Start_Search (S, Root, "*.*", (Ordinary_File => True, others => False));
   while More_Entries (S) loop
      Get_Next_Entry (S, E);
      Show (E);
   end loop;
   End_Search (S);

   Ada.Text_IO.Put_Line (Simple_Name ("/a/b/c.tar.gz") & " " & Containing_Directory ("/a/b/c.tar.gz") & " "
                         & Extension ("/a/b/c.tar.gz") & " " & Base_Name ("/a/b/c.tar.gz") & " "
                         & Containing_Directory ("x") & " " & Containing_Directory ("/x"));
   Ada.Text_IO.Put_Line (Full_Name ("/a/./b/../c//d"));

   begin
      Delete_Directory (Root);
   exception
      when Use_Error => Ada.Text_IO.Put_Line ("Use_Error: not empty");
   end;
   begin
      Delete_File (Compose (Root, "nothing"));
   exception
      when Name_Error => Ada.Text_IO.Put_Line ("Name_Error: no such file");
   end;
   Delete_Tree (Root);
   Ada.Text_IO.Put_Line ("gone: " & Boolean'Image (not Exists (Root)));
end Main;
