aboutsummaryrefslogtreecommitdiff
path: root/bin/mauveconsole
blob: b1f9e651982b3133a4a19c4f22da0d0e25e2bed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/ruby1.8
# NAME
#  mauveconsole -- Ruby console to query the mauvealert server directly
#
# SYNOPSIS
#  mauveconsole [-h | --help] [-V | --version] [-m | --manual] [<configuration file>]
# 
# OPTIONS
#  -h, --help            Show a help message, and exit
#
#  -m, --manual          Show this manual, and exit
#
#  -V, --version         Show the version number, and exit
#
#  <configuration file>  File from whence to load the configuration. If none is
#                        specified, then mauvealert.conf in the current
#                        directory is used, and failing that
#                        /etc/mauvealert/mauvealert.conf is used.
# 
# SEE ALSO
#  irb(1), mauveserver(1), mauveclient(1)
#
# AUTHOR
#  Patrick J Cherry <patrick@bytemark.co.uk>
#

def error(msg)
  STDERR.print "*** Error: #{msg}\n"
  STDERR.print "*** For help, type: #{$0} -h\n"
  exit 1
end

help    = ARGV.any?{|a| a =~ /-(h|-help)/}
version = ARGV.any?{|a| a =~ /-(V|-version)/}
manual  = ARGV.any?{|a| a =~ /-(m|-manual)/}

# CAUTION! Kwality kode.
#
if manual or help
  # Open the file, stripping the shebang line
  lines = File.open(__FILE__){|fh| fh.readlines}[1..-1]

  found_synopsis = false

  lines.each do |line|
    line.chomp!
    break if line.empty?

    if help and !found_synopsis
      found_synopsis = (line =~ /^#\s+SYNOPSIS\s*$/) if !found_synopsis
      next
    end

    puts line[2..-1].to_s

    break if help and found_synopsis and line =~ /^#\s*$/
  end
end

require 'mauve/version'

puts "#{$0}: version "+Mauve::VERSION if version

exit 0 if help or version or manual

configuration_file = ARGV.shift
configuration_file = [".", "/etc/mauvealert/"].collect{|x| File.join(x, "mauveserver.conf") }.find{|d| File.file?(d)} if configuration_file.nil?
configuration_file = File.expand_path(configuration_file) unless configuration_file.nil?

if configuration_file.nil?
  error "No configuration file could be found\n"
end

unless File.file?(configuration_file)
  error "Configuration file #{configuration_file} not found\n"
end

require 'irb'
require 'thread'
require 'mauve/configuration_builder'
require 'mauve/configuration_builders'
require 'mauve/configuration'

Thread.abort_on_exception = true

include Mauve

begin
  Configuration.current = ConfigurationBuilder.load(configuration_file)
  Server.instance.setup
rescue StandardError => ex
  error ex.message
end

IRB.start