mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Mark Craig
06.14.2015 41a9529116425a49eaabbdab74463b0f98ca5ea8
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
 
require 'fileutils'
 
# Automate copyright update with year 2015, using 'svn status' to retrieve modified files.
# Only modified files (with 'M' mark) are updated.
#
class Copyright
 
  def updated_files
    files = []
    puts "Get modified files from svn..."
    lines = `svn st`
    puts "There are #{lines.split("\n").size} files to update."
    lines.split("\n").each { |line|
      mod, file = line.split(" ")
      if mod=="M"
        files << file
      end
    }
    files
  end
 
  def process(file)
    is_replaced = nil
    File.open(file) { |source|
      content = source.read
      is_replaced = update_copyright(content)
      if is_replaced
        File.open(file + ".copy", "w+") { |f| f.write(content) }
        #puts `head -n 27 #{file}`
        #puts("content:\n" + content.split("\n")[20..27].join("\n"))
      else
        puts "WARN : no replacement"
      end
    }
    if is_replaced
      FileUtils.mv(file + ".copy", file, :verbose => false)
    end
  end
 
 
  def update_copyright(content)
    # handle FG copyright with 1 date
    pattern = /(^\s+\*\s+)Copyright (\d+) ForgeRock,? AS/i
    mdata = content.match(pattern)
    if !mdata.nil?
      if  mdata[2]!="2015"
        replace = '\1Copyright \2-2015 ForgeRock AS'
        is_replaced = content.gsub!(pattern, replace)
      else
        is_replaced = true # no action needed
      end
    end
 
    # handle FG copyright with 2 dates
    pattern = /(^\s+\*\s+)Copyright (\d+)-(\d+) ForgeRock,? AS/i
    replace = '\1Copyright \2-2015 ForgeRock AS'
    is_replaced = content.gsub!(pattern, replace)
 
    if is_replaced.nil?
      # handle FG portions copyright with 2 dates
      pattern = /(^\s+\*\s+)Portions Copyright (\d+)-(\d+) ForgeRock,? AS/i
      replace = '\1Portions Copyright \2-2015 ForgeRock AS'
      is_replaced = content.gsub!(pattern, replace)
    end
 
    if is_replaced.nil?
      # handle FG portions copyright with 1 date
      pattern = /(^\s+\*\s+)Portions Copyright (\d+) ForgeRock,? AS/i
      mdata = content.match(pattern)
      if !mdata.nil? && mdata[2]!="2015"
        replace = '\1Portions Copyright \2-2015 ForgeRock AS'
        is_replaced = content.gsub!(pattern, replace)
      end
      if is_replaced.nil?
        # Handle no FG portions
        pattern = /(^\s+\*)(\s+)Copyright (\d+-?\d*)\s(.*)$\n\s+\*\/$/i
        replace = '\1\2Copyright \3 \4' + "\n\\1\\2Portions Copyright 2015 ForgeRock AS\n\\1/"
        is_replaced = content.gsub!(pattern, replace)
      end
    end
    is_replaced
  end
 
  def run(args)
    if args.size==0
      files = updated_files
    else
      files = args
    end
    files.each { |file|
      puts "Processing file #{file}"
      process(file)
    }
  end
 
end
 
# Launch copyright update
Copyright.new.run(ARGV)