#!/usr/bin/env ruby 

# ================================================================
# This is a no-longer-needed one-off script -- but it's an example of
# batch-editing for files in test/cases.
#
# Before:
# cmd: mlr --flags put '$z = $x + $y'
#
# After:
# cmd: mlr --flags put -f ${CASEDIR}/mlr
# mlr: $z = $x + $y
# ================================================================

ARGV.each do |cmd_file_name|
  mlr_file_name = cmd_file_name.sub(/\.cmd$/, '.mlr')
  cmd_file_contents = File.read(cmd_file_name)
  if cmd_file_contents =~ /.*'.*'.*/
    cmd_file_contents.match(/(.*)'(.*)'(.*)/)
    left   = $1
    middle = $2
    right  = $3
    File.write(mlr_file_name, middle.strip + "\n")
    File.write(cmd_file_name, "#{left}-f #{mlr_file_name}#{right}".strip + "\n")
    puts mlr_file_name
  else
    puts cmd_file_name
  end
end
