# File lib/fpm/package/deb.rb, line 384
  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 attributes[:source_date_epoch].nil? and not attributes[:source_date_epoch_default].nil?
      attributes[:source_date_epoch] = attributes[:source_date_epoch_default]
    end
    if attributes[:source_date_epoch] == "0"
      logger.error("Alas, ruby's Zlib::GzipWriter does not support setting an mtime of zero.  Aborting.")
      raise "#{name}: source_date_epoch of 0 not supported."
    end
    if not attributes[:source_date_epoch].nil? and not ar_cmd_deterministic?
      logger.error("Alas, could not find an ar that can handle -D option. Try installing recent gnu binutils. Aborting.")
      raise "#{name}: ar is insufficient to support source_date_epoch."
    end
    if not attributes[:source_date_epoch].nil? and not tar_cmd_supports_sort_names_and_set_mtime?
      logger.error("Alas, could not find a tar that can set mtime and sort.  Try installing recent gnu tar. Aborting.")
      raise "#{name}: tar is insufficient to support source_date_epoch."
    end

    attributes.fetch(:deb_systemd_list, []).each do |systemd|
      name = File.basename(systemd, ".service")
      dest_systemd = staging_path("lib/systemd/system/#{name}.service")
      mkdir_p(File.dirname(dest_systemd))
      FileUtils.cp(systemd, dest_systemd)
      File.chmod(0644, dest_systemd)

      # set the attribute with the systemd service name
      attributes[:deb_systemd] = name
    end

    if script?(:before_upgrade) or script?(:after_upgrade) or attributes[:deb_systemd]
      puts "Adding action files"
      if script?(:before_install) or script?(:before_upgrade)
        scripts[:before_install] = template("deb/preinst_upgrade.sh.erb").result(binding)
      end
      if script?(:before_remove) or attributes[:deb_systemd]
        scripts[:before_remove] = template("deb/prerm_upgrade.sh.erb").result(binding)
      end
      if script?(:after_install) or script?(:after_upgrade) or attributes[:deb_systemd]
        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
      if script?(:after_purge)
        scripts[:after_purge] = template("deb/postrm_upgrade.sh.erb").result(binding)
      end
    end

    # There are two changelogs that may appear:
    #   - debian-specific changelog, which should be archived as changelog.Debian.gz
    #   - upstream changelog, which should be archived as changelog.gz
    # see https://www.debian.org/doc/debian-policy/ch-docs.html#s-changelogs

    # Write the changelog.Debian.gz file
    dest_changelog = File.join(staging_path, "usr/share/doc/#{name}/changelog.Debian.gz")
    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 not attributes[:source_date_epoch].nil?
          changelog_gz.mtime = attributes[:source_date_epoch].to_i
        end
        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.

    # Write the changelog.gz file (upstream changelog)
    dest_upstream_changelog = File.join(staging_path, "usr/share/doc/#{name}/changelog.gz")
    if attributes[:deb_upstream_changelog]
      File.new(dest_upstream_changelog, "wb", 0644).tap do |changelog|
        Zlib::GzipWriter.new(changelog, Zlib::BEST_COMPRESSION).tap do |changelog_gz|
            if not attributes[:source_date_epoch].nil?
              changelog_gz.mtime = attributes[:source_date_epoch].to_i
            end
            logger.info("Writing user-specified upstream changelog", :source => attributes[:deb_upstream_changelog])
            File.new(attributes[:deb_upstream_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
        end.close
      end # No need to close, GzipWriter#close will close it.
    end

    if File.exists?(dest_changelog) and not File.exists?(dest_upstream_changelog)
      # see https://www.debian.org/doc/debian-policy/ch-docs.html#s-changelogs
      File.rename(dest_changelog, dest_upstream_changelog)
    end

    attributes.fetch(:deb_init_list, []).each do |init|
      name = File.basename(init, ".init")
      dest_init = File.join(staging_path, "etc/init.d/#{name}")
      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}")
      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_init = staging_path("etc/init.d/#{name}")
      name = "#{name}.conf" if !(name =~ /\.conf$/)
      dest_upstart = staging_path("etc/init/#{name}")
      mkdir_p(File.dirname(dest_upstart))
      FileUtils.cp(upstart, dest_upstart)
      File.chmod(0644, dest_upstart)

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

    attributes.fetch(:deb_systemd_list, []).each do |systemd|
      name = File.basename(systemd, ".service")
      dest_systemd = staging_path("lib/systemd/system/#{name}.service")
      mkdir_p(File.dirname(dest_systemd))
      FileUtils.cp(systemd, dest_systemd)
      File.chmod(0644, dest_systemd)
    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

    args = [ tar_cmd, "-C", staging_path, compression ] + data_tar_flags + [ "-cf", datatar, "." ]
    if tar_cmd_supports_sort_names_and_set_mtime? and not attributes[:source_date_epoch].nil?
      # Use gnu tar options to force deterministic file order and timestamp
      args += ["--sort=name", ("--mtime=@%s" % attributes[:source_date_epoch])]
      # gnu tar obeys GZIP environment variable with options for gzip; -n = forget original filename and date
      args.unshift({"GZIP" => "-9n"})
    end
    safesystem(*args)

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