summaryrefslogtreecommitdiff
path: root/tool/rbuninstall.rb
blob: f682279c4549550c5d49389d063ed56fb546cdb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/ruby -nl
BEGIN {
  $dryrun = false
  until ARGV.empty?
    case ARGV[0]
    when /\A--destdir=(.*)/
      $destdir = $1
    when /\A-n\z/
      $dryrun = true
    else
      break
    end
    ARGV.shift
  end
  $dirs = []
  $files = []
}
list = (/\/\z/ =~ $_ ? $dirs : $files)
$_ = File.join($destdir, $_) if $destdir
list << $_
END {
  status = true
  if $dryrun
    $files.each do |file|
      puts "rm #{file}"
    end
    $dirs.reverse_each do |dir|
      puts "rmdir #{dir}"
    end
  else
    $files.each do |file|
      begin
        File.unlink(file)
      rescue Errno::ENOENT
      rescue
        status = false
        puts $!
      end
    end
    $dirs.reverse_each do |dir|
      begin
        begin
          Dir.rmdir(dir)
        rescue Errno::ENOTDIR
          raise unless File.symlink?(dir)
          File.unlink(dir)
        end
      rescue Errno::ENOENT, Errno::ENOTEMPTY
      rescue
        status = false
        puts $!
      end
    end
  end
  exit(status)
}