#!/usr/pkg/bin/ruby40
# prerequisite:
# gem install net-http-persistent
#
# dumps all bibxml for current, "Active" I-Ds in cache
# reasonably efficient after initial call if the output is retained
#
# requires Ruby 2.4 or above because of "liberal_parsing" option
#
# uses ENV["KRAMDOWN_REFCACHEDIR"] for where you want to have your bibxml3 data
#

require 'csv'
require 'fileutils'

begin
  require 'net/http/persistent'
rescue LoadError
  warn "*** please install net-http-persistent:"
  warn "   gem install net-http-persistent"
  warn "(prefix by sudo only if required)."
  exit 72                       # EX_OSFILE
end


TARGET_DIR = ENV["KRAMDOWN_REFCACHEDIR"] || (
  path = File.expand_path("~/.cache/xml2rfc")
  warn "*** set environment variable KRAMDOWN_REFCACHEDIR to #{path} to actually use the cache"
  path
)

FileUtils.mkdir_p(TARGET_DIR)
FileUtils.chdir(TARGET_DIR)

$http = Net::HTTP::Persistent.new name: 'allid'

KRAMDOWN_PERSISTENT_VERBOSE = true

      def get_and_write_resource_persistently(url, fn, age_verbose=false)
        t1 = Time.now
        response = $http.request(URI(url))
        if response.code != "200"
          raise "*** Status code #{response.code} while fetching #{url}"
        else
          File.write(fn, response.body)
        end
        t2 = Time.now
        warn "#{url} -> #{fn} (#{"%.3f" % (t2 - t1)} s)" if KRAMDOWN_PERSISTENT_VERBOSE
        if age_verbose
          if age = response.get_fields("age")
            warn "(working from a web cache, index is #{age.first} seconds stale)"
          end
        end
      end

CLEAR_RET = "\e[K\r" # XXX all the world is ECMA-48 (ISO 6429), no?

      def noisy(name)
        print "#{name}...#{CLEAR_RET}"
      end
      def clear_noise
        print CLEAR_RET
      end

ALL_ID2_SOURCE = "https://www.ietf.org/id/all_id2.txt"
ALL_ID2_COPY = ".all_id2.txt"

get_and_write_resource_persistently(ALL_ID2_SOURCE, ALL_ID2_COPY, true) unless ENV["KRAMDOWN_DONT_REFRESH_ALL_ID2"]
ix = File.read(ALL_ID2_COPY).lines.grep_v(/^#/).join

csv = CSV.new(ix, col_sep: "\t", liberal_parsing: true)

drafts = csv.read
active = drafts.select { |d| d[2] == "Active" }
active_names = active.map { |a| a[0] }
puts "#{active_names.size} active drafts"

active_names.each do |name|
  if name =~ /\Adraft-(.*)-(\d\d)\z/
    namepart = $1
    version = $2
    name0 = "reference.I-D.#{namepart}.xml"
    noisy(name0) if File.exist?(name0)
    name1 = "reference.I-D.draft-#{namepart}-#{version}.xml"
    if File.exist?(name1)
      noisy(name1)
      FileUtils.touch(name0)    # because name1 already exists, we believe name0 is fresh
    else
      begin
        url0 = "https://datatracker.ietf.org/doc/bibxml3/draft-#{namepart}.xml"
        get_and_write_resource_persistently(url0, name0) # get name0 first
        url1 = "https://datatracker.ietf.org/doc/bibxml3/draft-#{namepart}-#{version}.xml"
        get_and_write_resource_persistently(url1, name1) # then name1 to mark this as updated
      rescue => e
        warn "*** #{name0}: #{e}"
      end
    end
  else
    warn "*** Malformed draft name: #{name}"
  end
end
clear_noise
