#
# Cookbook Name:: users
# Recipe:: default
#
# Copyright 2009-2012, Opscode, Inc.
#
# 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.
#

# Empty default recipe for including LWRPs.
cookbook_file "/etc/skel/.bash_logout" do
	owner 'root'
	group 'root'
	mode '0644'
	source 'bash_logout'
end

cookbook_file "/etc/skel/.bash_profile" do
	owner 'root'
	group 'root'
	mode '0644'
	source 'bash_profile'
end

cookbook_file "/etc/skel/.bashrc" do
	owner 'root'
	group 'root'
	mode '0644'
	source 'bashrc'
end

data_ids = data_bag('users')
data_ids.each do |id|
	u = data_bag_item('users', id)
	user u['id'] do
		home u['home']
		action [:create]
	end
	directory u['home'] + '/.ssh' do
		owner u['id']
		group u['id']
		mode '0700'
		action [:create]
	end
	template u['home'] + '/.ssh/authorized_keys' do
		owner u['id']
		group u['id']
		mode '0600'
		source 'authorized_keys.erb'
		variables({
			:pubkey => u['ssh-keys']
		})
		action [:create]
	end
	if u['sudoer'] then
		template '/etc/sudoers.d/' + u['id'] do
			owner 'root'
			group 'root'
			mode '0440'
			source 'sudoers.erb'
			variables({
				:uname => u['id']
			})
			action [:create]
		end
	end
end
