#!/usr/bin/env ruby

filename = ARGV.last

unless filename
  warn "Please supply a filename"
  exit 1
end

content = File.read(filename)

refs = content.scan(/\[\d+\]:/)

unless refs == refs.uniq
  warn "Error: duplicate refs detected"
  exit
end

links = content.scan(/\[\d+\]/).uniq

links.zip(1..).each do |old_id, new_id|
  content.gsub! old_id, "[$$#{new_id}]"
end

content.gsub!("$$", "")

File.write(filename, content)
