#!/usr/bin/env ruby

require 'fileutils'
require 'rbconfig'

# for ruby 1.9.1 and earlier
unless defined? RbConfig.ruby
  def RbConfig.ruby
    File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"])
  end
end

require File.join(File.dirname(__FILE__), "surf-calabash-helpers")

@hierarchy = File.join(FileUtils.pwd, "features")
@source_dir = File.join(File.dirname(__FILE__), '..', 'sources')


if ARGV.length == 0
  print_usage
else
  cmd = ARGV.shift

  case cmd
    when 'help'
      print_help
    when 'gen'
      gen = File.join(File.dirname(__FILE__), "surf-calabash-gen")
      require gen
      surf_gen
    when 'android_console'
      require 'calabash-android/helpers'
      require 'calabash-android/utils'
      require 'calabash-android/java_keystore'
      require 'calabash-android/env'
      require File.join(File.dirname(__FILE__), "surf-calabash-console")

      if ARGV.empty? or not is_apk_file?(ARGV.first)
        Kernel.puts "The first parameter must be the path to the apk file."
        exit 1
      else
        android_console(relative_to_full_path(ARGV.shift))
      end

    when 'ios_console'
      require File.join(File.dirname(__FILE__), "surf-calabash-console")
      ios_console
    when 'version'
      require_relative '../lib/SurfCustomCalabash/version'
      Kernel.puts SurfCustomCalabash::VERSION
    when 'update'
      require File.join(File.dirname(__FILE__), "update_files")
      if File.exists?(@hierarchy)
        show_files_for_update
      else
        Kernel.puts("В текущей папке нет файлов автотестов, перейдите в нужную папку или создайте новый проект используя команду:")
        Kernel.puts("SurfCustomCalabash gen")
      end
    else
      Kernel.puts "Invalid command '#{cmd}'"
      print_usage
  end
end