-----------------------------------------------------------------------
--                               G P S                               --
--                                                                   --
--                      Copyright (C) 2010, AdaCore                  --
--                                                                   --
-- GPS is free  software; you can  redistribute it and/or modify  it --
-- under the terms of the GNU General Public License as published by --
-- the Free Software Foundation; either version 2 of the License, or --
-- (at your option) any later version.                               --
--                                                                   --
-- This program is  distributed in the hope that it will be  useful, --
-- but  WITHOUT ANY WARRANTY;  without even the  implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU --
-- General Public License for more details. You should have received --
-- a copy of the GNU General Public License along with this library; --
-- if not,  write to the  Free Software Foundation, Inc.,  59 Temple --
-- Place - Suite 330, Boston, MA 02111-1307, USA.                    --
-----------------------------------------------------------------------

--  Command-line tester for the template project system.

with Ada.Containers;        use Ada.Containers;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;           use Ada.Text_IO;

with GNATCOLL.VFS;          use GNATCOLL.VFS;

with Project_Templates;     use Project_Templates;

procedure Main is

   Errors    : Unbounded_String;

   use Project_Templates_List;
   Templates : List;
   C         : Cursor;
   T         : Project_Template;

   use type Ada.Containers.Count_Type;

   Dir       : Virtual_File;

begin
   --  Read the "templates" dir

   Read_Templates_Dir
     (Dir       => Create (+"templates"),
      Errors    => Errors,
      Templates => Templates);

   if Errors /= Null_Unbounded_String then
      Put_Line (To_String (Errors));
      return;
   end if;

   if Templates.Length = 0 then
      Put_Line ("No template found.");
      return;
   end if;

   --  Install all found templates in the "output" directory

   C := Templates.First;

   while Has_Element (C) loop
      T := Element (C);

      Errors := Null_Unbounded_String;

      Instantiate_Template
        (Template    => T,
         Target_Dir  =>
           Create_From_Dir (Create ("output"), +To_String (T.Label)),
         Assignments => Default_Assignments (T.Variables),
         Errors      => Errors);

      if Errors /= Null_Unbounded_String then
         Put_Line (To_String (Errors));
         return;
      end if;

      Next (C);
   end loop;
end Main;
