#! /usr/bin/env ruby # # cdbiff - a mailbox flag for a CD-ROM drive # # Copyright (C) 2001 Satoru Takabayashi # All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # require 'getoptlong' def print_usage print "\ Usage: cdbiff [OPTION] -f, --file=FILE Monitor FILE [/var/mail/$USER] -c, --command=COMMAND Execute COMMAND when mail arrives [eject] -i, --interval=SECONDS Monitor at intervals of SECONDS [30] -h, --help Display this help and exit " end def parse_options options = Hash.new parser = GetoptLong.new parser.set_options(['--file', '-f', GetoptLong::REQUIRED_ARGUMENT], ['--command', '-c', GetoptLong::REQUIRED_ARGUMENT], ['--inerval', '-i', GetoptLong::REQUIRED_ARGUMENT], ['--help', '-h', GetoptLong::NO_ARGUMENT]) parser.each_option {|name, arg| options[name.sub /^--/, ""] = arg } if options['help'] print_usage exit 1 end return options end def main options = parse_options filename = "/var/mail/#{ENV['USER']}" command = "eject" interval = 30 filename = options['file'] if options['file'] command = options['command'] if options['command'] interval = options['interval'].to_i if options['interval'] last = Time.now while true begin current = File::mtime(filename) rescue next end if current > last system(command) last = current end sleep(interval) end end main