# File lib/fpm/package/deb.rb, line 349
  def output(output_path)
    self.provides = self.provides.collect { |p| fix_provides(p) }
    output_check(output_path)
    # Abort if the target path already exists.

    # create 'debian-binary' file, required to make a valid debian package
    File.write(build_path("debian-binary"), "2.0\n")

    # If we are given --deb-shlibs but no --after-install script, we
    # should implicitly create a before/after scripts that run ldconfig
    if attributes[:deb_shlibs]
      if !script?(:after_install)
        logger.info("You gave --deb-shlibs but no --after-install, so " \
                     "I am adding an after-install script that runs " \
                     "ldconfig to update the system library cache")
        scripts[:after_install] = template("deb/ldconfig.sh.erb").result(binding)
      end
      if !script?(:after_remove)
        logger.info("You gave --deb-shlibs but no --after-remove, so " \
                     "I am adding an after-remove script that runs " \
                     "ldconfig to update the system library cache")
        scripts[:after_remove] = template("deb/ldconfig.sh.erb").result(binding)
      end
    end

    if script?(:before_upgrade) or script?(:after_upgrade)
      if script?(:before_install) or script?(:before_upgrade)
        scripts[:before_install] = template("deb/preinst_upgrade.sh.erb").result(binding)
      end
      if script?(:before_remove)
        scripts[:before_remove] = template("deb/prerm_upgrade.sh.erb").result(binding)
      end
      if script?(:after_install) or script?(:after_upgrade)
        scripts[:after_install] = template("deb/postinst_upgrade.sh.erb").result(binding)
      end
      if script?(:after_remove)
        scripts[:after_remove] = template("deb/postrm_upgrade.sh.erb").result(binding)
      end
    end

    write_control_tarball

    # Tar up the staging_path into data.tar.{compression type}
    case self.attributes[:deb_compression]
      when "gz", nil
        datatar = build_path("data.tar.gz")
        compression = "-z"
      when "bzip2"
        datatar = build_path("data.tar.bz2")
        compression = "-j"
      when "xz"
        datatar = build_path("data.tar.xz")
        compression = "-J"
      else
        raise FPM::InvalidPackageConfiguration,
          "Unknown compression type '#{self.attributes[:deb_compression]}'"
    end

    # Write the changelog file
    dest_changelog = File.join(staging_path, "usr/share/doc/#{name}/changelog.Debian.gz")
    FileUtils.mkdir_p(File.dirname(dest_changelog))
    File.new(dest_changelog, "wb", 0644).tap do |changelog|
      Zlib::GzipWriter.new(changelog, Zlib::BEST_COMPRESSION).tap do |changelog_gz|
        if attributes[:deb_changelog]
          logger.info("Writing user-specified changelog", :source => attributes[:deb_changelog])
          File.new(attributes[:deb_changelog]).tap do |fd|
            chunk = nil
            # Ruby 1.8.7 doesn't have IO#copy_stream
            changelog_gz.write(chunk) while chunk = fd.read(16384)
          end.close
        else
          logger.info("Creating boilerplate changelog file")
          changelog_gz.write(template("deb/changelog.erb").result(binding))
        end
      end.close
    end # No need to close, GzipWriter#close will close it.

    attributes.fetch(:deb_init_list, []).each do |init|
      name = File.basename(init, ".init")
      dest_init = File.join(staging_path, "etc/init.d/#{name}")
      FileUtils.mkdir_p(File.dirname(dest_init))
      FileUtils.cp init, dest_init
      File.chmod(0755, dest_init)
    end

    attributes.fetch(:deb_default_list, []).each do |default|
      name = File.basename(default, ".default")
      dest_default = File.join(staging_path, "etc/default/#{name}")
      FileUtils.mkdir_p(File.dirname(dest_default))
      FileUtils.cp default, dest_default
      File.chmod(0644, dest_default)
    end

    attributes.fetch(:deb_upstart_list, []).each do |upstart|
      name = File.basename(upstart, ".upstart")
      dest_upstart = staging_path("etc/init/#{name}.conf")
      FileUtils.mkdir_p(File.dirname(dest_upstart))
      FileUtils.cp(upstart, dest_upstart)
      File.chmod(0644, dest_upstart)

      # Install an init.d shim that calls upstart
      dest_init = staging_path("/etc/init.d/#{name}")
      FileUtils.mkdir_p(File.dirname(dest_init))
      FileUtils.ln_s("/lib/init/upstart-job", dest_init)
    end

    args = [ tar_cmd, "-C", staging_path, compression ] + data_tar_flags + [ "-cf", datatar, "." ]
    safesystem(*args)

    # pack up the .deb, which is just an 'ar' archive with 3 files
    # the 'debian-binary' file has to be first
    with(File.expand_path(output_path)) do |output_path|
      ::Dir.chdir(build_path) do
        safesystem("ar", "-qc", output_path, "debian-binary", "control.tar.gz", datatar)
      end
    end
  end