Multiple external filters

I'm trying to pipe my output through multiple external filters according
to the following logic:

1. If no output file is specified, write to $stdout
2. If the outfile extension is .lout, write lout output
3. If the config file has a path to lout, write ps output (default)
4. If the outfile extension is .pdf, write pdf output

I'm trying to retain the option to dump to stdout to facilitate piping
(in fact, to be totally consistent I should probably have a ps2pdf
config option too and use it if found), leading to the following,
somewhat ugly code:

if CONFIG['lout'].empty? || outfilename =~ /\.lout$/
  outfile.puts(out)
else
  tf = Tempfile.new("scrabpp")
  tf.puts(out)
  tf.close
  psfile = `#{CONFIG['lout']} #{tf.path}`
  if outfilename =~ /\.pdf$/
    tf = Tempfile.new("scrabpp")
    tf.puts(psfile)
    tf.close
    outfile.close
    system("ps2pdf #{tf.path} #{outfilename}")
  else
    outfile.puts(psfile)
  end
end

Any more elegant way of doing it, especially without the nested ifs?

martin