<commit_before># frozen_string_literal: true

use_inline_resources

require 'win32ole' if RUBY_PLATFORM =~ /mswin|mingw32|windows/

def whyrun_supported?
  true
end

def execute_wmi_query(wmi_query)
  wmi = ::WIN32OLE.connect('winmgmts://')
  result = wmi.ExecQuery(wmi_query)
  result.each.count > 0
end

def service_installed?(servicename)
  execute_wmi_query("select * from Win32_Service where name = '#{servicename}'")
end

def install_nssm
  return if run_context.loaded_recipe? 'nssm::default'
  recipe_eval do
    run_context.include_recipe 'nssm::default'
  end
end

def nssm_exe
  "#{node['nssm']['install_location']}\\nssm.exe"
end

action :install_if_missing do
  return Chef::Log.warn('NSSM service can only be installed on Windows platforms!') unless platform?('windows')

  install_nssm

  execute "Install #{new_resource.servicename} service" do
    command "#{nssm_exe} install \"#{new_resource.servicename}\" \"#{new_resource.program}\" #{new_resource.args}"
    not_if { service_installed }
  end

  new_resource.parameters.map do |k, v|
    execute "Set parameter #{k} #{v}" do
      command "#{nssm_exe} set \"#{new_resource.servicename}\" #{k} \"#{v.gsub('"', '^"').strip}\""
      not_if { service_installed }
    end
  end

  service new_resource.servicename do # ~FC021
    action [:start]
    only_if { new_resource.start }
  end
end

action :install do
  return Chef::Log.warn('NSSM service can only be installed on Windows platforms!') unless platform?('windows')

  install_nssm

  service_installed = service_installed?(new_resource.servicename)

  execute "Install #{new_resource.servicename} service" do
    command "#{nssm_exe} install \"#{new_resource.servicename}\" \"#{new_resource.program}\" #{new_resource.args}"
    not_if { service_installed }
  end

  parameters = new_resource.parameters.merge(
    'Application' => new_resource.program,
    'AppParameters' => new_resource.args
  )

  parameters.map do |k, v|
    execute "Set parameter #{k} to #{v}" do
      command "#{nssm_exe} set \"#{new_resource.servicename}\" #{k} \"#{v.to_s.gsub('"', '""').strip}\""
      not_if "#{nssm_exe} get \"#{new_resource.servicename}\" #{k} | grep -F -- \"#{v.to_s.gsub('"', '""').strip}\""
    end
  end

  service new_resource.servicename do # ~FC021
    action [:start]
    only_if { new_resource.start }
  end
end

action :remove do
  if platform?('windows')
    service_installed = service_installed?(new_resource.servicename)

    execute "Remove service #{new_resource.servicename}" do
      command "#{nssm_exe} remove \"#{new_resource.servicename}\" confirm"
      only_if { service_installed }<commit_msg>Properly escape parameters in install action<commit_after># frozen_string_literal: true

use_inline_resources

require 'win32ole' if RUBY_PLATFORM =~ /mswin|mingw32|windows/

def whyrun_supported?
  true
end

def execute_wmi_query(wmi_query)
  wmi = ::WIN32OLE.connect('winmgmts://')
  result = wmi.ExecQuery(wmi_query)
  result.each.count > 0
end

def service_installed?(servicename)
  execute_wmi_query("select * from Win32_Service where name = '#{servicename}'")
end

def install_nssm
  return if run_context.loaded_recipe? 'nssm::default'
  recipe_eval do
    run_context.include_recipe 'nssm::default'
  end
end

def nssm_exe
  "#{node['nssm']['install_location']}\\nssm.exe"
end

action :install_if_missing do
  return Chef::Log.warn('NSSM service can only be installed on Windows platforms!') unless platform?('windows')

  install_nssm

  execute "Install #{new_resource.servicename} service" do
    command "#{nssm_exe} install \"#{new_resource.servicename}\" \"#{new_resource.program}\" #{new_resource.args}"
    not_if { service_installed }
  end

  new_resource.parameters.map do |k, v|
    execute "Set parameter #{k} #{v}" do
      command "#{nssm_exe} set \"#{new_resource.servicename}\" #{k} \"#{v.gsub('"', '^"').strip}\""
      not_if { service_installed }
    end
  end

  service new_resource.servicename do # ~FC021
    action [:start]
    only_if { new_resource.start }
  end
end

action :install do
  return Chef::Log.warn('NSSM service can only be installed on Windows platforms!') unless platform?('windows')

  install_nssm

  service_installed = service_installed?(new_resource.servicename)

  execute "Install #{new_resource.servicename} service" do
    command "#{nssm_exe} install \"#{new_resource.servicename}\" \"#{new_resource.program}\" #{new_resource.args}"
    not_if { service_installed }
  end

  parameters = new_resource.parameters.merge(
    'Application' => new_resource.program,
    'AppParameters' => new_resource.args
  )

  parameters.map do |k, v|
    value = v.to_s.gsub('"', '^"').strip
    execute "Set parameter #{k} to #{value}" do
      command "#{nssm_exe} set \"#{new_resource.servicename}\" #{k} \"#{value}\""
      not_if "#{nssm_exe} get \"#{new_resource.servicename}\" #{k} | grep -F -- \"#{value}\""
    end
  end

  service new_resource.servicename do # ~FC021
    action [:start]
    only_if { new_resource.start }
  end
end

action :remove do
  if platform?('windows')
    service_installed = service_installed?(new_resource.servicename)

    execute "Remove service #{new_resource.servicename}" do
      command "#{nssm_exe} remove \"#{new_resource.servicename}\" confirm"
      only_if { service_installed }
{'fraction_non_alphanumeric': 0.14000752728641325, 'fraction_numerical': 0.005645464809936018, 'mean_word_length': 4.395939086294416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5070214', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1841, 'n_words': 414}