<commit_before>#!/usr/bin/env ruby

require 'os'
require 'inifile'
require 'yaml'
require 'fileutils'

if OS.linux?
  profiles_dir = File.expand_path('~/.zotero/zotero/')
else
  puts OS.report
  exit 1
end

class IniFile
  def write_compact( opts = {} )
    filename = opts.fetch(:filename, @filename)
    encoding = opts.fetch(:encoding, @encoding)
    mode = encoding ? "w:#{encoding}" : "w"

    File.open(filename, mode) do |f|
      @ini.each do |section,hash|
        f.puts "[#{section}]"
        hash.each {|param,val| f.puts "#{param}#{@param}#{escape_value val}" unless val.nil?}
        f.puts
      end
    end
    self
  end
end

profiles = IniFile.load(File.join(profiles_dir, 'profiles.ini'))

profiles['General']['StartWithLastProfile'] = 0

profile_name = 'BBTZ5TEST'
profile_dir = File.expand_path("~/.#{profile_name}.profile")
data_dir = File.expand_path("~/.#{profile_name}.data")

profile = nil
profiles.each{|section, param, val|
  profile = section if param == 'Name' && val ==  profile_name
}
if profile.nil?
  free = 0
  while true do
    profile = "Profile#{free}"
    break unless profiles.has_section?(profile)
    free += 1
  end

  profiles[profile]['Name'] = profile_name
end
profiles[profile]['IsRelative'] = 0
profiles[profile]['Path'] = profile_dir
profiles[profile]['Default'] = nil

profiles.each_section do |section|
  puts section
  puts profiles[section].to_yaml
end

profiles.write_compact

FileUtils.rm_rf(profile_dir)
FileUtils.rm_rf(data_dir)

FileUtils.cp_r(File.join(File.dirname(__FILE__), '../../test/fixtures/profile/profile'), profile_dir)
FileUtils.cp_r(File.join(File.dirname(__FILE__), '../../test/fixtures/profile/data'), data_dir)

File.open(File.join(profile_dir, 'prefs.js'), 'w'){|prefs|
  File.readlines(File.join(File.dirname(__FILE__), '../../test/fixtures/profile/profile/prefs.js')).each{|pref|
    if pref.start_with?('user_pref("extensions.zotero.dataDir",')
      prefs.puts("user_pref(\"extensions.zotero.dataDir\", #{data_dir.inspect});")
    else
      prefs.puts(pref)
    end
  }
}
<commit_msg>profile<commit_after>#!/usr/bin/env ruby

require 'os'
require 'inifile'
require 'yaml'
require 'fileutils'
require 'selenium/webdriver'

if OS.linux?
  profiles_dir = File.expand_path('~/.zotero/zotero/')
else
  puts OS.report
  exit 1
end

class IniFile
  def write_compact( opts = {} )
    filename = opts.fetch(:filename, @filename)
    encoding = opts.fetch(:encoding, @encoding)
    mode = encoding ? "w:#{encoding}" : "w"

    File.open(filename, mode) do |f|
      @ini.each do |section,hash|
        f.puts "[#{section}]"
        hash.each {|param,val| f.puts "#{param}#{@param}#{escape_value val}" unless val.nil?}
        f.puts
      end
    end
    self
  end
end

profiles = IniFile.load(File.join(profiles_dir, 'profiles.ini'))

profile_name = 'BBTZ5TEST'
profile_dir = File.expand_path("~/.#{profile_name}.profile")
data_dir = File.expand_path("~/.#{profile_name}.data")

profile = nil
profiles.each{|section, param, val|
  profile = section if param == 'Name' && val ==  profile_name
}
if profile.nil?
  free = 0
  while true do
    profile = "Profile#{free}"
    break unless profiles.has_section?(profile)
    free += 1
  end

  profiles[profile]['Name'] = profile_name
end
profiles[profile]['IsRelative'] = 0
profiles[profile]['Path'] = profile_dir
profiles[profile]['Default'] = nil

profiles.each_section do |section|
  puts section
  puts profiles[section].to_yaml
end

profiles.write_compact

profile = Selenium::WebDriver::Firefox::Profile.new(File.expand_path('test/fixtures/profile/profile'))
profile.log_file = File.expand_path(File.join(File.dirname(__FILE__), "#{ENV['LOGS'] || '.'}/firefox-console.log"))

puts "Installing plugins..."
profile.add_extension('/home/emile/Downloads/mozrepl-1.1.2-fx.xpi')

profile['extensions.zotero.dataDir'] = data_dir
profile['extensions.checkCompatibility.5.0'] = false

FileUtils.rm_rf(profile_dir)
FileUtils.cp_r(profile.layout_on_disk, profile_dir)
puts profile_dir

{'fraction_non_alphanumeric': 0.16154038509627408, 'fraction_numerical': 0.004001000250062516, 'mean_word_length': 4.4274084124830395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '97322', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1427, 'n_words': 265}