b9aa4a4984e22c97f894cb43930b0186fee749dc
[aids.git] / bin / aids
1 #!/usr/bin/ruby1.8
2
3 # Copyright (C) 2012  Steven McDonald <steven@steven-mcdonald.id.au>
4 #
5 # This program is free software. It comes without any warranty, to the
6 # extent permitted by applicable law. You can redistribute it and/or
7 # modify it under the terms of the Do What The Fuck You Want To Public
8 # License, Version 2, as published by Sam Hocevar. See the file COPYING
9 # in the root of this program's source distribution for details, or:
10 #
11 #   http://sam.zoy.org/wtfpl/COPYING
12
13 require 'lib/aids'
14
15 def main(argv)
16         svc = AIDS::Service.new(argv[1]) if argv[1]
17         case argv[0]
18                 when 'enable'
19                         usage unless svc
20                         svc.enable!
21                 when 'disable'
22                         usage unless svc
23                         svc.disable!
24                 when 'list'
25                         if svc
26                                 display_status(svc)
27                         else
28                                 AIDS.get_all_services.sort_by{|s|s.name}.each{|s|display_status(s)}
29                         end
30                 else
31                         usage
32         end
33 end
34
35 def display_status(svc)
36         print "#{svc.name[0..14]}#{' ' * (16 - svc.name[0..14].length)}"
37         status = svc.status
38         prettystatus = nil
39         AIDS::RUNLEVEL_ALL.each do |r|
40                 print "\t" if prettystatus
41                 case status[r]
42                         when :start
43                                 prettystatus = 'on'
44                         when :stop
45                                 prettystatus = 'off'
46                         when :none
47                                 prettystatus = 'none'
48                         else
49                                 next
50                 end
51                         print "#{r}:#{prettystatus}"
52                 end
53         puts
54 end
55
56 def usage
57         $stderr.puts <<-EOF
58 AIDS - Assistant for Initialisation of Debian Services
59
60 Usage: #{$0} command [service]
61
62 Command may be one of:
63
64   - enable
65       Enables the service.
66
67   - disable
68       Disables the service.
69
70   - list
71       Lists the current status of the service, or of all services if
72       none is provided.
73         EOF
74         exit 1
75 end
76
77 main(ARGV) if $0 == __FILE__