#!/usr/bin/ruby
# encoding: UTF-8

def execute(*args)
  cmd = args.map(&:inspect) * ' '
  puts "Executing: #{cmd}"
  IO.popen(cmd, 'r') do |io|
    io.each { |l| puts l }
  end
end

argv = ARGV.dup
filename = argv.shift or fail "require a filename as first argument"
argv.empty? and argv << 'true'
argv.map! { |a| a == '%f' ? filename : a }
warn "Observing #{filename.inspect} for changes now and execute #{argv.inspect}."
old_mtime = nil
loop do
  begin
    mtime = File.mtime(filename)
    if old_mtime.nil? || mtime > old_mtime
      execute(*argv)
    else
      sleep 0.1
    end
  rescue Interrupt
    exit 1
  rescue Errno::ENOENT
  ensure
    old_mtime = mtime
  end
end