#--
# Copyright (c) 2009, 2010 Novell, Inc.
# 
# All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License
# as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
# 
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#++
#
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))
$:.unshift File.join(File.dirname(__FILE__), "..", "webservice-tasks", "lib")

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

#include in doc:app also shared test enhancements for plugins
#FIXME find way how to change original rails task app:doc we only need include some files to it
namespace :doc do
  Rake::RDocTask.new("app") { |rdoc|
    rdoc.rdoc_dir = 'doc/app'
    rdoc.title    = "Rails Application Documentation"
    rdoc.options << '--line-numbers' << '--inline-source'
    rdoc.rdoc_files.include('doc/README_FOR_APP')
    rdoc.rdoc_files.include('app/**/*.rb')
    rdoc.rdoc_files.include('lib/**/*.rb')
#include shared test functionality in documentation
    rdoc.rdoc_files.include('test/plugin_basic_tests.rb')
  }
end

# load the shared rake files from the package itself
# skip 'deploy_local' task, it's redefined here
require 'tasks/webservice_tasks'
WebserviceTasks.loadTasks(:exclude => ["deploy_local.rake"])

# this call also loads WebserviceTasks but the second call is ignored there
# so this 'require' must be called _after_ WebserviceTasks.loadTasks
require 'tasks/rails'

require 'fileutils'

def grant(user, right)
  sh "/usr/bin/polkit-auth --user #{user} --grant #{right} ||:"
end

desc "Deploy for development - run db:migrate, create basic dirs and install grantwebyastrights."
task :deploy_local, [:user] => :"db:migrate" do |t, args|
  args.with_defaults :user => "yastws"
  FileUtils.mkdir_p "/etc/webyast"
  FileUtils.mkdir_p "/var/lib/yastws"
  FileUtils.mkdir_p "/usr/share/yastws"
  FileUtils.install "package/grantwebyastrights", "/usr/sbin", :mode => 0555
  mkdir_p "/etc/dbus-1/system.d/"
  cp_r    "package/webyast.permissions.conf", "/etc/dbus-1/system.d/"
  cp_r    "package/webyastPermissionsService.rb", "/usr/sbin/"
  chmod  0744, "/usr/sbin/webyastPermissionsService.rb"
  mkdir_p "/usr/share/dbus-1/system-services/"
  cp_r    "package/webyast.permissions.service.service", "/usr/share/dbus-1/system-services/"
  FileUtils.mkdir_p "/usr/share/PolicyKit/policy/"
  FileUtils.cp_r    "package/org.opensuse.yast.permissions.policy",  "/usr/share/PolicyKit/policy/"
  if args.user == "yastws"
    # add user yastws
    sh "/usr/sbin/groupadd -r yastws ||:" #&>/dev/null ||:
    sh "/usr/sbin/useradd  -g yastws -s /bin/false -r -c 'User for YaST-Webservice' -d /var/lib/yastws yastws ||:"
  end
  # grant rights to him
  grant args.user, "org.opensuse.yast.module-manager.import"
  grant args.user, "org.freedesktop.policykit.read"
  grant args.user, "org.freedesktop.packagekit.system-update"
end

