Remove debug code from Plugin.
[mailshears.git] / bin / mailshears
1 #!/usr/bin/ruby -wU
2 #
3 # mailshears, to prune unused mail directories (and more).
4 #
5
6 # Load all of our lib/ code.
7 require 'mailshears'
8
9 # Define a usage string using the program name.
10 program_name = File.basename($PROGRAM_NAME)
11
12 # Defaults
13 mode_name = 'prune'
14 mode = :prune
15
16 # If a mode was supplied, it should be in ARGV[0].
17 if ARGV.length() > 0
18 mode_names = ['prune', 'rm', 'mv']
19 if mode_names.include?(ARGV.first().downcase()) then
20 # Peel the mode name off the head of the list.
21 mode_name = ARGV.shift()
22 end
23 end
24
25 # Determine the mode from its name.
26 if mode_name == 'rm' then
27 mode = :rm
28 elsif mode_name == 'mv' then
29 mode = :mv
30 end
31
32 # Since we removed the mode name (if it existed) from ARGV, what
33 # remains should be the required arguments. Figure out if we have the
34 # wrong number of arguments, and store the associated error message in
35 # args_error_message if necessary.
36 args_error_message = nil
37
38 if mode == :prune and ARGV.length() != 0 then
39 args_error_message = "ERROR: prune mode takes no additional arguments."
40 elsif mode == :rm and ARGV.length() < 1 then
41 args_error_message = "ERROR: rm mode takes two or more user arguments."
42 elsif mode == :mv and ARGV.length() != 2 then
43 args_error_message = "ERROR: mv mode takes exactly two user arguments."
44 end
45
46 # If we got the wrong number of arguments, we'll have an error message
47 # here. Report it and exit with a failure code.
48 if not args_error_message.nil? then
49 STDERR.puts args_error_message
50 puts "Usage: #{UserInterface.usage(program_name)}"
51 Kernel.exit(ExitCodes::BAD_COMMAND_LINE)
52 end
53
54
55 # Load each of the plugins that we'll need.
56 cfg = Configuration.new()
57
58 cfg.plugins.each do |plugin_file|
59 require "#{mode_name}/plugins/#{plugin_file}"
60 end
61
62 # And the runners.
63 require "#{mode_name}/#{mode_name}_runner"
64 require "#{mode_name}/#{mode_name}_dummy_runner"
65
66 # Now we figure out which plugin module to use based on our mode.
67 plugin_module = nil
68
69 if mode == :rm then
70 plugin_module = RmPlugin
71 elsif mode == :mv then
72 plugin_module = MvPlugin
73 else
74 # Safe, catch-all default
75 plugin_module = PrunePlugin
76 end
77
78 # Parse the remaining arguments as User/Domain objects. If we get some
79 # other argument that isn't one of those, it's an error.
80 parsed_args = []
81
82 ARGV.each do |arg|
83 begin
84 u = User.new(arg)
85 parsed_args << u
86 rescue InvalidUserError
87 begin
88 d = Domain.new(arg)
89 parsed_args << d
90 rescue InvalidDomainError
91 STDERR.puts "ERROR: invalid user/domain argument #{arg}"
92 Kernel.exit(ExitCodes::BAD_COMMAND_LINE)
93 end
94 end
95 end
96
97
98 # Buffer the output so that we can avoid printing the informational
99 # header when no plugins produce output.
100 require 'stringio'
101 output_buffer = StringIO.new()
102 $stdout = output_buffer
103
104 begin
105 plugin_module.run(cfg, *parsed_args)
106 ensure
107 # Now restore stdout, and print the header plus whatever the plugins
108 # produced (if they produced anything). If they didn't produce any
109 # output, then we avoid printing the header.
110 #
111 # This gets wrapped in an "ensure" block because otherwise, if
112 # plugin_module.run() crashes, the traceback will get stored in
113 # output_buffer and never get printed.
114 $stdout = STDOUT
115
116 if output_buffer.size > 0 then
117 puts UserInterface.make_header(program_name, plugin_module.to_s())
118 puts output_buffer.string()
119 end
120 end
121
122 # If we made it here without crashing, well that sounds pretty
123 # successful to me.
124 Kernel.exit(ExitCodes::SUCCESS)