#!/usr/local/bin/ruby
$LIBNAME = "finite-set"
$LIBDIR = "../../lib"
$LIBS = ["finite-set", "finite-map"]
$ARCHDIR = "../../.."

def detab(line, tabwidth = 8)
  line = line.dup
  while line.sub!(/\t/) { " " * (tabwidth - $~.begin(0) % tabwidth) }
  end
  line
end

def read_lib(lib)
  lib = lib + ".rb"
  file = $LIBDIR + "/" + lib
  sw = false
  sw_ver = true
  ver = 0.0
  lines = []

  File.foreach(file) do |line|
    line.chop!
    line = detab(line)
    if sw_ver && line =~ /#\s*Version\s+([\d.]+)/
      ver = $1.to_f
      sw_ver = false
    end

#    line.sub!(/(Version:?\s+)[\d.]+/, "Version: #{$VERSION}")
#    line.sub!(/20\d\d\.\d\d?\.\d\d?/, Time.now.strftime("%Y.%m.%d"))
    if /^module Algebra\b/ =~ line
      sw = true
      next
    end
    if /^end\b/ =~ line && sw
      sw = false
      next
    end
    if sw
      line.sub!(/^  /, '')
    end
    lines.push line
  end
  [ver, lines]
end

def read_doc(file)
  lines = []
  File.foreach(file) do |line|
    line.chop!
    next if /include Algebra/ =~ line
    line.gsub!(/Algebra::/, '')
    line.sub!(/(Version:?\s+)[\d.]+/, "Version: #{$VERSION}")
    line.sub!(/20\d\d\.\d\d?\.\d\d?/, Time.now.strftime("%Y.%m.%d"))
    line.sub!(/require "rubyunit".*/) {
      "require \"runit/testcase\"\nrequire \"runit/cui/testrunner\""
    }
    line.sub!(/^Tests\((\w+)\)/, 'RUNIT::CUI::TestRunner.run(\1.suite)')
    line.sub!(/Runit/, 'RUNIT::TestCase')
    lines << line
  end
  lines
end

def write_file(dir, filename, lines)
  dirs = dir.split(/\//) #/

  (0...dirs.size).each do |i|
    d = dirs[0..i].join("/")
    unless File.exist?(d)
      Dir.mkdir(d)
    end
  end

  if filename =~ /\.html$/
    file = "|../add-stylesheet.rb > " + dir + "/" + filename
  else
    file = dir + "/" + filename
  end

  open(file, "w") do |f|
    lines.each do |line|
      f.puts line
    end
  end
end

ver0, lines0 = read_lib $LIBS[0]
ver1, lines1 = read_lib $LIBS[1]
$VERSION = sprintf("%.2f", (ver0+ver1)/2)

$ARCHNAME =$LIBNAME + "-" + $VERSION.to_s
$TAZNAME = $ARCHNAME + ".tar.gz"
$ARCHROOT = $ARCHDIR + "/" + $ARCHNAME

write_file($ARCHROOT + "/lib", $LIBS[0] + ".rb", lines0)
write_file($ARCHROOT + "/lib", $LIBS[1] + ".rb", lines1)

system("cd doc; rd2 index.rd > index.html")
system("cd doc-ja; rd2 index-ja.rd > index-ja.html")

[
  [".", "doc", "index.rd"],
  [".", "doc", "index.html"],
  ["../..", "doc", "finite-set.rd"],
  ["../..", "doc", "finite-map.rd"],
#  ["../..", "doc", "finite-set.txt"],
#  ["../..", "doc", "finite-map.txt"],
  ["../..", "doc", "finite-set.html"],
  ["../..", "doc", "finite-map.html"],
#########################################
  [".", "doc-ja", "index-ja.rd"],
  [".", "doc-ja", "index-ja.html"],
  ["../..", "doc-ja", "finite-set-ja.rd"],
  ["../..", "doc-ja", "finite-map-ja.rd"],
#  ["../..", "doc-ja", "finite-set-ja.txt"],
#  ["../..", "doc-ja", "finite-map-ja.txt"],
  ["../..", "doc-ja", "finite-set-ja.html"],
  ["../..", "doc-ja", "finite-map-ja.html"],
  ["../..", "tests", "test-finite-set.rb"],
  ["../..", "tests", "test-finite-map.rb"],
].each do |sdir, dir, file|
  lines = read_doc("#{sdir}/#{dir}/#{file}")
  write_file($ARCHROOT + "/#{dir}", file, lines)
end

system("cp ../../doc/style.css #$ARCHROOT/doc")
system("cp ../../lib/powers.rb #$ARCHROOT/lib")
system("cd #$ARCHDIR; tar cfvz #$TAZNAME #$ARCHNAME")

