#
# Cookbook Name:: magic_shell
# Provider:: alias
#
# Author:: Nathen Harvey <nharvey@customink.com>
# Author:: Seth Vargo <sethvargo@gmail.com>
#
# Copyright 2012-2014, CustomInk, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Delegate update actions to inline resources
#
use_inline_resources if defined?(use_inline_resources)

#
# This provider supports why-run mode.
#
def whyrun_supported?
  true
end

#
# Create a file entry for the given alias.
#
action :add do
  file = Chef::Resource::File.new(destination, run_context)
  file.owner('root')
  file.group('root')
  file.mode('0755')
  file.content(<<-EOH.gsub(/^ {8}/, ''))
    #
    # This file was generated by Chef for #{node['fqdn']}
    # Do NOT modify this file by hand!
    #

    alias #{new_resource.alias_name}="#{new_resource.command}"
  EOH
  file.run_action(:create)
end

#
# Delete the file entry for the given alias.
#
action :remove do
  file = Chef::Resource::File.new(destination, run_context)
  file.run_action(:delete)
end

def destination
  "/etc/profile.d/#{filename}.sh"
end

def filename
  new_resource.alias_name.to_s.gsub(/\s/, '_')
end
