--------------------------------------------------------------------------------
--                                                                            --
-- 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.Unbounded; use Ada.Strings.Unbounded;

with RASCAL.FileInternal;   use RASCAL.FileInternal;
with RASCAL.Utility;        use RASCAL.Utility;
with RASCAL.FileExternal;

package body RASCAL.MP3_Tag is

   --

   procedure Add_Tag (Path : String;
                      TAG  : String) is
   begin
      if FileExternal.Exists (Path) then
         declare
            mp3 : FileHandle_Type(new UString'(U(Path)),ReadWrite);
            Mark: String (1..3);
            Len : Integer;
         begin
            Len := FileInternal.Get_Size (mp3);
            if Len > 128 then
               FileInternal.Set_Ptr (mp3,Len - 128);
               Mark (1) := Character'Val(Get_Byte(mp3));
               Mark (2) := Character'Val(Get_Byte(mp3));
               Mark (3) := Character'Val(Get_Byte(mp3));
               if Mark /= "TAG" then
                  FileInternal.GotoEnd (mp3);
               else
                  FileInternal.Set_Ptr (mp3,Len - 128);
               end if;
            else
               FileInternal.GotoEnd (mp3);
            end if;
            FileInternal.Put_String (mp3,TAG,false);
         end;
      else
         raise file_not_found;
      end if;
   end Add_Tag;

   --

end RASCAL.MP3_Tag;
