#
# Cookbook Name:: dynatrace
# Resource:: uninstall_package
#
# Copyright:: Copyright 2016, Dynatrace
#

actions :run
default_action :run

property :name, String, :name_property => true
property :installation_path, String
property :installer_cache_path, String
property :service_name, String, :default => nil

action :run do
  include_recipe 'dynatrace::node_info'

  delete_cache_path_action = "Delete the installer cache directory #{installer_cache_path}"
  delete_install_path_action = "Delete installation dir by link ''#{installation_path}''"

  unless service_name.to_s.empty?
    service service_name do
      action [:stop, :disable]
    end
  end

  ruby_block 'Defer directories deletion to the end of run list' do
    block {}
    notifies :delete, "directory[#{delete_cache_path_action}]"
    notifies :run, "dynatrace_delete_directory_by_link[#{delete_install_path_action}]"
  end

  directory delete_cache_path_action do
    path installer_cache_path
    recursive true
    action :nothing
  end

  dynatrace_delete_directory_by_link delete_install_path_action do
    link2delete installation_path
    action :nothing
  end
end

#
# Cookbook Name:: dynatrace
# Resource:: uninstall_package
#
# Copyright:: Copyright 2016, Dynatrace
#

actions :run
default_action :run

property :name, String, :name_property => true
property :installation_path, String
property :installer_cache_path, String
property :service_name, String, :default => ''

action :run do
  include_recipe 'dynatrace::node_info'

  delete_cache_path_action = "Delete the installer cache directory #{installer_cache_path}"
  delete_install_path_action = "Delete installation dir by link ''#{installation_path}''"

  unless service_name.to_s.empty?
    service service_name do
      action [:stop, :disable]
    end
  end

  ruby_block 'Defer directories deletion to the end of run list' do
    block {}
    notifies :delete, "directory[#{delete_cache_path_action}]"
    notifies :run, "dynatrace_delete_directory_by_link[#{delete_install_path_action}]"
  end

  directory delete_cache_path_action do
    path installer_cache_path
    recursive true
    action :nothing
  end

  dynatrace_delete_directory_by_link delete_install_path_action do
    link2delete installation_path
    action :nothing
  end
end

Refactor uninstall recipes * Fixed error in logs "Default value nil is invalid for property service_name "
[JLT-163581] Refactor uninstall recipes
* Fixed error in logs "Default value nil is invalid for property service_name "
