#line 293 "/home/travis/build/felix-lang/felix/src/packages/filetools.fdoc"
  include "std/felix/flx_cp";
  
  fun dbg(s:string):string={ println s; return s; }
  //println$ System::args ();
  //println$ "argc=" + str System::argc;
  
  var dir = "";
  var regex = "";
  var target = "";
  var search = "";
  var replace = "";
  var live = true;
  var verbose = false;
  
  for var i in 1 upto System::argc do
    var arg = System::argv i;
    if arg == "--test" do live = false;
    elif arg == "-v" or arg == "--verbose" do verbose = true;
    elif arg.[0] == char "-" do
      println$ "Unknown option '" + arg+"'";
      System::exit(1);
    elif dir == "" do dir = arg;
    elif regex == "" do regex = arg;
    elif target == "" do target = arg;
    elif search == "" do search = arg;
    elif replace == "" do replace = arg;
    done
  done
  
  if dir == "" do println$ "Missing directory name (arg1)"; System::exit(1);
  elif regex == "" do println$ "Missing regex (arg2)"; System::exit(1);
  elif target == "" do println$ "Missing target (arg3)"; System::exit(1);
  elif search == "" do println$ "Missing search regex (arg4)"; System::exit(1);
  elif replace == "" do println$ "Missing replace spec (arg5)"; System::exit(1);
  done
  
  if verbose do println$ "#Dir='" + dir + "', pattern='"+regex+"', dst='"+target+"'"; done
  
  var searchre = RE2 search;
  gen sandr (src: string, dst:string) =
  {
    var text = load src;
    var result = search_and_replace (text, 0uz, searchre, replace);
    save (dst, result);
    return true;
  }
  
  var filere = Re2::RE2 regex;
  CopyFiles::processfiles sandr (dir, filere, target, live, verbose);
  System::exit(0);