namespace :debshots do
  desc "Remove duplicate images"

  task :remove_duplicate_images => :environment do
    Rails.logger = Logger.new(STDOUT)
    Rails.logger.level = Logger::INFO
    #Rails.logger.level = Logger::DEBUG

    Rails.logger.info "Removing packages with 'nil' screenshots"

    Screenshot.where(image_fingerprint: nil).each do |ss|
      Rails.logger.info "Destroying screenshot #{ss.id} of package #{ss.package.name} that has nil data"
      ss.destroy
    end

    Rails.logger.info "Removing packages with duplicate screenshot checksums"

    loop do
      destroyed = 0
      Screenshot.select(:package_id, :image_fingerprint).group(:package_id, :image_fingerprint, :created_at).having("count(*) > 1").each do |ss|
        # Look again for screenshots of this fingerprint.
        # This extra step may seem stupid but the above query lead to package_id=NULL results sometimes.
        del = Screenshot.find_by(image_fingerprint: ss.image_fingerprint)
        Rails.logger.info "Destroying duplicate screenshot #{del.id} of package #{del.package.name}"
        del.destroy
        destroyed += 1
      end
      break if destroyed == 0
    end
  end
end
