#!/usr/local/bin/ruby

require 'xml/libxml'

class XML::Node
  def first_child_element
    result = nil
    if self.children?
      self.each_child{|node_child| result = node_child if ((result.nil?) && node_child.element?) }
    end
    result
  end
end

#
# Method which generates crash
#
def merge(forms)
  starting_form = forms.shift
  forms.inject(starting_form) do |master_form,form| 
    master_form.find("//body").first << form.find("//body").first.first_child_element
    master_form
  end
end

#
# Run
#  Must tell it how many times to merge a document
#  On my linux box, it takes about 350 to crash it
#  On my OS X box, it takes about 150
#

unless (ARGV[0].to_i > 0)
  puts "Must specify how many documents to merge"
  exit(0)
end

count = ARGV[0].to_i 

merge_list = []

count.times do
  merge_list << XML::Parser.string(File.open("data.xml").read).parse
end

result = merge(merge_list)
puts "Merged!"

File.open("generated_form.xml", "w") do |f|
  puts "Trying to write to file" 
  f << result
  puts "After trying to write to file"
end

puts "nothing left to do but print this message"
