#!/usr/bin/env ruby require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'tournament')) require 'rubygems' require 'main' require 'yaml' require 'fileutils' require 'net/http' require 'uri' Main do # Loads the pool from the save file. If the save file # does not exist, creates a default pool def load_pool @pool = YAML::load_file(save_file_name) end def save_file_name params['save-file'].value end def init_pool @pool = Tournament::Pool.ncaa_2008 @pool.scoring_strategy = Tournament::ScoringStrategy.strategy_for_name(params['scoring'].value) end def save_pool if @pool File.open(save_file_name, "w") do |f| YAML::dump(@pool, f) end end end mode('install_webgui') do description 'Installs the Rails webgui.' option('web-dir', 'W') do required argument :required arity 1 description "Installation directory for the Rails application." error(:after) { puts usage.to_s } end option('site-name', 'S') do optional argument :required default 'Tournament' arity 1 description "Web site name used in tags an email subject lines." end option('relative-root', 'R') do optional argument :required arity 1 description "Relative URL root if you are installing the web application as a subdirectory in an existing website." end option('admin-email') do required argument :required arity 1 description "Email address of the admin account." error(:after) { puts usage.to_s } end option('email-server') do optional argument :required arity 1 description "SMTP email server name." end option('email-port') do cast :int optional argument :required default 25 arity 1 description "SMTP email server port." end option('email-domain') do optional argument :required arity 1 description "SMTP email server HELO domain." end option('email-user') do optional argument :required arity 1 description "SMTP email server user name." end option('email-password') do optional argument :required arity 1 description "SMTP email server user name." end option('email-auth') do optional argument :required default 'login' arity 1 validate {|ea| ['login', 'plain', 'cram_md5'].include?(ea)} description "SMTP email server authentication type." end option('use-princexml', 'X') do optional argument :required arity 1 description "Location of prince xml command line program, or directory into which the program will be installed after being downloaded from the Prince XML website." end option('tmp-dir', 't') do optional argument :required default '/tmp' arity 1 description "Temp directory location." end def run installer = Tournament::WebguiInstaller.new(params['web-dir'].value) installer.tmp_dir = params['tmp-dir'].value options = params.to_options if params['use-princexml'].given? prince_xml = params['use-princexml'].value if File.exist?(prince_xml) && File.executable?(prince_xml) && !File.directory?(prince_xml) puts "=> USING PRINCE XML EXECUTABLE #{prince_xml}" elsif File.exist?(prince_xml) && File.executable?(prince_xml) && File.directory?(prince_xml) puts "=> INSTALLING PRINCE XML INTO #{prince_xml}" installer.install_prince(prince_xml) prince_xml = File.join(prince_xml, 'bin', 'prince') else print usage.to_s exit_warn! end options['prince-path'] = prince_xml end puts "=> INSTALLING TOURNAMENT WEB GUI INTO #{installer.install_dir}" installer.install_webgui puts "=> ADJUSTING TOURNAMENT WEB GUI CONFIGURATION" installer.adjust_configuration(options) puts "=> INSTALLATION COMPLETE." puts "You should now change to #{installer.install_dir} and" puts "perform the following steps:" puts " 1. RAILS_ENV=production rake db:migrate" puts " 2. RAILS_ENV=production rake \"admin:create[admin,#{params['admin-email'].value},Joe Admin,password]\"" puts " You should substitute your desired admin user login, name and password." end end mixin :savefile do option('save-file', 's') do optional argument :required default 'pool.yml' arity 1 description "Save file for the pool." end end mode('setup') do description "Sets up the pool the first time" mixin :savefile option('scoring', 'S') do optional argument :required arity 1 default 'basic_scoring_strategy' validate {|s| Tournament::ScoringStrategy.available_strategies.include?(s)} description "Sets the scoring strategy, should be one of #{Tournament::ScoringStrategy.available_strategies.join(', ')}" end def run init_pool puts "Initialized new pool" puts "Scoring:" puts @pool.scoring_strategy.description save_pool end end mode('update') do mixin :savefile option('entry', 'e') do optional argument :required default 'tournament.yml' arity 1 description "Update the tournament entry using supplied yaml file" end def run load_pool tournament = YAML::load_file(params['entry'].value) @pool.tournament_entry = tournament save_pool end end mode('fee') do mixin :savefile argument('amount') do required cast :integer arity 1 description "The fee charged per entry." end def run load_pool @pool.entry_fee = params['amount'].value save_pool end end mode('payout') do mixin :savefile option('constant-amount', 'C') do optional arity 1 cast :boolean description "Specify if the payout is a constant amount rather than a percentage." end argument('rank') do required arity 1 description "The rank associated with the payout, such as 1, 2, 3 or last" end argument('amount') do required cast :integer arity 1 description "The amount of the payout, either a percentage or constant amount. If a constant amount, the --constant-amount option must be specified." end def run load_pool rank = params['rank'].value if 'last' == rank rank = :last else rank = rank.to_i end amount = params['amount'].value amount = -amount if params['constant-amount'].value @pool.set_payout(rank, amount) save_pool end end mode('entry') do mixin :savefile option('add', 'a') do optional argument :required arity -1 description "Add an entry yaml file to the pool" end option('remove', 'r') do optional argument :required arity -1 description "Remove an entry by name" end def run load_pool if params['add'].given? @pool.add_entry_yaml(params['add'].value) save_pool elsif params['remove'].given? @pool.remove_by_name(params['remove'].value) save_pool else puts "Please specify add or remove" print usage.to_s exit_warn! end end end mode('dump') do mixin :savefile option('entry', 'e') do optional argument :required default 'tournament.yml' description "Dump the pool entry object to a yaml file." end def run load_pool File.open(params['entry'].value, "w") do |f| YAML::dump(@pool.tournament_entry, f) end end end mode('report') do mixin :savefile available_reports = Tournament::Pool.instance_methods(false).map do |name| if idx = name.index("_report") name[0,idx] else nil end end.compact argument('type') do required arity 1 validate {|rt| available_reports.include?(rt)} description "Which report to run among #{available_reports.join(', ')}" end def run load_pool @pool.send("#{params['type'].value}_report") end end def run print usage.to_s exit_warn! end end