#!/usr/bin/ruby

require 'pp'
require 'fileutils'
include FileUtils
require 'erb'
require_relative '../unix/utils.rb'
import '../help_build.rake'

start_time = Time.now
binaries = {:lang => 'lang/Release/yabasic.exe', :setup => 'setup/Release/setup.exe', :wrapper => 'wrapper/Release/wrapper.exe' }

desc 'Check git'
task :git do 
  sh "/usr/bin/git diff --exit-code --quiet" do |ok,res|
    if !ok
      sh "git status"
      warn "Uncommited local changes, please fix with 'git add' etc."
    end
  end
end

desc 'Build yabasic'
task :build => :git do
  system('find . -name \\*.obj -exec rm {} \\;')
  binaries.each do |key,bin|
    vcxproj = {:lang => 'yabasic', :setup => 'setup', :wrapper => 'wrapper' }
    rm bin if File.exist?(bin)
    warnings = errors = -1
    cmd = "\"/mnt/c/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe\" c:/work/yabasic/windows/#{key.to_s}/#{vcxproj[key]}.vcxproj /p:Configuration=Release"
    puts cmd
    IO.popen(cmd) do |f|
      f.each_line do |l|
        l.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
        puts l
        mdata = l.match(/^\s+(\d+) Warnung\(en\)\s+$/)
        warnings = mdata[1].to_i if mdata
        mdata = l.match(/^\s+(\d+) Fehler\s+$/)
        errors = mdata[1].to_i if mdata
      end
      puts "\n\nFound #{warnings} Warnings and #{errors} Errors.\n"
      fail "Did not find number of warnings" if warnings == -1
      fail "Found #{warnings} warnings" if warnings > 0
      fail "Did not find number of errors" if errors == -1
      fail "Found #{errors} errors" if errors > 0
    end
    fail "File '#{bin}' has not been build" if !File.exist?(bin) || File.mtime(bin) < start_time
  end
end

version = nil
desc 'Collect Versions'
task :versions => :build do
  File.open('version.yab','w') { |f| f.puts 'open 1,"../yabasic.version","w":print #1 peek$("version");:close 1:exit' }
  sh './lang/Release/yabasic.exe version.yab'
  version=File.new('../yabasic.version').read.chomp.strip
  puts "Version = " + version
end

desc 'Run tests for windows'
task :tests => :versions do
  cd 'lang' do
    run_tests './tests', '../Release/yabasic.exe'
    run_tests '../../unix/lang/tests/common', '../../../../windows/lang/Release/yabasic.exe'
  end
end

desc 'Pack binaries'
task :pack => :tests do
#  fail "\n\n*** This should be run as admin user ! ***\n\n" unless %x{id -G}.match(/\s(114|544)\s/)
  cp '../unix/lang/LICENSE', '/mnt/c/Windows/Temp/LICENSE.txt'
  File.open('yabasic-setup.SED','w') do |file|
    file.write ERB.new(File.read('resources/yabasic-setup.SED.erb')).result(binding)
  end
  File.open('resources/yabver.txt','w') { |f| f.puts version }
  setup_exe = "/mnt/c/work/yabasic/products/yabasic-#{version}-setup.exe"
  rm setup_exe if File.exist?(setup_exe)
  sh 'powershell.exe -File C:/work/yabasic/windows/run_iexpress.ps1'
  while !File.exist?(setup_exe)
    puts "Waiting for #{setup_exe} to appear"
    sleep 2
  end
end

desc 'Create zip-file, e.g. for Windows XP'
task :zip => :pack do
  zipdir = 'yabasic-zip'
  yabdir = '/mnt/c/work/yabasic/windows'
  zipfile = "../products/yabasic-#{version}.zip"
  rm_r zipdir if File.exist?(zipdir)
  rm_r zipfile if File.exist?(zipfile)
  mkdir zipdir
  [ "#{yabdir}/lang/Release/yabasic.exe",
    "#{yabdir}/setup/Release/setup.exe",
    "#{yabdir}/resources/README.txt",
    "#{yabdir}/resources/yabver.txt",
    "#{yabdir}/resources/yabico.ico",
    "#{yabdir}/../products/yabasic.htm",
    "#{yabdir}/../unix/lang/demo.yab",
    "/mnt/c/Windows/Temp/LICENSE.txt" ].each {|file|
    cp file, zipdir
  }
  sh "zip #{zipfile} #{zipdir}/*"
  rm_r zipdir if File.exist?(zipdir)
end

desc 'Cleanup and Show products'
task :default => :zip do
  rm Dir.glob('../products/web/download/*')
  cleanup "../products/yabasic-*.zip"
  cleanup "../products/yabasic-*-setup.exe"
  sh 'ls -l ../products ' + binaries.values.join(" ")
end
