#!/usr/bin/env ruby

require 'rubygems'
require 'rails-installer'

    class Sqlite < RailsInstaller::Database
      # The name of the sqlite database file
      def self.db_file(installer)
        File.join(installer.install_directory,'db','database.sqlite')
      end
      
      def self.yml(installer)
        %q{
        login: &login
          adapter: sqlite3
          database: db/database.sqlite

        development:
          <<: *login

        production:
          <<: *login

        test:
          database: ":memory"
          <<: *login
        }        
      end
    end

class TypoInstaller < RailsInstaller
  application_name 'typo'
  support_location 'the Typo mailing list'
  
  def pre_migrate_database
    old_schema_version = get_schema_version
    new_schema_version = File.read(File.join(source_directory,'db','schema_version')).to_i
    
    return unless old_schema_version > 0
     
    # Are we downgrading?
    if old_schema_version > new_schema_version
      message "Downgrading schema from #{old_schema_version} to #{new_schema_version}"
      
      in_directory install_directory do
        unless system("rake -s db:migrate VERSION=#{new_schema_version}")
          raise InstallFailed, "Downgrade migrating from #{old_schema_version} to #{new_schema_version} failed."
        end
      end
    end
  end

  # Migrate the database
  def migrate
    message "Migrating #{@@app_name.capitalize}'s database to newest release"
    
    in_directory install_directory do
      unless system("rake -s db:migrate")
        raise InstallFailed, "Migration failed"
      end
    end
  end
  
  
  def install_post_hook
    sweep_cache
  end

  # Sweep the cache
  def sweep_cache
    in_directory install_directory do
      message "Cleaning out #{@@app_name.capitalize}'s cache"
      status = system_silently("rake -s sweep_cache")
    end
  end
end

class SweepCache < RailsInstaller::Command
  help "Sweep Typo's cache"
  
  def self.command(installer, *args)
    installer.sweep_cache
  end
end

# Installer program
directory = ARGV[1]

typo = TypoInstaller.new(directory)
typo.message_proc = Proc.new do |msg|
  STDERR.puts " #{msg}"
end
typo.execute_command(*ARGV)
