#!/usr/bin/env ruby
require 'gli'
require 'grid_tools'

include GLI::App

program_desc 'Describe your application here'

version GridTools::VERSION

#desc 'Describe some switch here'
#switch [:s,:switch]

#desc 'Describe some flag here'
#default_value 'the default'
#arg_name 'The name of the argument'
#flag [:f,:flagname]

desc 'Start a Hub or Node'
arg_name 'Describe arguments to start here'
command :start do |c|
  #c.desc ''
  #c.switch [:h,:hub]

  c.desc 'Hub Hostname'
  c.default_value 'localhost'
  c.flag [:h, :hostname]

  c.action do |global_options,options,args|
    


    if args.count != 1
      raise "No idea what to start bro."
    end
    
    if args[0] == "hub"
      if GridTools.windows?
        binaries =  File.expand_path(File.dirname(File.expand_path(__FILE__)) + '/../binaries')
        binaries.gsub!("/", "\\")
        cmd = "start java -jar #{binaries}\\selenium-server-standalone-2.29.0.jar -role hub -browserTimeout 300"
        system cmd
        puts "Started a hub."
      else
        #if `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/).count < 3 
          binaries =  File.expand_path(File.dirname(File.expand_path(__FILE__)) + '/../binaries')
          cmd = "java -jar #{binaries}/selenium-server-standalone* -browserTimeout 300 -role hub >> ~/hub.log 2>&1 &"
          system cmd
          pid = `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/)[0]
          puts "Started a hub. [pid: #{pid}]"
        #else
        # raise "Hub or node already started,"
        #end
      end
    end 
    
    if args[0] == "node"
      if GridTools.windows?
        binaries =  File.expand_path(File.dirname(File.expand_path(__FILE__)) + '/../binaries')
        binaries.gsub!("/", "\\")
        cmd = "start java -jar #{binaries}\\selenium-server-standalone-2.29.0.jar -role wd -hub http://#{options[:h]}:4444/grid/register -trustAllSSLCertificates"
        system cmd
        puts "Started a node."
      else
        #if `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/).count < 3
          binaries =  File.expand_path(File.dirname(File.expand_path(__FILE__)) + '/../binaries')
          platform = "MAC" if GridTools.mac?
          platform = "LINUX" if GridTools.linux?
          cmd = "java -jar #{binaries}/selenium-server-standalone* -role wd -hub http://#{options[:h]}:4444/grid/register -browser \"browserName=firefox,maxInstances=5,platform=#{platform}\" -browser \"browserName=chrome,maxInstances=5,platform=#{platform}\" -trustAllSSLCertificates >> ~/node.log 2>&1 &"
          system cmd
          pid = `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/)[0]
          puts "Started a node. [pid: #{pid}]"
        #else
        #  raise "Hub or node already started."
        #end
      end
    end


  end
end

desc 'Describe stop here'
arg_name 'Describe arguments to stop here'
command :stop do |c|
  c.action do |global_options,options,args|
     
    if args.count != 1
      raise "No idea what to stop."
    end
 

    if args[0] == "hub"
     pid_array = `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/)
     if pid_array.count > 2
       `kill #{pid_array[0]}`
       puts "Killed Process #{pid_array[0]}"
     else 
       raise "Idk what to kill..."
     end
    end
    
   if args[0] == "node"
     pid_array = `ps -e -o pid,args | awk '/selenium-server/ { print $1 }'`.split(/\n/)
     if pid_array.count > 2 
       `kill #{pid_array[0]}`
       puts "Killed Process #{pid_array[0]}"
     else 
       raise "Idk what to kill..."
     end 
    end 

  end
end

pre do |global,command,options,args|
  # Pre logic here
  # Return true to proceed; false to abourt and not call the
  # chosen command
  # Use skips_pre before a command to skip this block
  # on that command only
  true
end

post do |global,command,options,args|
  # Post logic here
  # Use skips_post before a command to skip this
  # block on that command only
end

on_error do |exception|
  # Error logic here
  # return false to skip default error handling
  true
end

exit run(ARGV)