# previewing  of the file $main_file with the previewer $preview   
#
# Copyright (C) 2004  G. Lamprecht, W. Lotz, R. Weibezahn; IWD, Bremen University

proc preview {f austext} {

  upvar 1 $austext a
  global vv preview prback proptions main_file prsuffix prpreopt dirprefix tlaOutLstAct
  global sub pid UnLock

  set a ""; if {$tlaOutLstAct==0} {writescr0 $f ""}


  if {$dirprefix==""} {set mfile $main_file} else {set mfile [fileName $main_file]} 

  if {$mfile==""} {set a "${a}***** $vv(ea1)\n"; mybell 2; return}

  writescr $f "[datime] $vv(pr1)  $preview $proptions $mfile$prsuffix $prback\n"


  if {$prsuffix==".ps"} {# postscript preview: recreate postscript file 

    if {[file exists "$mfile.ps"]} { # postscript file exists

      if {[file exists "$mfile.dvi"]} {
        if {[file mtime "$mfile.dvi"]>[file mtime "$mfile.ps"]} {
          writescr $f "$vv(pr2) dvips $mfile.dvi $prpreopt -o $mfile.ps\n"
	  set UnLock 0; mkCmd_wait $f dvips "$mfile.dvi $prpreopt -o $mfile.ps"; set UnLock 1
        }
      } 

    } else {# postscript file not found

      if {[file exists "$mfile.dvi"]} {# .dvi file exists in current directory

        writescr $f "$vv(pr3) dvips $mfile.dvi $prpreopt -o $mfile.ps\n"
	set UnLock 0; mkCmd_wait $f dvips "$mfile.dvi $prpreopt -o $mfile.ps"; set UnLock 1
 
      } elseif {[file exists "$main_file.dvi"]} {# .dvi file exists qualified in other direct.

        writescr $f "$vv(pr3) dvips $main_file.dvi $prpreopt -o $mfile.ps\n"
	set UnLock 0; mkCmd_wait $f dvips "$main_file.dvi $prpreopt -o $mfile.ps";set UnLock 1
 
      } else {# .dvi file not found either

        writescr $f "\n***** $vv(pr4) $mfile.ps $vv(pr5) $mfile.dvi)!\n$vv(pr6) $vv(pr9)\n"
        mybell 2; return

      }
    }

  }


  if {[file exists "$mfile$prsuffix"]} {
 
    set a "${a}[datime] $preview $vv(pr7)\n"

    if {$prback==""} {
      mkCmd_wait $f $preview "$proptions $mfile$prsuffix"
      set pid ""
    } else {
############################################################################## alt
      #eval set r [catch "exec $preview $proptions $mfile$prsuffix $prback" m]
      #set pid "$m";  set a "${a}$pid\n";  if {$r!=0} {set a "${a}$vv(pr8)=$r\n"}
############################################################################## alt
############################################################################## neu 21.3.00
	set ret [mkCmd_back $f $preview $proptions $mfile$prsuffix]
	set a "${a}$pid\n";  if {$ret!=""} {set a "${a}$vv(pr8)=$ret\n"}
############################################################################## neu 21.3.00

    }

  } else {

    set a "${a}\n***** $vv(aus5) $mfile$prsuffix $vv(ea4)\n$vv(pr6)"
    if	   {$prsuffix==".pdf"} {set a "${a}\n $vv(pr10) $prsuffix $vv(pr11)\n"} \
    elseif {($prsuffix!=".dvi")&&($prsuffix!=".ps")} {set a "${a}\n $vv(pr10) $prsuffix\n"}
    set a "${a} $vv(pr9)\n"
    mybell 2; return

  }

}
