#!/usr/bin/perl
#
# Copies necessary eps files from a LaTeX file & the LaTeX file itself
# to /tmp, runs latex on it & then views the resultant dvi file with xdvi.
#
# Usage: $ viewdvi somefile.tex
#
# Thu May  1 06:59:05 BST 2008 (c) Frank Shute


use strict;
use warnings;
use File::Copy;
use Cwd 'chdir';
use Cwd;


# Read in tex file & copy eps files referenced therein to /tmp

while (<>) {
	if (/\]\{.*\.eps\}/) {
		my $epsfile = $&;
		substr($epsfile, 0, 2) = "";
		substr($epsfile, -1) = "";
	if ( -f $epsfile ) {
		print "Found $epsfile\n";
		print "Copying $epsfile to /tmp \n";
		copy("$epsfile", "/tmp") || 
		 die "Can't copy $epsfile to /tmp: $!\n";
		} 
	}
}

my $dir = cwd();

# Copy tex file to /tmp

if ($dir ne "/tmp") {
	copy("$ARGV", "/tmp") || die "Can't copy $ARGV to /tmp: $!\n";
	chdir "/tmp" || die "Can't change to /tmp: $!\n";
}

# Run tex file through LaTeX & view with xdvi

	print "Processing $ARGV ...\n";
	system "latex $ARGV";
	substr($ARGV, -4) = "";
	my $dvifile = $ARGV . ".dvi";
if ( -f $dvifile ){
	system "xdvi -display :0 -geometry 860x860 -s 6 $dvifile";
	print "To print: dvips /tmp/$dvifile\n";
} else {
	print "$dvifile not created\n"
	}