#!/usr/bin/env ruby
=begin

**  Description: 

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

**  Copyright: (C) 2007 - 2015 Ralph Amissah

**  License: 

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

=end
#% manual settings, edit/update as required (note current default settings are obtained from sisu version yml file)
begin
  require 'thor'
rescue LoadError
  puts 'thor (package ruby-thor) not found'
end
begin
  require './setup/sisu_version'   # ./setup/sisu_version.rb
    include SiSUversion
rescue LoadError
  puts 'this does not appear to be a SiSU development directory'
  exit
end
require_relative 'setup/qi_libs'        # setup/qi_libs.rb
require 'find'
require 'fileutils'
  include FileUtils
require 'pathname'
require 'rbconfig.rb'
require 'yaml'
module SiSUconf
  class Configure < Thor
    class_option :verbose, :type => :boolean
    desc 'setup --all --bin --lib --conf --data --alt --dryrun',
      'setup sisu'
    options \
      :all => :boolean,
      :bin => :boolean,
      :lib => :boolean,
      :conf => :boolean,
      :data => :boolean,
      :share => :boolean,
      :man => :boolean,
      :vim => :boolean,
      :alt => :boolean,
      :dryrun => :boolean,
      :is => :boolean
    def setup
      unless options.length >= 1 \
      and not (options[:bin] \
      or options[:lib] \
      or options[:conf] \
      or options[:data] \
      or options[:share] \
      or options[:man] \
      or options[:vim])
        puts 'setup --all --bin --lib --conf --data --share --man --vim'
      end
      act=(options[:dryrun]) ? (:dryrun) : (:action)
      if options[:is]
        puts Version_info.version_number_info_stable
      end
      if not options[:alt]
        if options[:all] \
        or options[:bin]
          exclude_files=['sisugem']
          Install.setup_find_create(
            'bin',
            Project_details.dir.bin,
            exclude_files,
            act
          ) if File.directory?('bin')
        end
        if options[:all] \
        or options[:lib]
          Install.setup_find_create(
            'lib',
            Project_details.dir.lib,
            act
          ) if File.directory?('lib')
        end
        if options[:all] \
        or options[:conf]
          Install.setup_find_create(
            'conf',
            Project_details.dir.conf,
            act
          ) if File.directory?('conf')
        end
        if options[:all] \
        or options[:data]
          Install.setup_find_create(
            'data',
            Project_details.dir.data,
            act
          ) if File.directory?('data')
        end
        if options[:all] \
        or options[:share]
          Install.setup_find_create(
            'data/sisu',
            Project_details.dir.share,
            act
          ) if File.directory?('data/sisu')
        end
        if options[:all] \
        or options[:man]
          Install.setup_find_create(
            'man',
            Project_details.dir.man,
            act
          ) if File.directory?('man')
        end
        if options[:all] \
        or options[:vim]
          Install.setup_find_create(
            'data/vim',
            Project_details.dir.vim,
            act
          ) if File.directory?('data/vim')
        end
      else
        if options[:all] \
        or options[:bin]
          Install.setup_find_cp_r(
            'bin',
            Project_details.dir.bin,
            act
          ) if File.directory?('bin')
        end
        if options[:all] \
        or options[:bin]
          Install.setup_find_cp_r(
            'lib',
            Project_details.dir.lib,
            act
          ) if File.directory?('lib')
        end
        if options[:all] \
        or options[:conf]
          Install.setup_find_cp_r(
            'conf',
            Project_details.dir.conf,
            act
          ) if File.directory?('conf')
        end
        if options[:all] \
        or options[:data]
          Install.setup_find_cp_r(
            'data',
            Project_details.dir.data,
            act
          ) if File.directory?('data')
        end
        if options[:all] \
        or options[:share]
          Install.setup_find_cp_r(
            'data/sisu',
            Project_details.dir.share,
            act
          ) if File.directory?('data/sisu') #
        end
        if options[:all] \
        or options[:man]
          Install.setup_find_cp_r(
            'man',
            Project_details.dir.man,
            act
          ) if File.directory?('man')
        end
        #if options[:all] \
        #or options[:vim]
        #  Install.setup_find_cp_r('data/vim',"#{Project_details.dir.data}/vim") \
        #    if File.directory?('data/vim')
        #end
      end
    end
    desc 'gem --create --build --install',
      'gem create build and install'
    options \
      :create => :boolean,
      :build => :boolean,
      :install => :boolean,
      :git_version_number => :boolean,
      :is => :boolean
    def gem
      if options[:is]
        puts Version_info.version_number_info_stable
      end
      if options[:create]
        version=(options[:git_version_number]) \
        ? :version_git
        : :version_standard
        Gemspecs::Current.create_stable(version)
        puts 'created gemspec' \
          if options[:verbose]
      end
      if options[:build]
        Gemspecs::Current.build_stable
        puts 'built gem' \
          if options[:verbose]
      end
      if options[:install]
        Gemspecs::Current.install_stable
        puts 'installed gem, version: stable' \
          if options[:verbose]
      end
      unless options.length > 0
        system("#{$called_as} help gem")
      end
    end
    desc 'pkg',
      'package maintenance tasks, ' \
       + 'of no general interest ' \
       + '(maintainer specific for package maintainer\'s convenience)'
    options \
      :open_version=> :boolean,
      :version_and_tag_for_release=> :boolean,
      :tip => :boolean,
      :is => :boolean
    def pkg
      if options[:is]
        puts Version_info.version_number_info_stable
      end
      if options[:tip]
        Package.sequence
      end
      if options[:open_version]
        Version_info::Update.update_documentation
        Version_info::Update.update_stable(:pre_release)
        Version_info::Update.update_pkgbuild_stable(:pre_release)
        Version_info::Update.changelog_header_stable_pre_release
        Version_info::Update.changelog_header_commit(:pre_release)
      end
      if options[:version_and_tag_for_release]
        Version_info::Update.update_documentation
        Version_info::Update.update_stable(:release)
        Version_info::Update.update_pkgbuild_stable(:release)
        Version_info::Update.changelog_header_stable
        Version_info::Update.changelog_header_commit_tag_upstream(:release)
      end
      if options.length == 0
        system("#{$called_as} help pkg")
        system("#{$called_as} pkg --tip")
      end
    end
  end
end
begin
  $called_as,$argv=$0,$*
  SiSUconf::Configure.start(ARGV)
rescue
end
__END__