Upload 2.0.2
[physicsfs] / extras / physfs_rb / installer.rb
1 # $Id: installer.rb,v 1.3 2003/07/21 03:46:50 icculus Exp $
2
3 require 'rbconfig'
4 require 'find'
5 require 'ftools'
6
7 include Config
8
9 module Slimb
10   class Installer
11     def initialize target_dir = "", &user_skip 
12       @user_skip = user_skip or proc {|f| false}
13       
14       @version = CONFIG["MAJOR"] + "." + CONFIG["MINOR"]
15       @libdir = File.join(CONFIG["libdir"], "ruby", @version)
16       @sitedir = CONFIG["sitedir"] || File.join(@libdir, "site_ruby")
17       @dest = File.join @sitedir, target_dir
18
19       File::makedirs @dest
20       File::chmod 0755, @dest, true
21     end
22
23     def skip? file
24       @user_skip[file] or
25         file[0] == ?. or file[-1] == ?~ or file[-1] == ?#
26     end
27     
28     def install_dir dir 
29       File::makedirs(File.join(@dest, dir))
30       File::chmod(0755, File.join(@dest, dir), true)
31       Dir.foreach(dir) {|file|
32         next if skip? file
33         
34         if File.ftype(File.join(dir, file)) == "directory"
35           install_dir File.join(dir, file)
36         else
37           install_file File.join(dir, file)
38         end
39       }
40     end
41
42     def install_file file
43       if file =~ /\.so$/
44         install_so file
45       else
46         File::install file, File.join(@dest, file), 0644, true
47       end
48     end
49
50     def install_so file
51       File::install file, File.join(CONFIG["sitearchdir"], file), 0644, true
52     end
53
54     def uninstall_so file
55       file = File.join(CONFIG["sitearchdir"], file)
56       File::safe_unlink file
57     end
58
59     def install something
60       case something
61       when Array
62         something.each {|x|
63           install x if x.is_a? String
64         }
65       when String
66         if File.ftype(something) == "directory"
67           install_dir something
68         else
69           install_file something
70         end
71       end
72     end
73
74     def uninstall what = "*"
75       case what
76       when Array
77         files = what.map {|x| File.join(@dest, x)}
78       when String
79         files = Dir[File.join(@dest, what)]
80       end
81       
82       files.each {|x|
83         # FIXME: recursive uninstall is a must
84         next if FileTest.directory? x
85         File::safe_unlink x
86       }
87     end
88
89     def run files, argv
90       if !argv.grep(/--uninstall/).empty?
91         uninstall files
92       else
93         install files
94       end       
95     end
96   end
97 end
98
99 # self-installation 
100 if $0 == __FILE__
101   $stderr.puts "Installing slimb installer..."
102   Slimb::Installer.new("slimb").install File.basename(__FILE__)
103 end