# concatcsv.csv
# Concatenate all the CSV files that match the argument.
# E.g.:
# $ ruby concatcsv.rb port_optode
# ...getting data from ./plan1/data-09-optodes/port_optode.csv => big-port_optode.csv
# ...getting data from ./plan2/data-09-optodes/port_optode.csv => big-port_optode.csv
#        :
# ...getting data from ./plan9/data-09-optodes/port_optode.csv => big-port_optode.csv
# ...Done!
#

def usage()
  puts("Usage:")
  puts("  ruby concatcsv.rb match_name")
  exit(1)
end

if (ARGV.size < 1)
  usage()
end

plan_name = "long-term"
file_search = "./**/*#{ARGV[0]}*.csv"
puts("...Searching for #{file_search}")

list = Dir[file_search]

puts("...Found #{list.size} matches")
if (list.size <=0 )
  exit(0)
end

bfile = "big-#{ARGV[0]}.csv"
big = File.open(bfile,"w")
header = false
nfiles = 0

# Copy the lines from each file
#
list.each do |csv|

  # Skip plan folders that are not part of the real deployment.
  # Plans that do not match the partial plan_name are skipped.
  #
  next unless (csv.index(plan_name))

  # Announce the optode file we're processing
  #
  nfiles += 1
  puts("...getting data from #{csv} => #{bfile}")
  file = File.open(csv)

  # Copy each data line. Get only one header line.
  #
  while (l = file.gets())
    # Header lines contain "Rover Timestamp"
    #
    if (l.index("Timestamp"))
      big.puts(l) unless (header)
      header = true
    else
      big.puts(l)
    end
  end
end

puts("...Done! Processed #{nfiles} files.")
