]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - bin/rbot
Licensing uniformity: dual-license rbot core under MIT+acknowledgement and GPLv2
[user/henk/code/ruby/rbot.git] / bin / rbot
1 #!/usr/bin/env ruby
2
3 =begin rdoc
4
5 = rbot main executable
6
7 Usage:
8
9   % rbot [options] [config directory]
10
11 == Options
12
13 [-h, --help]
14     display a help message and exit
15 [-v, --version]
16     display version information and exit
17 [-d, --debug]
18     enable debug messages
19 [-l, --loglevel _level_]
20     sets the minimum log level verbosity
21 [-b, --background]
22     background (daemonize) the bot
23 [-p, --pidfile _filename_]
24     write the bot pid to _filename_
25
26 The default config directory is <tt>~/.rbot</tt>.
27
28 The default pidfile is <tt><i>botdir</i>/rbot.pid</tt>.
29
30 The logfile is located at <tt><i>botdir</i>/<i>botname</i>.log</tt>, and
31 the default loglevel is 1 (INFO messages). Possible values for the loglevel
32 are 0 (DEBUG), 1 (INFO), 2 (WARN), 3 (ERROR), 4 (FATAL).
33
34 Please note that the logfile doesn't contain IRC logs (which are located at
35 <tt><i>botdir</i>/logs/*</tt>, but only rbot diagnostic messages.
36
37 =end
38
39 # Copyright (C) 2002-2006 Tom Gilbert.
40 # Copyright (C) 2007-2008 Giuseppe Bilotta and the rbot development team
41 #
42 # This is free software, see COPYING for licensing details
43
44 $VERBOSE=true
45
46 require 'etc'
47 require 'getoptlong'
48 require 'fileutils'
49
50 $version="0.9.11-git"
51 $opts = Hash.new
52
53 if $version =~ /git/
54   if defined?(SCM_DIR) and File.exists?(File.join(SCM_DIR, '.git'))
55     begin
56       git_out = `git status`
57       git_out.match(/^# On branch (.*)\n/)
58       if $1 # git 1.5.x
59         branch = $1.dup || "unknown"
60         changed = git_out.match(/^# Change(.*)\n/)
61         rev = "revision "
62         git_out = `git log -1 --pretty=format:"%h%n%b%n%ct"`.split("\n")
63         rev << git_out.first
64         $version_timestamp = git_out.last.to_i
65         if git_out[1].match(/^git-svn-id: \S+@(\d+)/)
66           rev << "(svn #{$1})"
67         end
68         rev << ", local changes" if changed
69       else # older gits
70         git_out = `git branch`
71         git_out.match(/^\* (.*)\n/)
72         branch = $1.dup rescue "unknown"
73         rev = "revision " + `git rev-parse HEAD`[0,6]
74       end
75     rescue => e
76       puts e.inspect
77       branch = "unknown branch"
78       rev = "unknown revision"
79     end
80
81     $version << " (#{branch} branch, #{rev})"
82   else
83     up = File.dirname(__FILE__) + "/.."
84     rev = " (unknown revision)"
85     begin
86       svn_out = `svn info #{up}`
87       if svn_out =~ /Last Changed Rev: (\d+)/
88         rev = " (revision #{$1}"
89       end
90       svn_st = `svn st #{up}`
91       if svn_st =~ /^[MDA] /
92         rev << ", local changes"
93       end
94       rev << ")"
95     rescue => e
96       puts e.inspect
97     end
98     $version += rev
99   end
100 end
101
102 orig_opts = ARGV.dup
103
104 opts = GetoptLong.new(
105   ["--background", "-b", GetoptLong::NO_ARGUMENT],
106   ["--debug", "-d", GetoptLong::NO_ARGUMENT],
107   ["--help",  "-h", GetoptLong::NO_ARGUMENT],
108   ["--loglevel",  "-l", GetoptLong::REQUIRED_ARGUMENT],
109   ["--trace",  "-t", GetoptLong::REQUIRED_ARGUMENT],
110   ["--pidfile", "-p", GetoptLong::REQUIRED_ARGUMENT],
111   ["--version", "-v", GetoptLong::NO_ARGUMENT]
112 )
113
114 $debug = $DEBUG
115 $daemonize = false
116
117 opts.each {|opt, arg|
118   $debug = true if(opt == "--debug")
119   $daemonize = true if(opt == "--background")
120   $opts[opt.sub(/^-+/, "")] = arg
121 }
122
123 $cl_loglevel = $opts["loglevel"].to_i if $opts["loglevel"]
124
125 if ($opts["trace"])
126   set_trace_func proc { |event, file, line, id, binding, classname|
127     if classname.to_s == $opts["trace"]
128       printf "TRACE: %8s %s:%-2d %10s %8s\n", event, File.basename(file), line, id, classname
129     end
130   }
131 end
132
133 defaultlib = File.expand_path(File.dirname($0) + '/../lib')
134
135 if File.directory? "#{defaultlib}/rbot"
136   unless $:.include? defaultlib
137     $:.unshift defaultlib
138   end
139 end
140   
141 begin
142   require 'rbot/ircbot'
143 rescue LoadError => e
144   puts "Error: couldn't find the rbot/ircbot module (or one of its dependencies)\n"
145   puts e
146   exit 2
147 end
148
149 if ($opts["version"])
150   puts "rbot #{$version}"
151   exit 0
152 end
153
154 if ($opts["help"])
155   puts "usage: rbot [options] [config directory]"
156   puts "  -h, --help         this message"
157   puts "  -v, --version      version information"
158   puts "  -d, --debug        enable debug messages"
159   puts "  -l, --loglevel     sets the log level verbosity"
160   puts "  -b, --background   background (daemonize) the bot"
161   puts "  -p, --pidfile      write the bot pid to file"
162   puts "config directory defaults to ~/.rbot"
163   exit 0
164 end
165
166 if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
167   # just run the bot
168   bot.mainloop
169 end
170