#!/usr/bin/env ruby
ROOT = File.expand_path(File.dirname(__FILE__).gsub(/bin$/,''))
require 'fileutils'
require File.join(ROOT, "lib", "generate.rb")
require File.join(ROOT, "lib", "classic.rb")
require File.join(ROOT, "lib", "create.rb")
require File.join(ROOT, "lib", "run.rb")
require File.join(ROOT, "lib", "update.rb")
require File.join(ROOT, "lib", "test.rb")

# ---------------------------------------------------------- #
#                                                            #
#                   command line interface                   #
#                                                            #
# ---------------------------------------------------------- #

# droidgap gen [app name]
Generate.new(ARGV[1]) if ARGV.first == 'gen'

# droidgap classic (for windows users mostly)
Classic.new(ARGV[1..-1]) if ARGV.first == 'classic'

# droidgap create [path to phonegap project]
Create.new(ARGV[1]) if ARGV.first == 'create'

# droidgap run [optional directory]
Run.new if ARGV.first == 'run'

# droidgap update [params]
Update.new if ARGV.first == 'update'

# droidgap log
if ARGV.first == 'log'
  $stdout.sync = true
  IO.popen('adb logcat') do |f|
    until f.eof?
      puts f.gets
    end
  end
end

# droidgap test
Test.new if ARGV.first == 'test'

# TODO implement these!
puts "droidgap ship not implemented" if ARGV.first == 'ship'

if ARGV.first.nil? || ARGV.first == 'help'
  help = <<-EOF
  
  DroidGap: PhoneGap/Android Dev Script
  -------------------------------------
  
  Useful utilities for devlopers building mobile apps using PhoneGap for Android.
  
  Usage:
  
  droidgap <command> <parameters>
  
  Commands:
  
  help ...... See this message. Type help [command name] to see specific help topics.
  gen ....... Generate the example PhoneGap application to current directory (or optionally provide an output directory as parameter).
  create .... Creates an Android compatible project from a WWW folder. 
  classic ... Backwards support for droidgap script. Run "droidgap help classic" for more info.
  update .... Copy a fresh phonegap.jar and phonegap.js into a valid PhoneGap/Android project.
  ship ...... Build and sign an APK suitable for submission to an Android Marketplace.
    
  Quickstart:
  
  $ droidgap gen exampleapp 
  $ cd exampleapp
  $ ant debug install && adb logcat
  
  EOF
  
  gen = <<-EOF
  
  DroidGap Generate
  -----------------
  
  Generate the example PhoneGap application to path supplied or current working directory if none is supplied.
  
  Usage:
  
    droidgap gen [path]
    
  NOTE: Do *not* run "droidgap gen example" - you will end up with a recursive directory problem.
  
  EOF
  
  run = <<-EOF
  
  DroidGap Run
  ------------
  
  Launches PhoneGap project to first device found and attaches a logger that listens for console.log statements.
  
  Usage:

    droidgap run <path>
    
  EOF
  
  ship = <<-EOF
  
  DroidGap Ship
  -------------
  
  Build and sign an APK suitable for submission to an Android Marketplace.
  
  Usage:
  
    droidgap ship <path>
  
  EOF
  
  log = <<-EOF
  
  DroidGap Log
  -------------
  
  Launches LogCat
  
  Usage:
  
    droidgap log
  
  EOF
  
  create = <<-EOF
  
  DroidGap Create
  ----------------
  
  Creates an Android compatable project from a PhoneGap project. For example, if you have MyProject with index.html this command will create MyProject_android.
  
  Usage:
  
    droidgap create <path>
  
  EOF
  
  update = <<-EOF

  DroidGap Update
  ~~~~~~~~~~~~~~~
  
  Builds the JS and PhoneGap Android jar file and copies them to your project.
  
  EOF
  
  classic = <<-EOF

  DroidGap Classic
  ~~~~~~~~~~~~-~~~
  
  Compatability for older droidgap scripts. 
  
  Usage:
  
  droidgap classic [android_sdk_path] [name] [package_name] [www] [path]

  android_sdk_path ... The path to your Android SDK install.
  name ............... The name of your application.
  package_name ....... The name of your package (For example: com.nitobi.demo)
  www ................ The path to your www folder. (Wherein your HTML, CSS and JS app is.)
  path ............... The path to generate the application.
  
  EOF
  
  puts ARGV[1].nil? ? help : eval(ARGV[1])
end
