#!/usr/bin/ruby
# frozen_string_literal: true
# This is meant to be run with the output of detect_duplicate_addrs on stdin
# The assumption is that some logging will dump that, and then someone will
# run this after looking into why
# Theoretically they could be piped together directly for automated fixing
require 'redis'
redis = Redis.new
customer_id = ENV['DEFAULT_CUSTOMER_ID']
unless customer_id
puts "The env-var DEFAULT_CUSTOMER_ID must be set to the ID of the customer who will receive the duplicated addrs, preferably a support customer or something linked to notifications when stray money is sent to these addresses"
exit 1
end
STDIN.each_line do |line|
match = line.match(/^(\w+) is used by the following \d+ keys: (.*)/)
unless match
puts "The following line can't be understood and is being ignored"
puts " #{line}"
next
end
addr = match[1]
keys = match[2].split(" ")
# This is the customer ID of the support chat
# All duplicates are moved to the support addr so we still hear when people
# send money there
redis.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
keys.each do |key|
redis.srem(key, addr)
end
end