# This is pretty naive -- in that it assumes that the title
# that is passed to it has the form "title: actual words of the title"
# or consists only of the actual title.
# The actual title is then searched and the Postscript form of the
# document passed to the system Postscript viewer.
global CURR_PAGE_NUM
global CURR_PAGE_NAME
global PAGE_IMAGE_LIST
set CURR_PAGE_NUM 0
set CURR_PAGE_NAME ""


proc viewdoc {title} {

    global PSCOMMAND
    global PSPARAMS

    set title2 [join $title]
    set temp $title2
    regexp (title:\ )(.*\$) $temp junk1 junk2 title2
    set query [format "retrieve (DOC_TEXT.data, DOC_TEXT.format) \
       where TECH_REPORT_BIB.title = \"%s\"::text \
       and TECH_REPORT_BIB.oid = DOC_TEXT.reference" $title2]
    global DOCFILES
    catch {unset DOCFILES}
  puts stdout $query
    WaitWindow "Retrieving Document Info"
    PQexec $query
    PQtuplearray blank DOCFILES

    set ntuples $DOCFILES(TUPLES.0)

    if {$ntuples==0} {
        WaitWindow {No documents retrieved
  click here to remove window }
        normalcursor
        return
    }
    set firstpage 1

    foreach i [lsort [array names DOCFILES]] {
               if {[string match "\[A-Z\]*" $i ] == 1} break
               # use LOCopy (in lolist.tcl) to move the inversion file
               # to /tmp
		set formattype [lindex "$DOCFILES($i)" 1]
                set docname [lindex "$DOCFILES($i)" 0]
		if {[string match "TIFF" $formattype]} {
puts stdout $docname
		  lappend pagelist $docname
                  if {$firstpage} {
		     WaitWindow "Retrieving first page image of $ntuples pages"
		     set firstpage 0
		  }

		} else { # it's a PostScript, Text or Markup file
puts stdout "testing for PS"
   		  if {[string match "PS" $formattype]} {
puts stdout $docname
                     WaitWindow "Retrieving PostScript File"
                     set tval [LOCopy [concat $docname]]
                     exec $PSCOMMAND  $tval &
                  }
   		  if {[string match "TEXT" $formattype]} {
                     WaitWindow "Text File Also Available"
                    # set tval [LOCopy [concat $docname]]
                    # LObrowse "Text Document Browse" $tval
                  }
		  
                }
        }
    if {$firstpage == 0} {
       viewpages [lsort $pagelist] $title2
    }
    normalcursor
}


proc viewpages {imagelist title} {
  global CURR_PAGE_NUM
  global CURR_PAGE_NAME
  global PAGE_IMAGE_LIST
  global BIGFONT

  set CURR_PAGE_NUM 1
  set CURR_PAGE_NAME [lindex $imagelist 0]

  catch {destroy .docim}

  toplevel .docim -relief {raised}
  wm geometry .docim +150+100
  wm minsize .docim 10 10
  wm title .docim "Scanned Page Browser"

  set npages [llength $imagelist]

  set PAGE_IMAGE_LIST $imagelist

  set messag "View Page Number for \n  \"$title\""

  message .docim.message -aspect {900}  \
    -font $BIGFONT \
    -justify {center}  -padx {0}  -pady {10}  -relief {raised} \
    -text "$messag"

  frame .docim.buttons -borderwidth {2}  -geometry {20x20}  -relief {raised}

  button .docim.buttons.cancel -text {Cancel Browse} -command {destroy .docim}
  button .docim.buttons.show -text {Show Page} \
      -command {showpageimage [.docim.scales.page get]}
  button .docim.buttons.next -text {Next Page} -command {nextpageimage}
  button .docim.buttons.prev -text {Previous Page} -command {prevpageimage}

  pack append .docim.buttons \
              .docim.buttons.show {left expand fillx} \
              .docim.buttons.next {left expand fillx} \
              .docim.buttons.prev {left expand fillx} \
              .docim.buttons.cancel {left expand fillx}


frame .docim.scales -borderwidth {2}  -relief {raised}

scale .docim.scales.page -label {Page Number}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from 1 -to $npages

pack append .docim.scales \
              .docim.scales.page {top expand fillx}

pack append .docim \
	      .docim.message {top frame center expand fillx} \
              .docim.scales {top frame center expand fillx} \
              .docim.buttons {top frame center expand fillx}


.docim.scales.page set 1

showpageimage 1

}


proc showpageimage {pagenum} {
  global PAGE_IMAGE_LIST
  global CURR_PAGE_NUM
  global CURR_PAGE_NAME

  set CURR_PAGE_NUM $pagenum
  set CURR_PAGE_NAME [lindex $PAGE_IMAGE_LIST [expr $pagenum-1]]
  set ntuples [llength $PAGE_IMAGE_LIST]
  WaitWindow "Retrieving page image $CURR_PAGE_NUM of $ntuples pages"
  set tval [LOCopy [concat $CURR_PAGE_NAME]]
  if {[file exists $tval.Z] == 0} { 
puts stdout "Getting $tval page image"
  exec mv $tval $tval.Z
#  WaitWindow "Uncompressing page image $CURR_PAGE_NUM of $ntuples pages"
#  exec uncompress $tval.Z
#  exec mv $tval $tval.unc
  }
  WaitWindow "Loading page image $CURR_PAGE_NUM of $ntuples pages into viewer"
  exec xv $tval.Z &

}

proc nextpageimage {} {
  global CURR_PAGE_NUM

  set tmpnum [.docim.scales.page get]

  if {$tmpnum <= 1} {
    set CURR_PAGE_NUM 1
  } else {
    incr CURR_PAGE_NUM 1
puts stdout "current page is $CURR_PAGE_NUM"
  }
  .docim.scales.page set $CURR_PAGE_NUM
  showpageimage $CURR_PAGE_NUM
}

proc prevpageimage {} {
  global CURR_PAGE_NUM

  set tmpnum [.docim.scales.page get]

  if {$tmpnum <= 1} {
    set CURR_PAGE_NUM 1
  } else {
    incr CURR_PAGE_NUM -1
  }
puts stdout "current page is $CURR_PAGE_NUM"
  .docim.scales.page set $CURR_PAGE_NUM
  showpageimage $CURR_PAGE_NUM
}


