ftp -o - https://jcs.org/move_in | sh -
1#!/usr/bin/env ruby
2# vim:ft=ruby
3#
4# forks and executes the requested program with the temporary file given,
5# allowing mutt to return to its message view while you view the attachment in
6# the program
7#
8# usage in ~/.mailcap:
9# application/pdf; ~/bin/mutt_bgrun xpdf %s
10#
11
12require "fileutils"
13require "open3"
14
15viewer = ARGV[0]
16orig_file = ARGV[1]
17
18if !orig_file
19 STDERR.puts "usage: #{$0} <viewing application> <filename>"
20 exit 1
21end
22
23tmp_dir = `mktemp -d -t`.chomp
24tmp_file = tmp_dir + "/" + File.basename(orig_file).gsub(/[^A-Za-z_\.-]/, "")
25
26system("cp", orig_file, tmp_file)
27
28Kernel.fork do
29 launch_file = tmp_file.dup
30 if viewer == "firefox"
31 launch_file = "file:///#{launch_file}"
32 end
33
34 i, _, _, w = Open3.popen3(viewer, launch_file)
35 i.close
36
37 begin
38 Process.waitpid(w[:pid])
39 rescue Errno::ECHILD
40 # process ended really quickly, most likely just handed it off to another
41 # process, so wait a bit for it to process the tmp file before removing it
42 sleep 3
43 end
44
45 File.unlink tmp_file
46 Dir.unlink tmp_dir
47end