#!/usr/bin/env ruby
=begin
 Common Rakefile, Rantfile installer for SiSU
   softlink Rakefile and Rantfile to this file

 * Homepage: <http://www.jus.uio.no/sisu/SiSU>
             <http://search.sisudoc.org>
 * Download: <http://www.jus.uio.no/sisu/download>

 Copyright (C) 2007 Ralph Amissah

 * License: LGPL - GNU Lesser General Public License
     [same license as Rant provided within the Rant package]

 * Ralph Amissah <ralph@amissah.com>
   Ralph Amissah <ralph.amissah@gmail.com>

 Rake is a Ruby build program by Jim Weirich
 * Rake may be downloaded and installed from:
     <http://rake.rubyforge.org/>

 Rant is a Ruby build program by Stefan Lang
 * Rant may be downloaded and installed from:
     <http://make.rubyforge.org/>

 Notes on use:
   [if rake is preferred and installed]
     rake -T
   [if rant is preferred and installed]
     rant -T

 SiSU can also be Setup/Installation using:
 * Minero Aoki's setup.rb, provided along with SiSU, or

=end
#%% produce a makefile suitable for the target platform
#require 'mkmf'
#create_makefile("sisu")
#% manual settings, edit/update as required (note current default settings are obtained from sisu version yml file)
require 'find'
require 'fileutils'
#require 'ftools'
require 'rbconfig.rb'
require 'yaml'
include FileUtils
require_relative 'sisu_version'                # sisu_version.rb
  include SiSUversion
require_relative 'rbuild_libs'                 # rbuild_libs.rb
  include Project_details
  include Utils
  include Version_info
  include Gemspecs
  include GitExtractTaggedVersionBuild
require_relative 'rbuild_help'                 # rbuild_help.rb
  include Help
#% tasks
desc "show rake/rant tasks for sisu install, and sisu gem (create spec, build &) install"
task :default =>
  [:note_sources,:tasks,:note_installation] #[:default_notice,:setup_base]
desc "Setup/Install #{Project_details.name}"
task :setup_project=>
  [:setup_bin_,:setup_lib_,:setup_conf_,:setup_share_,:setup_data_,:setup_man_,:setup_vim_,:src_note]
task :project=> \
  [:setup_project]
desc "Setup/Install #{Project_details.name}"
task :setup=>
  [:setup_bin_, :setup_lib_,:setup_conf_,:setup_share_,:setup_data_] #, :help]
desc "Setup/Install #{Project_details.name}: bin, lib and conf (no data)"
task :setup_base=>
  [:setup_bin_,:setup_lib_,:setup_conf_,:setup_share_,:setup_man_,:setup_vim_,:note_sources]
task :base=>
  [:setup_base]
desc "Setup/Install #{Project_details.name}: bin, lib, conf & data"
task :setup_with_data=>
  [:setup_base,:setup_data]
desc "check package version"
task :sisuversion =>
  [:sisu_version]
task :version =>
  [:sisu_version]
#desc "set package version"
task :sisuversionset =>
  [:sisu_version_set,:changelog_headers]
desc "check gemspec info"
task :gem_spec =>
  [:gemspec_info]
desc "create sisu v_stable gemspec"
task :gem_create_spec_stable =>
  [:gemspecs_stable_create_default_version]
task :gem5cs =>
  [:gem_create_spec_stable]
task :gem5createspecs =>
  [:gemspecs_stable_create_default_version]
desc "create sisu v_unstable gemspec"
task :gem_create_spec_unstable =>
  [:gemspecs_unstable_create_default_version]
task :gem6cs =>
  [:gem_create_spec_unstable]
task :gem6createspecs =>
  [:gemspecs_unstable_create_default_version]
desc "create gemspec"
task :gem_create_spec =>
  [:gemspecs_create_default_version]
task :gemcs =>
  [:gemspecs_create_default_version]
task :gemc =>
  [:gemspecs_create_default_version]
task :gemspecscreate =>
  [:gemspecs_create_default_version]
#---
desc "build gem"
task :gem_build =>
  [:gem_build_]
task :gemb =>
  [:gem_build]
task :gembuild =>
  [:gem_build]
desc "build sisu v_stable gem"
task :gem_build_stable =>
  [:gem_stable_build]
task :gem5b =>
  [:gem_build_stable]
task :gem5build =>
  [:gem_build_stable]
desc "build sisu v_unstable gem"
task :gem_build_unstable =>
  [:gem_unstable_build]
task :gem6b =>
  [:gem_build_unstable]
task :gem6build =>
  [:gem_build_unstable]
#---
desc "create, build & install sisu v_stable gem"
  task :gem_create_build_install_stable =>
    [:gemspecs_stable_create_default_version,:gem_stable_build,:gem_stable_install]
task :gem5cbi =>
  [:gem_create_build_install_stable]
desc "create, build & install sisu v_unstable gem"
task :gem_create_build_install_unstable =>
  [:gemspecs_unstable_create_default_version,:gem_unstable_build,:gem_unstable_install]
task :gem6cbi =>
  [:gem_create_build_install_unstable]
desc "create, build & install sisu v_stable & v_unstable gem"
task :gem_create_build_install =>
  [:gemspecs_create_default_version,:gem_build_,:gem_install_]
task :gemcbi =>
  [:gem_create_build_install]
#---
desc "install gem"
task :gem_install =>
  [:gem_install_]
task :gemi =>
  [:gem_install]
task :geminstall =>
  [:gem_install]
desc "build & install sisu v_stable gem"
task :gem_build_install_stable =>
  [:gem_stable_build,:gem_install_]
task :gem5bi =>
  [:gem_build_install_stable]
desc "build & install sisu v_unstable gem"
task :gem_build_install_unstable =>
  [:gem_unstable_build,:gem_install_]
task :gem6bi =>
  [:gem_build_install_unstable]
desc "build & install gem"
task :gem_build_install =>
  [:gem_build,:gem_install_]
task :gembi =>
  [:gem_build_install]
#-- manually set next version
#desc "create sisu v_stable gemspec, manually set next version"
task :gem5csn =>
  [:gemspecs_stable_create_next_version]
#desc "create sisu v_unstable gemspec, manually set next version"
task :gem6csn =>
  [:gemspecs_unstable_create_next_version]
#desc "create gemspec, manually set next version"
task :gemcsn =>
  [:gemspecs_create_next_version]
task :gemcn =>
  [:gemspecs_create_next_version]
#desc "build gem, manually set next version"
task :gembn =>
  [:gem_build_next_version]
#desc "build sisu v_stable gem, manually set next version"
task :gem5bn =>
  [:gem_stable_build_next_version]
#desc "build sisu v_unstable gem, manually set next version"
task :gem6bn =>
  [:gem_unstable_build_next_version]
#desc "install gem, manually set next version"
task :gemin =>
  [:gem_install_next_version]
#desc "build & install sisu v_stable gem, manually set next version"
task :gem5bin =>
  [:gem_stable_build_next_version,:gem_install_next_version]
#desc "build & install sisu v_unstable gem, manually set next version"
task :gem6bin =>
  [:gem_unstable_build_next_version,:gem_install_next_version]
#desc "build & install gem, manually set next version"
task :gembin =>
  [:gem_build_next_version,:gem_install_next_version]
#desc "create, build & install sisu v_stable gem, manually set next version"
task :gem5cbin =>
  [:gemspecs_stable_create_next_version,:gem_stable_build_next_version,:gem_stable_install_next_version]
#desc "create, build & install sisu v_unstable gem, manually set next version"
task :gem6cbin =>
  [:gemspecs_unstable_create_next_version,:gem_unstable_build_next_version,:gem_unstable_install_next_version]
#desc "create, build & install sisu v_stable & v_unstable gem, manually set next version"
task :gemcbin =>
  [:gemspecs_create_next_version,:gem_build_next_version,:gem_install_next_version]
#---
#desc "check changelog headers"
task :changelogheaders =>
  [:changelog_headers]
task :dev =>
  [:note_developer]
task :developer_note =>
  [:note_developer]
if File.directory?('bin')
  desc "Setup #{Project_details.name} bin only, synonym :bin"
  task :setup_bin =>
    [:setup_bin_]
  task :bin =>
    [:setup_bin]
end
if File.directory?('lib')
  desc "Setup #{Project_details.name} lib only, synonym :lib"
  task :setup_lib =>
    [:setup_lib_]
  task :lib =>
    [:setup_lib]
end
if File.directory?('conf')
  desc "Setup #{Project_details.name} conf only, synonyms :conf & :etc"
  task :setup_conf =>
    [:setup_conf_]
  task :conf =>
    [:setup_conf]
  task :setup_etc =>
    [:setup_conf]
  task :etc =>
    [:setup_conf]
end
if File.directory?('data')
  desc "Setup #{Project_details.name} data only, synonyms :data & :examples"
  task :setup_data =>
    [:setup_data_]
  task :data =>
    [:setup_data]
  task :setup_examples =>
    [:setup_data]
  task :examples =>
    [:setup_data]
end
if File.directory?('data/sisu')
  #desc "Setup #{Project_details.name} shared data only (odf & shared images)"
  task :setup_share =>
    [:setup_share_]
  task :share =>
    [:setup_share]
end
if File.directory?('man')
  desc "Setup #{Project_details.name} man pages only, synonyms :man"
  task :setup_man =>
    [:setup_man_]
  task :man =>
    [:setup_man]
end
if File.directory?('data/vim')
  desc "Setup #{Project_details.name} vim config files only, synonyms :vim"
  task :setup_vim =>
    [:setup_vim_]
  task :vim =>
    [:setup_vim]
end
desc "Remove #{Project_details.name} (all versions)"
task :remove_package =>
  [:remove_bin, :remove_lib, :remove_conf]
if File.directory?('bin')
  #desc "Remove #{Project_details.name} bin only"
  task :remove_bin =>
    [:remove_bin]
end
if File.directory?('lib')
  #desc "Remove #{Project_details.name} lib only"
  task :remove_lib =>
    [:remove_lib]
end
if File.directory?('conf')
  #desc "Remove #{Project_details.name} conf only"
  task :remove_conf =>
    [:remove_conf]
end
desc "Re-setup #{Project_details.name}, synonym :reinstall"
task :resetup =>
  [:remove, :setup]
task :reinstall =>
  [:remove, :setup]
#desc "Re-setup #{Project_details.name}: bin, lib, conf (ignore data), synonym :reinstall"
task :resetup_base =>
  [:remove, :setup_base_]
task :reinstall_base =>
  [:remove, :setup_base_]
if File.directory?('bin')
  #desc "Re-setup #{Project_details.name} bin, synonym :reinstall"
  task :resetup_bin =>
    [:remove_bin, :setup_bin_]
  task :reinstall_bin =>
    [:remove_bin, :setup_bin_]
end
if File.directory?('lib')
  #desc "Re-setup #{Project_details.name} lib, synonym :reinstall_lib"
  task :resetup_lib =>
    [:remove_lib, :setup_lib_]
  task :reinstall_lib =>
    [:remove_lib, :setup_lib_]
end
if File.directory?('conf')
  #desc "Re-setup #{Project_details.name} conf, synonyms :reinstall_conf & :resetup_etc"
  task :resetup_conf =>
    [:remove_conf, :setup_conf_]
  task :reinstall_conf =>
    [:remove_conf, :setup_conf_]
  task :resetup_etc =>
    [:remove_conf, :setup_conf_]
  task :reinstall_etc =>
    [:remove_conf, :setup_conf_]
end
if File.directory?('data/sisu')
  #desc "Re-setup #{Project_details.name} shared data, (odf & images)"
  task :resetup_share =>
    [:remove_share, :setup_share_]
  task :reinstall_share =>
    [:remove_share, :setup_share_]
end
if File.directory?('man')
  #desc "Re-setup #{Project_details.name} man, synonym :reinstall_man"
  task :resetup_man =>
    [:remove_man, :setup_man_]
  task :reinstall_man =>
    [:remove_man, :setup_man_]
end
desc 'Setup Note'
task :setup_note =>
  [:help]
desc "System information used by #{Project_details.name}"
task :system =>
  [:system_info,:project_help,:note_sources]
desc "show all system info available - parameters found"
task :system_param =>
  [:system_param_]
desc "Gem environment information used ruby gems for #{Project_details.name}"
task :gem_env =>
  [:gem_env_]
desc 'Help'
task :help =>
  [:project_help,:system_info,:tasks]
#desc "Setup/Install #{Project_details.name} (uses filelist)"
task :install =>
  [:default_notice,:project]
task :install_bin =>
  [:setup_bin_]
#desc "search for a version tag e.g. 'tag[5.6.0]'"
task :tag, [:tag] do |t, args|
  args.with_defaults(:tag => Version_info::Next.setting_stable[:version])
  puts "Check for Version Tag: #{args.tag}"
  print "Version Tag:           "
  GitExtractTaggedVersionBuild::git_tagged_versions(args)
end
#desc "build and install a specific git tagged version of sisu, e.g. 'build[5.6.0]'"
task :build, [:tag, :branch] => :done do |t, args|
  args.with_defaults(:tag => '5.6.0', :branch => 'stable')
  puts "Version Tag: #{args.tag}"
  puts "Branch: #{args.branch}"
  GitExtractTaggedVersionBuild::git_tagged_versions(args.tag)
  ans= <<-WOK
Gem Install SiSU Version
  WOK
  resp=Utils.answer?(ans)
  exit unless resp
  GitExtractTaggedVersionBuild::git_checkout_and_build_version(args)
end
task :done do
  puts 'done'
end
                                                           #%% setup/install tasks
task :rant_independence do                                 #notice
  resp=''
  while resp.length < 4
    resp='sisu-install'                                     #default name install
    print %{#{Project_details.rake_rant}
  Create a rant dependency independent file
  provide filename default name is "install"
    [Warning, will overwrite file of name provided
      provide name or "quit" to exit]: }
    exit if resp =~/^(?:n|quit|exit)$/
  end
  remove='y' #remove='n'
 if remove =~/y/
   system("rant-import --force --auto #{resp};
     chmod 755 #{resp}
   ")
 else #puts "#{resp} not replaced"
 end
end

task :default_notice do                                    #notice
  Utils.default_notice
end
task :default2 do                                     #secondary
  setup_find_cp_r('bin',Project_details.dir.bin) \
    if File.directory?('bin')
  setup_find_cp_r('lib',Project_details.dir.lib) \
    if File.directory?('lib')
  setup_find_cp_r('conf',Project_details.dir.conf) \
    if File.directory?('conf')
  setup_find_cp_r('data/sisu',Project_details.dir.share) \
    if File.directory?('data/sisu') #
  setup_find_cp_r('data',Project_details.dir.data) \
    if File.directory?('data')
  setup_find_cp_r('data/vim',"#{Project_details.dir.data}/vim") \
    if File.directory?('data/vim')
  setup_find_cp_r('man',Project_details.dir.man) \
    if File.directory?('man')
end
task :setup_bin_ do
  exclude_files=['sisugem']
  Install.setup_find_create('bin',Project_details.dir.bin,exclude_files) \
    if File.directory?('bin')
end
task :setup_lib_ do
  Install.setup_find_create('lib',Project_details.dir.lib) \
    if File.directory?('lib')
end
task :setup_conf_ do
  Install.setup_find_create('conf',Project_details.dir.conf) \
    if File.directory?('conf')
end
task :setup_share_ do
  Install.setup_find_create('data/sisu',Project_details.dir.share) \
    if File.directory?('data/sisu')
end
task :setup_data_ do
  Install.setup_find_create('data',Project_details.dir.data) \
    if File.directory?('data')
end
task :setup_man_ do
  Install.setup_find_create('man',Project_details.dir.man) \
    if File.directory?('man')
  Install.setup_find_create('man.deb/man',Project_details.dir.man) \
    if File.directory?('man.deb/man')
end
task :setup_vim_ do
  Install.setup_find_create('data/vim',Project_details.dir.vim) \
    if File.directory?('data/vim')
end
task :gemspec_info do
  Gemspecs.info_stable
  Gemspecs.info_unstable
end
task :gemspecs_stable_create_default_version do
  Gemspecs::Current.create_stable
end
task :gemspecs_unstable_create_default_version do
  Gemspecs::Current.create_unstable
end
task :gemspecs_create_default_version do
  Gemspecs::Current.create_stable
  Gemspecs::Current.create_unstable
end
task :gemspecs_stable_create_next_version do
  Gemspecs::Next.create_stable
end
task :gemspecs_unstable_create_next_version do
  Gemspecs::Next.create_unstable
end
task :gemspecs_create_next_version do
  Gemspecs::Next.create_stable
  Gemspecs::Next.create_unstable
end
task :gem_stable_build do
  Gemspecs::Current.build_stable
end
task :gem_unstable_build do
  Gemspecs::Current.build_unstable
end
task :gem_build_ do
  Gemspecs::Current.build_stable
  Gemspecs::Current.build_unstable
end
task :gem_stable_build_next_version do
  Gemspecs::Next.build_stable
end
task :gem_unstable_build_next_version do
  Gemspecs::Next.build_unstable
end
task :gem_build_next_version do
  Gemspecs::Next.build_stable
  Gemspecs::Next.build_unstable
end
task :gem_stable_install do
  Gemspecs::Current.install_stable
end
task :gem_unstable_install do
  Gemspecs::Current.install_unstable
end
task :gem_install_ do
  Gemspecs::Current.install_stable
  Gemspecs::Current.install_unstable
end
task :gem_stable_install_next_version do
  Gemspecs::Next.install_stable
end
task :gem_unstable_install_next_version do
  Gemspecs::Next.install_unstable
end
task :gem_install_next_version do
  Gemspecs::Next.install_stable
  Gemspecs::Next.install_unstable
end
task :changelog_headers do
  puts '---'
  puts Version_info::Update.changelog_header_stable
  puts '---'
  puts Version_info::Update.changelog_header_unstable
end
task :sisu_version do
  puts Version_info::Next.setting_stable
  puts Version_info::Next.setting_unstable
  puts '---'
  puts Version_info::Current.setting_stable[:project]
  puts Version_info::Current.setting_stable[:version]
  puts Version_info::Current.setting_stable[:date]
  puts Version_info::Current.setting_stable[:date_stamp]
  puts '---'
  puts Version_info::Current.setting_unstable[:project]
  puts Version_info::Current.setting_unstable[:version]
  puts Version_info::Current.setting_unstable[:date]
  puts Version_info::Current.setting_unstable[:date_stamp]
end
task :sisu_version_set do
  Version_info::Update.update_stable
  Version_info::Update.update_unstable
end
                                                           #%% post install
                                                           #%% clobber/remove tasks
task :remove_bin do
  rm_r "#{Project_details.dir.bin}/#{Project_details.dir.proj}" \
    if FileTest.file?("#{Project_details.dir.bin}/#{Project_details.dir.proj}")
end
task :remove_lib do
  rm_r "#{Project_details.dir.lib}/#{Project_details.dir.proj}" \
    if FileTest.directory?("#{Project_details.dir.lib}/#{Project_details.dir.proj}")
end
task :remove_conf do
  rm_r "#{Project_details.dir.conf}/#{Project_details.dir.proj}" \
    if FileTest.directory?("#{Project_details.dir.conf}/#{Project_details.dir.proj}")
end
task :remove_man do
  rm_r "#{Project_details.dir.man}/**/#{Project_details.dir.proj}" \
    if FileTest.directory?("#{Project_details.dir.man}/man1/#{Project_details.dir.proj}")
end
task :remove_version do
  rm_r "#{Project_details.dir.bin}/#{Project_details.dir.proj}" \
    if FileTest.file?("#{Project_details.dir.bin}/#{Project_details.dir.proj}")
  rm_r "#{Project_details.dir.lib}/#{Project_details.dir.proj}/#{Project_details.version}" \
    if FileTest.directory?("#{Project_details.dir.lib}/#{Project_details.dir.proj}/#{Project_details.version}")
  rm_r "#{Project_details.dir.conf}/#{Project_details.dir.proj} \
    if FileTest.directory?("#{Project_details.dir.conf}/#{Project_details.dir.proj}")
end
task :remove_package do
  rm_r "#{Project_details.dir.bin}/#{Project_details.dir.proj}" \
    if FileTest.file?("#{Project_details.dir.bin}/#{Project_details.dir.proj}")
  rm_r "#{Project_details.dir.lib}/#{Project_details.dir.proj}" \
    if FileTest.directory?("#{Project_details.dir.lib}/#{Project_details.dir.proj}")
  rm_r "#{Project_details.dir.conf}/#{Project_details.dir.proj}" \
    if FileTest.directory?("#{Project_details.dir.conf}/#{Project_details.dir.proj}")
end
task :note_sources do
  puts <<-WOK

  SiSU project:
      <http://sisudoc.org>
      <http://www.jus.uio.no/sisu>
  sisu source code is available at:
      <http://git.sisudoc.org>
      <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
  sisu markup samples are provided/packaged separately as sisu-markup-samples:
      <http://git.sisudoc.org/gitweb/?p=doc/sisu-markup-samples.git;a=summary>
  WOK
end
task :note_installation do
  puts <<-WOK
  alternative 0: distribution install, rather than this Rakefile
    a distribution install pulls in the many dependencies used by sisu after
    initial processing to generate and store output, significant amongst these are
    XeTeX & databases (sqlite3 and postgresql)

  alternative 1: gem install, you need to:
    create the gemspec; build the gem (from the gemspec); install the gem
  which can be done with the single command:
    rake gem_create_build_install   # (to build and install sisu v5 & sisu v6, alias gemcbi)
  separate gems are made/installed for sisu v5 & sisu v6 contained in source:
    rake gem_create_build_install_stable   # (to build and install sisu v5, alias gem5cbi)
    rake gem_create_build_install_unstable   # (to build and install sisu v6, alias gem6cbi)
  for individual steps (create, build, install) see rake options, rake -T
  to specify sisu version for sisu installed via gem
    sisu _#{Version_info::Current.setting_stable[:version]}_ --version
    sisu _#{Version_info::Current.setting_unstable[:version]}_ --version
  to uninstall sisu installed via gem
    sudo gem uninstall --verbose sisu
  WOK
end
task :note_developer do
  puts <<-WOK

  changelogheaders

  sisuversion
  sisuversionset

  gemcsn gem5csn gem6csn
  gembn gem5bn gem6bn
  gemin gem5in gem6in
  gembin gem5bin gem6bin
  gemcbin gem5cbin gem6cbin
  WOK
end
                                                           #%% help & system info
task :system_info do
  Project_details.system_info
end
task :system_param_ do
  Project_details.env.each {|c| puts c.inspect }
end
task :gem_env_ do
  Project_details.gem_env
end
task :project_help do
  Help.project_help
end
task :tasks do
  Help.tasks
end