# encoding: utf-8
#

require 'kde-build/command/module_based'

module BuildTool


    class BuildCommand < ModuleBasedCommand

        def initialize
            super( 'build', false )
            self.short_desc = "Do a complete build (fetch, rebase, compile, install)."
            self.description = <<-"EOS"
Do a complete build.
    * Fetch changes from the remote repository,
    * Rebase the local repository,
    * Compile,
    * Install
            EOS
            @update = true
            @configure = false
            @clean = false
            @reconfigure = false
            @install = true
            @from_scratch = false
            self.options = CmdParse::OptionParserWrapper.new do |opt|
                opt.separator "Options:"
                opt.on( "--no-update", "Do not update from the repository" ) { |t|
                    @update = false
                    }
                opt.on( "-c", "--configure", "Run the configuration step again" ) { |t|
                    @configure = true
                    }
                opt.on( "--[no-]clean", "Make clean before building." ) { |t|
                    @clean = t
                    }
                opt.on( "--reconfigure", "Remove old configuration then run configuration again" ) { |t|
                    @reconfigure = true
                    }
                opt.on( "--no-install", "Do not install" ) { |t|
                    @install = false
                    }
                opt.on( "--from-scratch", "Remove build dir" ) { |t|
                    @from_scratch = true
                    }
                opt.on( "--help", "Show this help" ) { |t|
                    }
            end
        end

        def is_module_ready( mod )
            skip = false
            if @update
                mod.activate_ssh_key
            else
                if !mod.checkedout?
                    $log.error("Module #{mod.name} is not checkedout and will be skipped!")
                    skip = true
                end
            end
            !skip
        end

        def execute_for_module( mod )

            $log.info( "###############################################################" )
            $log.info( "### Building module #{mod.name}" )
            $log.info( "###############################################################" )

            if @from_scratch
                mod.remove_build_directory
            end

            if @update

                if mod.checkedout?
                    mod.fetch
                    mod.rebase
                else
                    mod.init
                end

            end

            if @reconfigure
                mod.reconfigure( @clean.nil? ? true : @clean )

            elsif @configure
                if mod.configured?
                    mod.reconfigure
                else
                    mod.configure
                end

            elsif !mod.configured?
                mod.configure
            end

            mod.build( @install, @clean )

        end

    end

end