##Eskil Plugin : Compare text from PDF files. (needs pdftotext)

# Example file for a plugin.
# A plugin must start exactly like this one.
# The text after : is the summary you can get at the command line

# This plugin runs input through the external tool pdftotext.
# Thus it must be run together with the -pluginallow flag.
# Anything given in -plugininfo is passed as parameters to pdftotext.

# A plugin must define this procedure to do the job.
# side: left or right
# chi:  An input channel for reading the original file.
# cho:  An output channel for writing the processed file.
proc PreProcess {side chi cho} {
    if {[info commands exec] eq ""} {
        puts $cho "PDF plugin must be run with privilege to be able\
                   to execute pdftotext"
        return 1
    }
    set cands [auto_execok pdftotext]
    lappend cands [file join $::WhoAmIFull pdftotext]
    lappend cands [file join $::WhoAmIFull .. pdftotext]
    lappend cands [file join $::WhoAmIFull .. .. pdftotext]
    set found 0
    foreach cand $cands {
        if {[file exists $cand]} {
            set found 1
            break
        }
        if {[file exists $cand.exe]} {
            set cand $cand.exe
            set found 1
            break
        }
    }

    if {!$found} {
        puts $cho "PDF plugin needs external tool 'pdftotext' to run"
        return 1
    }
    if {[catch {llength $::Info}]} {
        puts $cho "PDF plugin needs -plugininfo parameter to be a list"
        return 1
    }
    # Use source file with pdftotext since stdin is not reliable on e.g Windows
    if {[catch {exec $cand {*}$::Info $::File($side) - >&@ $cho}]} {
        puts $cho "**************************************"
        puts $cho "PDF plugin got an error from pdftotext"
    }

    # Signal that the file after processing should be used both
    # for comparison and for displaying.
    return 1
}
