#!/usr/bin/ruby
def rewrite(fname, ihtml)
  fspec = fname + "-*.tar.gz"
  freg = Regexp.new("(" + fname + "-)(.*)(\\.tar\\.gz)$")
  sreg = Regexp.new("(" + fname + "-)\\d+(\\.\\w+)*")
  aname = Dir[fspec].sort[-1]

  (freg =~ aname)
  dummy, stem, ver, ext = $~
  
  ihtmlbak = ihtml + ".bak"
  system("cp #{ihtml} #{ihtmlbak}")
  
  newlines = []
  open(ihtmlbak, "r") do |s|
    while line = s.gets
      newlines.push line.gsub(sreg) {
	if $& =~ (ext + "$")
	  stem + ver + ext
	else
	  stem + ver
	end
      }
    end
  end
  open(ihtml, "w") do |t|
    newlines.each do |line|
      t.print line
    end
  end
  system("tar xfvz #{aname}")
  current_tar_gz = fname + "-current.tar.gz"
  system("ln -fs #{aname} #{current_tar_gz}")
end

fname = ARGV[0] || "algebra"
ihtml = "index.html"
system("chmod a+r *")
rewrite(fname, ihtml)
      
