#!/usr/bin/env ruby
require 'gems'

action = ARGV.shift.to_s.strip
project = ARGV.shift.to_s.strip

case action
when 'install'
  gems = Gems.new project
  gems.install
when 'uninstall'
  gems = Gems.new project
  gems.uninstall
when 'list'
  gems = Gems.new project
  gems.list
when 'import'
  gems_config = GemsConfig.new(project)
  gems_file = ARGV.shift.to_s.strip
  gems_config.import_gems(gems_file)
when 'export'
  gems_config = GemsConfig.new(project)
  gems_file = ARGV.shift.to_s.strip
  gems_config.export_gems(gems_file)
when 'projects'
  gems_config = GemsConfig.new(nil)
  gems_config.project_names.each do |project|
    puts project
  end
when 'configure'
  gems_config = GemsConfig.new(project)
  gemname = ARGV.shift
  gems_config.set_gem_options(gemname, ARGV)
when 'add'
  gems_config = GemsConfig.new(project)
  gems_file = ARGV.shift.to_s.strip
  gems_config.add_gems(gems_file)
else 'help'
  puts <<-EOS
Syntax:
  gems <action> <arguments>

Actions and arguments:
  install <name>
    Install all gems in project <name>.
  uninstall <name>
    Uninstall all gems in project <name>.
  list <name>
    List all gems in project <name>.
  import <name> <file>
    Import all gems in <file> into project <name>.
    This will overwrite the gems currently in this project.
  export <name> <file>
    Export all gems in project <name> to <file>.
    The file will be overwritten and can be parsed by the import action.
  add <name> <file>
    Add the gems <file> to project <name>.
  projects
    List all stored project names.
  configure <name> <gem> [option1 option2 option3]
    Configure a specific gem inside a project to use specific options when installing.
  EOS
end
