import sys,os

def init():
  global i, year, count, cur_status, years, nyears, res_dir, ff

  i = 0
  year = 1995
  count = 0
  cur_status = 0

  with open("%s/results/CC-MAIN-2019-35/warc_lmhx/years.txt"%os.getenv("HOME"),"r") \
        as yf:
    years=[int(l) for l in yf]

  nyears = len(years)
  ff = [None] * (nyears + 1)
  res_dir = sys.argv.pop(1)
  ff[nyears] = open("%s/oob"%res_dir,"bw")

def finish():
  global ff, i, count, cur_status
  ff[i].write(b'%d\t%d\n'%(count, cur_status))

def cbf(status,lastmod):
  global cur_status, count, i, year, nyears
  stat = int(status)
  lm = int(lastmod)
  print(i, year, cur_status, count, stat, lm, file = sys.stderr)
  while (not year == 0) and (lm > years[i]):
    # change of year
    if count != 0:
      ff[i].write(b'%s\t%d\n'%(count, cur_status))
    if i < nyears:
      i += 1
      year += 1
      count = 0
      cur_status = 0
      if i == nyears:
        year = 0
        break
    if not ff[i]:
      ff[i] = open("%s/%s"%(res_dir, year),"bw")
  if stat == cur_status:
    count += 1
  else:
    ff[i].write(b'%d\t%d\n'%(count, cur_status))
    cur_status = stat
    count = 1
  
  
