require 'securerandom'

module MockSetupFile
  def setup_mocked_config_files
    setup_invoker_config
    setup_osx_resolver_path
    setup_linux_resolver_path
    setup_socket_path
  end

  def remove_mocked_config_files
    restore_invoker_config
    restore_osx_resolver_setup
    restore_linux_resolver_path
    restore_socket_path
  end

  def safe_remove_file(file_location)
    File.exist?(file_location) &&
      File.delete(file_location)
  end

  def safe_make_directory(directory)
    FileUtils.mkdir(directory) unless Dir.exist?(directory)
  end

  private

  def setup_invoker_config
    Invoker::Power::Config.stubs(:config_file).returns("/tmp/.invoker/config")
    Invoker::Power::Config.stubs(:config_dir).returns("/tmp/.invoker")
    safe_make_directory("/tmp/.invoker")
    safe_remove_file(Invoker::Power::Config.config_file)
  end

  def restore_invoker_config
    safe_remove_file("/tmp/.invoker/config")
  end

  def restore_osx_resolver_setup
    safe_remove_file(Invoker::Power::OsxSetup::RESOLVER_FILE)
    FileUtils.rm_rf(Invoker::Power::OsxSetup::RESOLVER_DIR)
    Invoker::Power::OsxSetup.const_set(:RESOLVER_FILE, @old_osx_resolver)
  end

  def setup_osx_resolver_path
    @old_osx_resolver = Invoker::Power::OsxSetup::RESOLVER_FILE
    Invoker::Power::OsxSetup.const_set(:RESOLVER_FILE, "/tmp/resolver/invoker-dev")
    Invoker::Power::OsxSetup.const_set(:RESOLVER_DIR, "/tmp/resolver")

    safe_make_directory(Invoker::Power::OsxSetup::RESOLVER_DIR)
    safe_remove_file(Invoker::Power::OsxSetup::RESOLVER_FILE)
  end

  def setup_socket_path
    @old_socket_path = Invoker::IPC::Server::SOCKET_PATH
    socket_uuid = SecureRandom.urlsafe_base64
    Invoker::IPC::Server.const_set(:SOCKET_PATH, "/tmp/invoker-#{socket_uuid}")
  end

  def restore_socket_path
    Invoker::IPC::Server.const_set(:SOCKET_PATH, @old_socket_path)
  end

  def setup_linux_resolver_path
    @old_linux_resolver = Invoker::Power::Distro::Base::RESOLVER_FILE
    @old_socat_script = Invoker::Power::Distro::Base::SOCAT_SHELLSCRIPT
    @old_socat_unit = Invoker::Power::Distro::Base::SOCAT_SYSTEMD
    Invoker::Power::Distro::Base.const_set(:RESOLVER_FILE, "/tmp/dev-tld")
    Invoker::Power::Distro::Base.const_set(:SOCAT_SHELLSCRIPT, "/tmp/invoker_forwarder.sh")
    Invoker::Power::Distro::Base.const_set(:SOCAT_SYSTEMD, "/tmp/socat_invoker.service")
  end

  def restore_linux_resolver_path
    safe_remove_file(Invoker::Power::Distro::Base::RESOLVER_FILE)
    safe_remove_file(Invoker::Power::Distro::Base::SOCAT_SHELLSCRIPT)
    safe_remove_file(Invoker::Power::Distro::Base::SOCAT_SYSTEMD)
    Invoker::Power::Distro::Base.const_set(:RESOLVER_FILE, @old_linux_resolver)
    Invoker::Power::Distro::Base.const_set(:SOCAT_SHELLSCRIPT, @old_socat_script)
    Invoker::Power::Distro::Base.const_set(:SOCAT_SYSTEMD, @old_socat_unit)
  end
end
