]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - bin/rbot
rbot: don't fail in git checkout if git can't be run
[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 Tom Gilbert.
40 #
41 # Permission is hereby granted, free of charge, to any person obtaining a copy
42 # of this software and associated documentation files (the "Software"), to
43 # deal in the Software without restriction, including without limitation the
44 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
45 # sell copies of the Software, and to permit persons to whom the Software is
46 # furnished to do so, subject to the following conditions:
47 #
48 # The above copyright notice and this permission notice shall be included in
49 # all copies of the Software and its documentation and acknowledgment shall be
50 # given in the documentation and software packages that this Software was
51 # used.
52 #
53 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
56 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
57 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
58 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59
60 $VERBOSE=true
61
62 require 'etc'
63 require 'getoptlong'
64 require 'fileutils'
65
66 $version="0.9.11-git"
67 $opts = Hash.new
68
69 if $version =~ /git/
70   if defined?(SCM_DIR) and File.exists?(File.join(SCM_DIR, '.git'))
71     begin
72       git_out = `git status`
73       git_out.match(/^# On branch (.*)\n/)
74       if $1 # git 1.5.x
75         branch = $1.dup || "unknown"
76         changed = git_out.match(/^# Change(.*)\n/)
77         rev = "revision "
78         git_out = `git log -1 --pretty=format:"%h%n%b"`.split("\n")
79         rev << git_out.first
80         if git_out.last.match(/^git-svn-id: \S+@(\d+)/)
81           rev << "(svn #{$1})"
82         end
83         rev << ", local changes" if changed
84       else # older gits
85         git_out = `git branch`
86         git_out.match(/^\* (.*)\n/)
87         branch = $1.dup rescue "unknown"
88         rev = "revision " + `git rev-parse HEAD`[0,6]
89       end
90     rescue => e
91       puts e.inspect
92       branch = "unknown branch"
93       rev = "unknown revision"
94     end
95
96     $version << " (#{branch} branch, #{rev})"
97   else
98     up = File.dirname(__FILE__) + "/.."
99     rev = " (unknown revision)"
100     begin
101       svn_out = `svn info #{up}`
102       if svn_out =~ /Last Changed Rev: (\d+)/
103         rev = " (revision #{$1}"
104       end
105       svn_st = `svn st #{up}`
106       if svn_st =~ /^[MDA] /
107         rev << ", local changes"
108       end
109       rev << ")"
110     rescue => e
111       puts e.inspect
112     end
113     $version += rev
114   end
115 end
116
117 orig_opts = ARGV.dup
118
119 opts = GetoptLong.new(
120   ["--background", "-b", GetoptLong::NO_ARGUMENT],
121   ["--debug", "-d", GetoptLong::NO_ARGUMENT],
122   ["--help",  "-h", GetoptLong::NO_ARGUMENT],
123   ["--loglevel",  "-l", GetoptLong::REQUIRED_ARGUMENT],
124   ["--trace",  "-t", GetoptLong::REQUIRED_ARGUMENT],
125   ["--pidfile", "-p", GetoptLong::REQUIRED_ARGUMENT],
126   ["--version", "-v", GetoptLong::NO_ARGUMENT]
127 )
128
129 $debug = $DEBUG
130 $daemonize = false
131
132 opts.each {|opt, arg|
133   $debug = true if(opt == "--debug")
134   $daemonize = true if(opt == "--background")
135   $opts[opt.sub(/^-+/, "")] = arg
136 }
137
138 $cl_loglevel = $opts["loglevel"].to_i if $opts["loglevel"]
139
140 if ($opts["trace"])
141   set_trace_func proc { |event, file, line, id, binding, classname|
142     if classname.to_s == $opts["trace"]
143       printf "TRACE: %8s %s:%-2d %10s %8s\n", event, File.basename(file), line, id, classname
144     end
145   }
146 end
147
148 defaultlib = File.expand_path(File.dirname($0) + '/../lib')
149
150 if File.directory? "#{defaultlib}/rbot"
151   unless $:.include? defaultlib
152     $:.unshift defaultlib
153   end
154 end
155   
156 begin
157   require 'rbot/ircbot'
158 rescue LoadError => e
159   puts "Error: couldn't find the rbot/ircbot module (or one of its dependencies)\n"
160   puts e
161   exit 2
162 end
163
164 if ($opts["version"])
165   puts "rbot #{$version}"
166   exit 0
167 end
168
169 if ($opts["help"])
170   puts "usage: rbot [options] [config directory]"
171   puts "  -h, --help         this message"
172   puts "  -v, --version      version information"
173   puts "  -d, --debug        enable debug messages"
174   puts "  -l, --loglevel     sets the log level verbosity"
175   puts "  -b, --background   background (daemonize) the bot"
176   puts "  -p, --pidfile      write the bot pid to file"
177   puts "config directory defaults to ~/.rbot"
178   exit 0
179 end
180
181 if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
182   # just run the bot
183   bot.mainloop
184 end
185