#!/usr/bin/env ruby

ADB = ENV['ADB'] || 'adb'

unless ::File.executable? ADB
  warn %(adb-push-ebook: `adb` not found.\nPlease set the ADB environment variable or add `adb` to your PATH.)
  exit 1
end

require 'open3'
require 'shellwords'

docname = ARGV[0] || '_output/sample-book'

targets = {
  'epub' => '/sdcard/',
  'mobi' => '/sdcard/Android/data/com.amazon.kindle/files/'
}

targets.each {|(ext, target)|
  file = %(#{docname}.#{ext})
  Open3.popen2e(Shellwords.join [ADB, 'push', file, target]) {|input, output, wait_thr|
    output.each {|line| puts line }
  } if File.file? file
}
