<commit_before>require "sshd_config"

module Gritano
  class Ssh < Plugin
    
    def self.info
      "It installs a patched OpenSSH version used by Gritano that enables SSH lookup for public keys in a database"
    end
    
    def on_add
      File.open(File.join(@home_dir, '.gritano', 'config.yml'), "w").write({'ssh' => true}.to_yaml)
      File.open(File.join(@ssh_path, 'authorized_keys'), "w").write('')
      gritano_pub_key_path = `which gritano-pub-key`[0..-2]
      `ln -s #{gritano_pub_key_path} #{File.join(@home_dir, '.gritano', 'gritano-pub-key')}`
    end
    
    def on_remove
      File.open(File.join(@home_dir, '.gritano', 'config.yml'), "w").write({'ssh' => false}.to_yaml)
      File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
      FileUtils.rm(File.join(@home_dir, '.gritano', 'gritano-pub-key')) if File.exist?(File.join(@home_dir, '.gritano', 'gritano-pub-key'))
    end
    
    def self.check_install
      home = Etc.getpwuid.dir
      if File.exist?(File.join(home, '.gritano', 'config.yml'))
        config = YAML::load(File.open(File.join(home, '.gritano', 'config.yml')))
        return config['ssh']
      else
        return false
      end
    end
    
    add_command "help" do |params|
      Ssh.help
    end
    
    add_command "install", "gritano_path" do |params|
      gritano_dir, = params
      FileUtils.rm_rf(File.join('/tmp', 'gritano-openssh')) if Dir.exist?(File.join('/tmp', 'gritano-openssh'))
      
      puts "[git] Cloning"
      if system "git clone git://github.com/igorbonadio/gritano-openssh.git /tmp/gritano-openssh"
        puts "[build] Configuring"
        if system "cd /tmp/gritano-openssh/src && ./configure"
          puts "[build] Compiling"
          if system "cd /tmp/gritano-openssh/src && make"
            puts "[build] Installing"
            if system "cd /tmp/gritano-openssh/src && make install"
              sshd_config = SshdConfig::SshdConfig.read(File.join("/usr", "local", "etc", "sshd_config"))
              sshd_config.AuthorizedKeysScript = File.join(gritano_dir, 'gritano-pub-key')
              sshd_config.save
              return "Installed"
            end
          end
        end
      end
      <commit_msg>ssh plugin uses Config class<commit_after>require "sshd_config"

module Gritano
  class Ssh < Plugin
    
    def self.info
      "It installs a patched OpenSSH version used by Gritano that enables SSH lookup for public keys in a database"
    end
    
    def on_add
      config = Config.new(File.join(@home_dir, '.gritano', 'config.yml'))
      config.ssh = true
      config.save
      File.open(File.join(@ssh_path, 'authorized_keys'), "w").write('')
      gritano_pub_key_path = `which gritano-pub-key`[0..-2]
      `ln -s #{gritano_pub_key_path} #{File.join(@home_dir, '.gritano', 'gritano-pub-key')}`
    end
    
    def on_remove
      config = Config.new(File.join(@home_dir, '.gritano', 'config.yml'))
      config.ssh = false
      config.save
      File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
      FileUtils.rm(File.join(@home_dir, '.gritano', 'gritano-pub-key')) if File.exist?(File.join(@home_dir, '.gritano', 'gritano-pub-key'))
    end
    
    def self.check_install
      home = Etc.getpwuid.dir
      if File.exist?(File.join(home, '.gritano', 'config.yml'))
        config = Config.new(File.join(home, '.gritano', 'config.yml'))
        if config.ssh
          return config.ssh
        else
          return false
        end
      else
          return false
      end
    end
    
    add_command "help" do |params|
      Ssh.help
    end
    
    add_command "install", "gritano_path" do |params|
      gritano_dir, = params
      FileUtils.rm_rf(File.join('/tmp', 'gritano-openssh')) if Dir.exist?(File.join('/tmp', 'gritano-openssh'))
      
      puts "[git] Cloning"
      if system "git clone git://github.com/igorbonadio/gritano-openssh.git /tmp/gritano-openssh"
        puts "[build] Configuring"
        if system "cd /tmp/gritano-openssh/src && ./configure"
          puts "[build] Compiling"
          if system "cd /tmp/gritano-openssh/src && make"
            puts "[build] Installing"
            if system "cd /tmp/gritano-openssh/src && make install"
              sshd_config = SshdConfig::SshdConfig.read(File.join("/usr", "local", "etc", "sshd_config"))
              sshd_config.AuthorizedKeysScript = File.join(gritano_dir, 'gritano-pub-key')
              sshd_config.save
              return "Installed"
            end
          end
        end
      end
      
{'fraction_non_alphanumeric': 0.14145810663764963, 'fraction_numerical': 0.0008705114254624592, 'mean_word_length': 2.4530428249436516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '325540', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1573, 'n_words': 320}