# File vpp, line 395
def pdf
  f = ARGV.shift
  case f
  when nil
    fhin = STDIN
    using = "standard input"
  when /\.(pdf|ps)$/ # extension given - use f
    File.exist?(f) or quit("File #{f} does not exist!")
    fhin = File.new(f)
    using = f
  else                               # no extension given:
    if File.exist?("#{f}.pdf")       # try .pdf
      using = "#{f}.pdf"
      fhin = File.new(using)
    elsif File.exist?("#{f}.ps")     # try .ps
      using = "#{f}.ps"
      fhin = File.new(using)
    else                             # use f if f.pdf and f.ps aren't there
      File.exist?(f) or quit("Found no file #{f}, #{f}.pdf or #{f}.ps!")
      fhin = File.new(f)
      using = f
    end
  end
  warn "Using #{using}" if @verbose
  fhin.eof and quit("Empty input file")
  fhin.binmode

  # is it PDF or PostScript?
  first = fhin.read(4)
  ispdf = case first
  when /%!/   
    false
  when /%PDF/ 
    true
  else
    quit("Input is neither PDF nor PostScript")
  end
  #
  # continue running in temporary directory
  Dir.mkdir(TMP)
  warn "Running in temporary directory #{TMP}" if @verbose
  Dir.chdir(TMP)

  # make a pdf copy in the temporary directory:
  open(ispdf ? 'main.pdf' : '|ps2pdf - main.pdf','wb') do |fout|
    fout.write(first)
    while s = fhin.read(512)
      fout.write(s)
    end
  end
  return File.join(Dir.getwd,'main.pdf')
end