#!/usr/bin/ruby

$: << 'lib'
require 'rubygems'
require 'optparse'
require 'pp'
require 'apt_downloader'

class ThisApplication
  def parse_options ops = ARGV
    @option_parser = OptionParser.new do |opts|
      opts.banner = "Usage apt_downloader [ops] debian-package [more packages]"

      opts.on("-a", "--architecture ARCH", "Debian architecure to download " +
              "packages for.  Options are:" +
              "#{AptDownloader::VALID_DEBIAN_ARCHITECTURES.map{|x| "\n" +"\t"*5+"#{x}"} }") do |a|
	@architecture = a
      end
      
      opts.on("-o", "--output-dir DIR", "Copy all deb files to directory " +
              "(directory created if needed)") do |dir|
        @output_dir = dir
      end

      opts.on("-x", "--extract-dir DIR", "Extract all downloaded deb files to" +
              " the supplied directory.  Debs will be extracted without" +
              " root privelages.") do |x|
        @extract_dir = x
        @sudo_extract = false
      end

      opts.on("-X", "--sudo-extract-dir DIR", "Extract all downloaded deb files" +
              " to the supplied directory.  Debs will be extracted with" +
              " root privelages (for extracting to your target root" +
              " filesystem).  Users will be asked for sudo password.") do |x|
        @extract_dir = x
        @sudo_extract = true
      end

      # these features not yet implemented
      if nil
        opts.on("-s", "--save-dir-structure", "NOT YET IMPLEMENTED " +
                "Save the temp filesystem so that the" +
                " next call to apt_downloader can build on what you have" +
                " already downloaded and not from scratch") do |s|
          @save_fs = true
        end

        opts.on("-r", "--reuse-dir-structure", "NOT YET IMPLEMENTED " +
                "Reuse a previously saved  directory structure" +
                " (and save new downloads to it)." +
                " Structure should have been created with a previous run" +
                " of apt_downloader with the -s option") do |dir|
          @reuse_fs = dir
        end
      end

      opts.on("--", "--end",
              "Don't parse any more options") do
        OptionParser.terminate
      end
    end #end option parser block

    @packages = begin @option_parser.parse ops
                rescue OptionParser::InvalidOption => e
                  puts e
                  puts option_parser
                  exit 1
                end
  end

  def end_badly
    puts @option_parser
    exit 1
  end

  def initialize
    @output_dir = nil
    @extract_dir = nil
    @sudo_extract =false
    @architecture = nil

    parse_options
    
    unless @packages.size > 0
      end_badly
    end
   
    unless @output_dir || @extract_dir
      puts "This program is being run without saving or extracting *any* deb" +
        " files!  Cowardly refusing to run!"
      end_badly
    end

    ad = AptDownloader.new
    ad.download_dir = @output_dir if @output_dir
    ad.extract_dir = @extract_dir if @extract_dir
    ad.sudo_extract = @sudo_extract
   
    begin
      unless @architecture then
        puts "Architecture left blank, defaulting to armel"
        @architecture = 'armel'
      end
      ad.architecture = @architecture
    rescue
      puts "#{@architecture} is not a valid architecture"
      puts 'Giving up...'
      end_badly
    end

    @packages.each{  |package|
      begin
        ad.addPackage package
      rescue
        puts "#{package} was not found as a valid package!"
        puts 'Giving up...'
        end_badly
      end
    }

    ad.go
  end
end

ThisApplication.new
