#!/usr/bin/env ruby

require "thor"

class DiscourseCLI < Thor
  class_option :verbose, default: false, aliases: :v

  desc "remap", "Remap a string sequence accross all tables"
  def remap(from, to)
    load_rails

    puts "Rewriting all occurences of #{from} to #{to}"
    puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
    text = STDIN.gets
    if text.strip != "YES"
      puts "aborting."
      exit
    end

    sql = "SELECT table_name, column_name
FROM information_schema.columns
WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%')"

    cnn = ActiveRecord::Base.connection.raw_connection

    results = cnn.async_exec(sql).to_a

    results.each do |result|
      table_name = result["table_name"]
      column_name = result["column_name"]
      puts "Remapping #{table_name} #{column_name}"
      result = cnn.async_exec("UPDATE #{table_name}
                      SET #{column_name} = replace(#{column_name}, $1, $2)
                      WHERE NOT #{column_name} IS NULL
                        AND #{column_name} <> replace(#{column_name}, $1, $2)", [from, to])
      puts "#{result.cmd_tuples} rows affected!"
    end

  end

  desc "backup", "Backup a discourse forum"
  def backup(filename = nil)
    load_rails
    require "backup_restore"
    require "export/exporter"

    puts "Starting export..."
    backup = Export::Exporter.new(Discourse.system_user.id).run
    if filename.present?
      puts "Moving '#{backup}' to '#{filename}'"
      FileUtils.mv(backup, filename)
      backup = filename
    end
    puts "Export done."
    puts "Output file is in: #{backup}", ""
  end

  desc "export", "Backup a Discourse forum"
  def export
    backup
  end

  desc "restore", "Restore a Discourse backup"
  def restore(filename)
    load_rails
    require "backup_restore"
    require "import/importer"

    begin
      puts "Starting restore: #{filename}"
      Import::Importer.new(Discourse.system_user.id, filename).run
      puts 'Restore done.'
    rescue Import::FilenameMissingError
      puts '', 'The filename argument was missing.', ''
      usage
    rescue Import::ImportDisabledError
      puts '', 'Restore are not allowed.', 'An admin needs to set allow_restore to true in the site settings before restores can be run.', ''
      puts 'Restore cancelled.', ''
    end
  end

  desc "import", "Restore a Discourse backup"
  def import(filename)
    restore(filename)
  end

  desc "migrate", "Make sure all the posts are pointing to the new domain"
  option :from, type: :array, required: true, banner: "http://previous.domain.com"
  option :database, default: "default", aliases: :db
  def migrate
    verbose = options[:verbose]
    database = options[:database]
    from = options[:from].map { |f| schemaless f }

    begin
      puts "loading rails..." if verbose
      load_rails

      puts "connecting to #{database}..." if verbose
      RailsMultisite::ConnectionManagement.establish_connection(db: database)

      base_url = schemaless Discourse.base_url_no_prefix

      puts "updating #{Post.count} posts to #{base_url}" if verbose
      Post.find_each do |post|
        raw, cooked = post.raw.dup, post.cooked.dup
        from.each do |f|
          raw.gsub!(f, base_url)
          cooked.gsub!(f, base_url)
        end
        if raw != post.raw || cooked != post.cooked
          Post.where(id: post.id).update_all(raw: raw, cooked: cooked)
          putc "#" if verbose
        else
          putc "."
        end
      end

    rescue => e
      puts "Cannot connect to database: #{database}"
      puts e
      puts e.backtrace.join("\n")
    end

    puts "", "done!" if verbose
  end

  private

  def load_rails
    require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
  end

  def schemaless(url)
    url.gsub(/^https?:/, "")
  end

end

DiscourseCLI.start(ARGV)
