Flesh out the status handling a bit more, actually check all multi-user runlevels
[aids.git] / bin / aids
1 #!/usr/bin/ruby1.8
2
3 require 'lib/aids'
4
5 def main(argv)
6         svc = AIDS::Service.new(argv[1]) if argv[1]
7         case argv[0]
8                 when 'enable'
9                         usage unless svc
10                         svc.enable!
11                 when 'disable'
12                         usage unless svc
13                         svc.disable!
14                 when 'list'
15                         if svc
16                                 display_status(svc)
17                         else
18                                 AIDS.get_all_services.sort_by{|s|s.name}.each{|s|display_status(s)}
19                         end
20                 else
21                         usage
22         end
23 end
24
25 def display_status(svc)
26         print "#{svc.name[0..14]}:#{' ' * (16 - svc.name[0..14].length)}"
27         status = svc.status
28         AIDS::RUNLEVEL_ALL.each do |r|
29                 case status[r]
30                         when :start
31                                 prettystatus = 'on'
32                         when :stop
33                                 prettystatus = 'off'
34                         when :none
35                                 prettystatus = 'none'
36                         else
37                                 next
38                 end
39                         print "#{r}: #{prettystatus + ' ' * (8 - prettystatus.length)}"
40                 end
41         puts
42 end
43
44 def usage
45         $stderr.puts <<-EOF
46 AIDS - Assistant for Initialisation of Debian Services
47
48 Usage: #{$0} command [service]
49
50 Command may be one of:
51
52   - enable
53       Enables the service.
54
55   - disable
56       Disables the service.
57
58   - list
59       Lists the current status of the service, or of all services if
60       none is provided.
61         EOF
62         exit 1
63 end
64
65 main(ARGV) if $0 == __FILE__