#!/usr/bin/env ruby
require 'yaml'
require 'fileutils'
include FileUtils

def prompt_for(question, suggestion, required, validation = "")
  newanswer = "X"

  if required
    while  not File.exists? newanswer + validation
      print "JDK path (required) (#{suggestion}): "
      newanswer = gets.chomp!
      newanswer = suggestion if newanswer == ""
      puts "Invalid JDK path" unless File.exists? newanswer  + validation
    end
  else
    while newanswer != "" and not File.exists? newanswer + validation
      print "#{question} (blank to skip) (#{suggestion}): "
      newanswer = gets.chomp!
      puts "Invalid #{question}" if newanswer != "" and not File.exists? newanswer + validation
    end
  end

  suggestion = newanswer unless newanswer == ""

  File.expand_path(suggestion) unless suggestion == ""
end

rhobuildyml = File.expand_path(File.dirname(__FILE__) + "/../") + "/rhobuild.yml"
puts "We will ask you a few questions below about your dev environment.\n\n"

#first try and find java
java = ""
if ENV['JAVA_HOME']
  java = ENV['JAVA_HOME']
elsif File.exists? "/Library/Java/Home"
  java = "/Library/Java/Home"
elsif File.exists? "/Program Files/Java"
  start = pwd
  chdir  "/Program Files/Java"
  Dir.glob("**/jdk1.6*") { |f| java = File.expand_path(f) }
  chdir start
end

java = java.gsub(/\\/,"/")
java = "" unless File.exists? java  + "/lib/dt.jar"

java = prompt_for("JDK Path", java, true, "/lib/dt.jar")

java = java + "/bin"

#now try and find android
android = ""
if ENV['ANDROID_HOME']
  android = ENV['ANDROID_HOME']
end

android = prompt_for("Android SDK path", android, false)

# try to detect android ndk
ndk = ""
unless android.nil?
  ndks = Dir.glob(File.dirname(android) + "/android-ndk-*").sort
  ndk = ndks.last if ndks.size > 0
end
androidndk = prompt_for("Android NDK path", ndk, false)

#now cabwiz
cabwiz = ""
cabwiz = "/Program Files/Windows Mobile 6 SDK/Tools/CabWiz" if File.exists? "/Program Files/Windows Mobile 6 SDK/Tools/CabWiz"
cabwiz = "C:/Program Files/Windows Mobile 6 SDK/Tools/CabWiz" if File.exists? "C:/Program Files/Windows Mobile 6 SDK/Tools/CabWiz"

cabwiz = prompt_for("Windows Mobile 6 SDK CabWiz", cabwiz, false)

#now blackberry

jde46 = ""
jde46 = File.expand_path("/Program Files/Research In Motion/BlackBerry JDE 4.6.0") if File.exists? "/Program Files/Research In Motion/BlackBerry JDE 4.6.0"
jde46 = File.expand_path("C:/Program Files/Research In Motion/BlackBerry JDE 4.6.0") if File.exists? "C:/Program Files/Research In Motion/BlackBerry JDE 4.6.0"

jde46 = prompt_for("BlackBerry JDE 4.6", jde46, false)

jde46mds = ""
jde46mds = File.expand_path("/Program Files/Research In Motion/BlackBerry JDE 4.6.0/MDS") if File.exists? "/Program Files/Research In Motion/BlackBerry JDE 4.6.0/MDS"
jde46mds = File.expand_path("C:/Program Files/Research In Motion/BlackBerry JDE 4.6.0/MDS") if File.exists? "C:/Program Files/Research In Motion/BlackBerry JDE 4.6.0/MDS"

jde46mds = prompt_for("BlackBerry JDE 4.6 MDS", jde46mds, false)

jde42 = ""
jde42 = File.expand_path("/Program Files/Research In Motion/BlackBerry JDE 4.2.0") if File.exists? "/Program Files/Research In Motion/BlackBerry JDE 4.2.0"
jde42 = File.expand_path("/Program Files/Research In Motion/BlackBerry JDE 4.2.0") if File.exists? "C:/Program Files/Research In Motion/BlackBerry JDE 4.2.0"

jde42 = prompt_for("BlackBerry JDE 4.2", jde42, false)

jde42mds = ""
jde42mds = File.expand_path("/Program Files/Research In Motion/BlackBerry Email and MDS Services Simulators 4.1.2/MDS") if File.exists? "/Program Files/Research In Motion/BlackBerry Email and MDS Services Simulators 4.1.2/MDS"
jde42mds = File.expand_path("C:/Program Files/Research In Motion/BlackBerry Email and MDS Services Simulators 4.1.2/MDS") if File.exists? "C:/Program Files/Research In Motion/BlackBerry Email and MDS Services Simulators 4.1.2/MDS"

jde42mds = prompt_for("BlackBerry JDE 4.2 MDS", jde42mds, false)

puts "\nIf you want to build with other BlackBerry SDK versions edit: #{rhobuildyml}\n\n"


if File.exists? rhobuildyml
  config = YAML::load_file(rhobuildyml)
else
  config = YAML::load_file(File.expand_path(File.dirname(__FILE__) + "/../") + "/rhobuild.yml.example")
end

config["env"]["paths"]["java"] = java
config["env"]["paths"]["android"] = android
config["env"]["paths"]["android-ndk"] = androidndk
config["env"]["paths"]["cabwiz"] = cabwiz
config["env"]["paths"][4.2] = {} if config["env"]["paths"][4.2].nil?
config["env"]["paths"][4.6] = {} if config["env"]["paths"][4.6].nil?
config["env"]["paths"][4.6]["jde"] = jde46
config["env"]["paths"][4.6]["mds"] = jde46mds
config["env"]["paths"][4.2]["jde"] = jde42
config["env"]["paths"][4.2]["mds"] = jde42mds

  File.open(  rhobuildyml, 'w' ) do |out|
    YAML.dump( config, out )
  end
