Subroutine Send_Info_Target(File)
/*
    Controls the output of SEND_INFO_TO subroutine.

    Parameters  :
        File        [in] -- The path to a file where output should be redirected. If path is empty then SEND_INFO will
                            stop being redirected.

*/

Declare function    RTI_OS_Directory
Declare subroutine  Send_Info, Set_Status, RTP27, Delete_Row, Write_Row

$insert Logical

// Set a named common so the SEND_INFO redirect knows which file to update.
Common /SEND_INFO_TO/ sitFile@

Error   = ''
If Unassigned(sitFile@) then sitFile@ = ''
If Unassigned(File) then File = ''

If File NE '' then
    // Create an MD pointer for our alias to the the stock SEND_INFO routine.
    TableName   = 'MD'
    AliasKey    = 'SEND_INFO_STOCK'
    Rec         = ''
    Rec<1>      = 'P'
    Rec<5>      = 'SYSOBJ'
    Rec<6>      = 'SEND_INFO'
    Set_Status(0)
    Write_Row(TableName, AliasKey, Rec, 0)
    If Get_Status(StatusCode) then
        Error   = 'Unable to setup hooking. Pointer record ' : TableName : '*' : AliasKey : ' could not be written.'
        Send_Info(Error)
    end else
        // Create an MD pointer to tell OpenInsight to call our redirect routine instead of the SEND_INFO routine.
        StockKey    = 'SEND_INFO'
        Rec         = ''
        Rec<1>      = 'P'
        Rec<5>      = 'SYSOBJ'
        Rec<6>      = 'SEND_INFO_TO'
        Set_Status(0)
        Write_Row(TableName, StockKey, Rec, 0)
        If Get_Status(StatusCode) then
            Error   = 'Unable to setup hooking. Pointer record ' : TableName : '*' : StockKey : ' could not be written.'
            Send_Info(Error)
            Set_Status(0)
            Delete_Row(TableName, AliasKey, 0)
        end
    end

    If Error EQ '' then
        // Both MD pointers have been created.

        // Verify the path and file of where we want to direct output and create if needed.
        FileName    = File[-1, 'B\']
        Path        = File[1, Len(File) - Len(FileName)]

        // Create the path to the redirect file if it doesn't already exist.
        If RTI_OS_Directory('EXISTS', Path) EQ -1 then
            Result  = RTI_OS_Directory('CREATE', Path, True$)
            If Result EQ False$ then
                Error   = 'Could not create ' : File : '.'
                Send_Info(Error)
            end
        end

        If Error EQ '' then
            // Update the named common with the path to the redirect file.
            sitFile@    = File

            // Touch the file so we know it's working.
            OSWrite 'SEND_INFO now directed to this file.' To sitFile@

            // Load the SEND_INFO alias and reload the SEND_INFO routine so OpenInsight will call our routine instead.
            RTP27('SEND_INFO_STOCK')
            RTP27('SEND_INFO')

            // Delete the MD pointers right away so this won't impact other users. This will also allow SEND_INFO
            // to be restored in case the current session aborts and has to be restarted.
            Delete_Row(TableName, StockKey, 0)
            Delete_Row(TableName, AliasKey, 0)
        end
    end
end else
    // For the reloading of the SEND_INFO routine. Since the MD pointers are removed, this will restore the original
    // SEND_INFO.
    RTP27('SEND_INFO')
end

Return
