with Io;
with Queue;
with Directory;
with Debug_Tools;

procedure Lprint (File : String := "<IMAGE>";
                  Options : String := "";
                  Class : String := "!Machine.Laser_Class";
                  Trace_Only : Boolean := False) is

    Error_Termination : exception;

    function Join (A, B : String) return String is
    begin
        if B'Length = 0 then
            return A;
        elsif A'Length = 0 then
            return B;
        else
            return A & "," & B;
        end if;
    end Join;


    function Is_Ada (Name : String) return String is
        -- return format=>fancy if the first object is ada.
        Ada_Class : Directory.Class := Directory.Class_Value ("ada");
        Iter : Directory.Naming.Iterator;
        Name_Status : Directory.Naming.Name_Status;
        use Directory;
    begin
        Directory.Naming.Resolve (Iter, Name, Name_Status);
        if Directory.Naming.Done (Iter) then
            return "";
        end if;
        if Directory.Naming.Get_Class (Iter) = Ada_Class then
            return "format=>fancy";
        else
            return "";
        end if;  
    exception
        when others =>
            return "";
    end Is_Ada;

    function File_Contents (Name : String) return String is
        F : Io.File_Type;
        function Close_And_Return (S : String) return String is
        begin
            Io.Close (F);
            return S;
        end Close_And_Return;
    begin
        Io.Open (F, Io.In_File, Name);
        return Close_And_Return (Io.Get_Line (F));
    exception
        when others =>
            Io.Put_Line ("Unable to read class from file : " & Name &
                         " due to " & Debug_Tools.Get_Exception_Name);
            Io.Put_Line ("Check acls and file existence.");
            raise Error_Termination;
    end File_Contents;


begin  
    declare
        Opt : constant String :=  
           "class=>" & File_Contents (Class) & ", postscript=>(" &
              Join (Is_Ada (File), Options) & ")";
    begin

        Io.Put_Line (Io.Current_Error, "Queue.Print ("""  
                                           & File  
                                           & """, Options=>"""  
                                           & Opt  
                                           & """);");
        if not Trace_Only then
            Queue.Print (Name => File, Options => Opt);
        end if;
    end;
exception
    when Error_Termination =>
        null;
end Lprint;
pragma Main;