#!/bin/csh -f
#
#  Document viewer for Analysis and Acquisition user documents.
#  8/18/99  MCSQ
#
#
#  First check for a file name on the command line
#
if ($1 == "" || $1 == "help") then
  echo " "
  echo 'Usage:   viewdoc document_name'
  echo " "
  echo "You must specify a document_name."
  goto list
endif
#
#  Allow a file type only if it is ps or doc.
#
set fileext = $1:e
set filename = $1
if ($fileext == "doc") then
  set filename = $1:r
endif
if ($fileext == "ps") then
  set filename = $1:r
endif
#
#  If no path is specified, look for the file.doc in the current working
#  directory.  If not found, then search for either the file.ps or file.doc
#  in the directories /usr/hhirf/doc, /usr/hhirf/vme/doc and /usr/hhirf/wks/doc.
#
if ($filename:h == $filename:t) then
  if (-e $filename:h.doc) then
    set filename = $cwd/$filename:t
    cd
    goto  txxprt
  endif
  cd
  if (-e /usr/hhirf/doc/$filename:t.ps) then
    set filename = "/usr/hhirf/doc/$filename:t"
    goto psprt
  endif
  if (-e /usr/hhirf/doc/$filename:t.doc) then
    set filename = "/usr/hhirf/doc/$filename:t"
    goto txxprt
  endif
  if (-e /usr/hhirf/vme/doc/$filename:t.ps) then
    set filename = "/usr/hhirf/vme/doc/$filename:t"
    goto psprt
  endif
  if (-e /usr/hhirf/vme/doc/$filename:t.doc) then
    set filename = "/usr/hhirf/vme/doc/$filename:t"
    goto txxprt
  endif
  if (-e /usr/hhirf/wks/doc/$filename:t.ps) then
    set filename = "/usr/hhirf/wks/doc/$filename:t"
    goto psprt
  endif
  if (-e /usr/hhirf/wks/doc/$filename:t.doc) then
    set filename = "/usr/hhirf/wks/doc/$filename:t"
    goto txxprt
  endif
else
#
#  User specified a path.  Look for either a .ps or a .doc in that path.
#
  if (-e $filename.ps) then
    set filename = $filename
    goto psprt
  endif
  if (-e $filename.doc) then
    set filename = $filename
    goto txxprt
  endif
endif
echo "Can't find document - $1"
#
list:
echo "Available document files:"
echo "** Files in /usr/hhirf/doc **"
cd /usr/hhirf/doc
ls *.doc
ls *.ps
echo " "
echo "** Files in /usr/hhirf/vme/doc **"
cd /usr/hhirf/vme/doc
ls *.doc
ls *.ps
echo " "
echo "** Files in /usr/hhirf/wks/doc **"
cd /usr/hhirf/wks/doc
ls *.doc
ls *.ps
echo " "
exit
#
#  View a .ps file
#
psprt:
  echo "View document - $filename.ps"
    gv -magstep -1 $filename.ps
  exit
#
#  Format and view a .doc file
#
txxprt:
  echo "View document - $filename.doc"
  /usr/hhirf/txx   $filename "x"
  set err = $status
  if ($err != 0) then
    if ($err == 9) then
      echo 'You MUST run viewdoc from YOUR directory\!'
    endif
    exit
  endif
  /usr/hhirf/txxps $filename:t
  set err = $status
  if ($err != 0) then
    exit
  endif
    gv -magstep -1 $filename:t.ps
  rm $filename:t.ps
  rm $filename:t.txx
exit
