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