(* Run as `ocaml gen.ml` *)

let count = 1000
let () =
  Out_channel.with_open_text "dune" (fun dunech ->
    let dprintfn fmt = Format.kasprintf (Printf.fprintf dunech "%s\n") fmt in
    dprintfn "; This file was autogenerated\n";
    dprintfn "(cram (deps ../../src/Driver.exe ../../runtime/Std.i))\n";
  for i=0 to count / 10 do
    let cram_buf = Buffer.create 100 in
    let cram_printfn fmt = Format.kasprintf (Printf.bprintf cram_buf "%s\n") fmt in
    cram_printfn "; This file was auto generated\n";
    let cram_file = Printf.sprintf "generated%04dx.t" i in
    let deps = ref [] in
    for j=0 to 9 do
      let k = (i*10+j) in
      let lama_file = Printf.sprintf "generated%05d.lama" k in
      let input_file = Printf.sprintf "generated%05d.input" k in

      if Sys.file_exists lama_file then (
        deps := lama_file :: input_file :: !deps;
        cram_printfn "  $ ../../src/Driver.exe -runtime ../../runtime -I ../../runtime -I ../../stdlib/x64 -i generated%05d.lama < generated%05d.input" k k
        )
    done;
    match !deps with
    | [] -> ()
    | xs ->
       (dprintfn "(cram (applies_to generated%04dx)" i;
        dprintfn "  (deps %s))" (String.concat " " (List.rev xs));
        Out_channel.with_open_text cram_file (fun ch ->
          output_string ch (Buffer.contents cram_buf))
        )
  done
  )
