#!/usr/bin/env ruby
# -*- ruby -*-

require 'rbconfig'
require 'getoptlong'
require 'find'
require 'ftools'

# ====================================================================
class Installer

  def makedirs (*args)
    File.makedirs (*args)
  end
  def install (from, to, mode=nil, verbose=false)
    File.install(from, to, mode, verbose)
  end
end

# ====================================================================
class FakeInstaller

  def makedirs (*args)
    args.each {|dir| puts "Create directory #{dir}"}
  end
  def install (from, to, mode=nil, verbose=false)
    print "Copy #{from} to #{to}"
    printf(" (mode=%04o)", mode) if mode
    print "\n"
  end
end

# ====================================================================
class InstallationProgram
  
  def initialize
    @executables = []
    @libraries   = []

    @bindir = Config::CONFIG['bindir']
    @sitedir = Config::CONFIG['sitedir']

    @quiet = false
    @installer = Installer.new
  end

  def add_executables (pgms)
    @executables |= pgms
  end

  def add_libraries (libs)
    @libraries |= libs
  end

  def handle_options
    parser = GetoptLong.new *[
      ['--prefix',       GetoptLong::REQUIRED_ARGUMENT],
      ['--bindir',       GetoptLong::REQUIRED_ARGUMENT],
      ['--sitedir',      GetoptLong::REQUIRED_ARGUMENT],
      ['--help',   '-h', GetoptLong::NO_ARGUMENT],
      ['--test',   '-t', GetoptLong::NO_ARGUMENT],
      ['--quiet',  '-q', GetoptLong::NO_ARGUMENT]
    ]
    
    parser.each { |k,v|
      case k
      when '--prefix'
	@bindir = "#{v}/bin"
	@sitedir = "#{v}/lib/ruby/site_ruby"
      when '--bindir'
	@bindir = v
      when '--sitedir'
	@sitedir = v
      when '--test'
	puts "Test Install ... "
	@installer = FakeInstaller.new
      when '--quiet'
	@quiet = true
      when '--help'
	usage
	exit
      end
    }
  end

  def find_files
    @libfiles = []
    @libraries.each { |lib|
      Find.find(lib) { |f|
	next if f !~ /\.rb$/
	next if f =~ /\/(test|mock)/
	@libfiles << f
      }
    }
  end

  def install_files
    @installer.makedirs (@bindir)
    @libraries.each { |lib|
      libpath = File.join(@sitedir,lib)
      @installer.makedirs(libpath)
    }
    @executables.each { |exe|
      @installer.install(exe, File.join(@bindir,exe), 0755, !@quiet)
    }
    @libfiles.each { |fn|
      @installer.install(fn, File.join(@sitedir,fn), 0644, !@quiet)
    }
  end

  def install
    handle_options
    find_files
    install_files
  end

  def usage
    puts <<EOS
Usage: install.rb options

Valid Options are ...
  --prefix=PREFIX    Common prefix for bin and site directories
                     It has the same effect and doing both 
                        --bindir=PREFIX/bin
                        --sitedir=PREFIX/lib/ruby/site_ruby
  --bindir=BINDIR    Directory to install main executable.
                        (default=#{@bindir})
  --sitedir=SITEDIR  Directory to install the library files.
                        (default=#{@sitedir})
  --help,  -h        Display this help message.
  --test,  -t        Do not install, just print installation actions.
  --quiet, -q        Do not display installation information.
EOS
  end

end


# Main ---------------------------------------------------------------

if __FILE__ == $0 then
  ins = InstallationProgram.new
  ins.add_executables %w(gemfinder)
  ins.add_libraries %w(gem)
  ins.install
end
