#! /usr/bin/ruby

require "ftools"
while 1 do
	if ! test ?f,"/tmp/imx.check"
		app_path = ""
		if ! test ?f,"/tmp/imx.pid"
			Process.exit!(0)
		end
		begin
			File.open("/tmp/imx.pid") do |f|
				lines = f.readlines
				pid = lines[0].to_i
				if pid == 0
					system "rm /tmp/imx.pid"
					Process.exit!(0)
				end
				system "kill -9 #{pid}"
				# p "killing #{pid}"
				app_path = lines[1]
			end
		rescue Exception => e
			$stderr.puts e.backtrace
		ensure
			# p "command = open -a #{ app_path.to_s }"
			system "open -a #{ app_path.to_s } &"
			Process.exit!(0)
		end
	else 
		system "rm /tmp/imx.check"
	end
	sleep 10
end