require_relative 'base'
require 'apaka/packaging/config'

module Apaka
    module CLI
        class Prepare < Base

            def prepare(packager, options)
                puts "Preparing local building of packages"
                local_distribution = packager.target_platform.distribution_release_name
                local_arch = packager.target_platform.architecture
                Apaka::Packaging::Installer.install_all_requirements(options[:patch_dir], local_distribution, local_arch)

                tempfile = Tempfile.new("apaka.conf.")
                document_root = File.join(Apaka::Packaging::WWW_ROOT)
                packages_subfolder = File.basename(Apaka::Packaging::DEB_REPOSITORY)
                autogenerated_config = tempfile.path

                Apaka::Packaging::Installer.ensure_webserver_running
                Apaka::Packaging::Installer.create_webserver_config(document_root, packages_subfolder,
                                                                       autogenerated_config)
                Apaka::Packaging::Installer.install_webserver_config(autogenerated_config)

                puts "Preparation completed"
            end


            def initialize
                super()
            end

            def run(args, options)
                packager = Apaka::Packaging::Deb::Package2Deb.new(options)
                prepare(packager, options)
            end
        end
    end
end
