41556050601ab3d85bb681f7e98803b62c820b25
[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         prettystatus = nil
29         AIDS::RUNLEVEL_ALL.each do |r|
30                 print "\t" if prettystatus
31                 case status[r]
32                         when :start
33                                 prettystatus = 'on'
34                         when :stop
35                                 prettystatus = 'off'
36                         when :none
37                                 prettystatus = 'none'
38                         else
39                                 next
40                 end
41                         print "#{r}:#{prettystatus}"
42                 end
43         puts
44 end
45
46 def usage
47         $stderr.puts <<-EOF
48 AIDS - Assistant for Initialisation of Debian Services
49
50 Usage: #{$0} command [service]
51
52 Command may be one of:
53
54   - enable
55       Enables the service.
56
57   - disable
58       Disables the service.
59
60   - list
61       Lists the current status of the service, or of all services if
62       none is provided.
63         EOF
64         exit 1
65 end
66
67 main(ARGV) if $0 == __FILE__