require 'erb'
require 'fileutils'
#options = {:noop => true }
#options = {}
options = {:verbose => true }
num_fname="tmp/num.txt"
template_fname="tmp/script-area.txt"
tmp_fname="tmp/tmp.txt"
to_fname="index.html"

num = File.read(num_fname).to_i
#puts num
File.write(num_fname , num + 1)

lines=File.readlines("index.html").map{|x| x.chomp!}

parts = [[],[],[]]
index = 0
first_delimitor = %r@<!-- AREA-START -->@
second_delimitor = %r@<!-- AREA-END -->@

lines.map{ |x| 
       if x =~ first_delimitor
         parts[ index ] << x
         index += 1
       elsif x =~ second_delimitor
         index += 1
         parts[ index ] << x
       else
         parts[ index ] << x
       end
}

content = File.read(template_fname)
erb = ERB.new(content)
new_content = erb.result_with_hash({num: num})
parts[1] = [new_content]

File.write( tmp_fname,  parts.flatten.join("\n") )
FileUtils.cp( tmp_fname , to_fname, options )

