--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library 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           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser 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    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with Ada.Strings;              use Ada.Strings;


with RASCAL.FileInternal;      use RASCAL.FileInternal;
with RASCAL.FileExternal;
with RASCAL.Variable;

package body RASCAL.CDDB is

   --
   --
   --
   function Search_CDBurnDB (CD_ID : in Integer;
                             Nudge : in Integer) return Album_Type is

      Empty : Album_Type(0);
      CDBurn: String := Variable.Get_Variable ("CDBurn$Dir");
      DBPath: String := CDBurn & ".cddatabase";
   begin
      if CDBurn'Length = 0 then
         return Empty;
      end if;
      if not FileExternal.Exists (DBPath) then
         return Empty;
      end if;

      RASCAL.FileExternal.Close_AllInPath (DBPath);
--      declare
--         DBFile : FileHandle_Type(new UString'(U(DBPath)),Read);
--         Lines  : Line_List_Type := RASCAL.FileInternal.Read_Lines(DBFile,Trim => True);
--         Name,Artist,Title    : Unbounded_String;
--         Start,nr_end,ID,Nr,i : Integer := 0;
--         Found,title_read     : boolean := false;
--      begin
--         for x in Lines'range loop
--            if not Found then
--               if Element (Lines(x),1) = '#' then
--                  Lines(x) := U(Slice (Lines(x),2,Length(Lines(x))));
--                  Ada.Strings.Unbounded.Trim(Lines(x),both);
--                  ID  := strint(S(Lines(x)));
--                  if (ID = CD_ID) or
--                     (ID < (CD_ID+Nudge) and ID > (CD_ID-Nudge)) then
--                     found := true;                     
--                  end if;
--               end if;
--            elsif Found and not title_read then
--               Title := Lines(x);
--               i := Index (Lines(x),"/");
--               if i = 0 then
--                  i := Index (Lines(x),":");
--               end if;
--               if i = 0 then
--                  i := Index (Lines(x),"-");
--               end if;
--               Name   := Lines(x); Artist := U("");
--               if i > 0 then
--                  Name   := U(Slice (Lines(x),i+1,Length(Lines(x))));
--                  Ada.Strings.Unbounded.Trim(Name,both);
--                  Artist := U(Slice (Lines(x),1,i-1));
--                  Ada.Strings.Unbounded.Trim(Artist,both);
--               end if;
--               start := x; title_read := true;              
--            else
--               nr_end := x;
--               exit when Element (Lines(x),1) = '#';
--            end if;
--         end loop;
--         declare
--            Album : Album_Type(nr_end-1-start);
--         begin
--            Album.Name   := Name;
--            Album.Artist := Artist;
--            Album.Title  := Title;
--            for x in Album.Tracks'range loop
--               if Length (Lines(x)) > 0 then
--                  Album.Tracks(x) := Lines(start-1+x);
--               end if;
--            end loop;
--            return Album;
--         end;
--      exception
--         when others => null;
--      end;
      return Empty;
   end Search_CDBurnDB;

   --
   
end RASCAL.CDDB;