require "rdoc/task"
require "rake"

task default: %w[build]

desc "Build"
task :build do
  ruby "Tools/rpgscript.rb ./ ../"
end

desc "ISEQ Build"
task :build_iseq do
  ruby "Tools/iseq_generator.rb"
  pid = spawn("../lib/oneshot")
  Process.wait(pid)

  puts "Iseqing done!"
  ruby "Tools/iseqscript.rb ./"
end

desc "Debug Build"
task :build_debug do
  ruby "Tools/dbgscript.rb ./ ../"
end

desc "Test"
task test: %w[build] do
  pid = spawn(["../lib/oneshot.exe", "2>&1"])
  Process.wait(pid)
end

desc "Debug Test"
task test_debug: %w[build_debug] do
  require "rubygems"
  pid = Process.spawn(File.realpath("../lib/oneshot.exe") + " > log.txt 2>&1")
  Process.detach(pid)
  sleep 5
end

task :launch do
  pid = Process.spawn(File.realpath("../lib/oneshot.exe") + " > log.txt 2>&1")
  Process.detach(pid)
end

desc "RDOC Task"
RDoc::Task.new do |rdoc|
  rdoc.main = "README.doc"
  rdoc.rdoc_dir = "../docs"
end

"Do Nothing"
task :nothing do
end

"Make psuedocode"
task :pseudocode do
  scripts_file = File.open("_scripts.txt", "r").readlines
  psuedocode_file = File.open("../Data/pseudocode.txt", "w")

  line_nums = []
  files = []
  5.times do |i|
    next if scripts_file[i].start_with?("#") || scripts_file[i] == "" || !File.exists?(scripts_file[i].strip + ".rb")
    line_nums << i
    files << File.open(scripts_file[i].strip + ".rb", "r").read
  end

  files.each do |file|
    psuedocode_file.puts file
    psuedocode_file.puts "\n"
  end
  psuedocode_file.close
end
