namespace :air do
    
  desc "Compile"
  task :compile do
    begin
      project = Airake::Project.new_from_rake(ENV)
      fcsh = PatternPark::FCSH.new_from_rake(ENV)
      fcsh.execute([ project.base_dir, project.amxmlc.compile ])      
    rescue PatternPark::FCSHConnectError => e
      puts "Cannot connect to FCSHD (start by running: rake fcsh:start); Continuing compilation..."
      Airake::Runner.run(project.amxmlc, :compile)
    end
  end

  desc "Test"
  task :test do
    ENV["DEBUG"] = "true" unless ENV.has_key?("DEBUG")
    test_project = Airake::Project.new_from_rake(ENV, true)
    Airake::Runner.run(test_project.amxmlc, :compile)
    Airake::Runner.run(test_project.adl, :launch)
  end
  
  desc "Generate certificate"
  task :certificate do 
    pfx_file = ENV["CERTIFICATE"]
    
    if pfx_file.blank? || pfx_file == "path/to/certificate.pfx"
      print "Certificate file (to create; e.g. cert.pfx): "
      pfx_file = STDIN.gets.chomp!      
    end  
    
    if File.exist?(pfx_file)
      raise <<-EOS
        Certificate file exists at '#{pfx_file}'. If you want to generate a new certificate, please delete it first.
        You can override the setting in the Rakefile, e.g.: rake air:certificate CERTIFICATE=path.pfx
      EOS
                    
    end
    
    puts "Will generate a certificate file at: #{pfx_file}"
    
    project = Airake::Project.new_from_rake(ENV)
    optionals = {}

    print "Specify common name (e.g. SelfSign, ADigitalID): "
    cn = STDIN.gets.chomp!

    print "Key type (e.g. 1024-RSA, 2048-RSA): "
    key_type = STDIN.gets.chomp!

    print "Org unit (optional; e.g QE): "
    optionals[:ou] = STDIN.gets.chomp!

    print "Org name (optional; e.g Example - CO): "
    input = STDIN.gets.chomp!
    optionals[:o] = "\"#{input}\"" unless input.blank?

    print "Country (optional; e.g. US): "
    optionals[:c] = STDIN.gets.chomp!

    print "Password: "
    password = STDIN.gets.chomp!

    Airake::Runner.run(project.adt, :certificate, cn, pfx_file, key_type, password, optionals)
  end
  
  # Check certificate configuration for package task
  task :check_certificate do 
    pfx_file = ENV["CERTIFICATE"]
    
    if pfx_file.blank? || pfx_file == "path/to/certificate.pfx"
      raise <<-EOS
          No certificate path found (or not set). Please specify ENV[\"CERTIFICATE\"] = \"path/to/certificate.pfx\" in Rakefile. 
          If you don't have a certificate run rake air:certificate to generate one.
      EOS
    end
    
    raise "Certificate does not exist yet. Run rake air:certificate to generate one at #{pfx_file}" unless File.exist?(pfx_file)
  end

  desc "Package"
  task :package => [ :check_certificate, :compile ] do
    project = Airake::Project.new_from_rake(ENV)
    Airake::Runner.run(project.adt, :package)
  end
  
  task :set_debug do
    ENV["DEBUG"] = "true" unless ENV.has_key?("DEBUG")
  end

  desc "Launch ADL"
  task :adl => [ :set_debug, :compile ] do    
    project = Airake::Project.new_from_rake(ENV)
    Airake::Runner.run(project.adl, :launch)
  end
  
end