#! /usr/bin/gawk -f
# Last edited on 2011-12-12 07:18:15 by stolfilocal

# Reads from "stdin" a log file as produced by "fetch-student-files".
# Outputs a list of the form "{URLDIR} {FILEDATE} {TPNAME}/{AUCS}/{RA}/{FILENAME}"
# where {AUCS} is "aula" or "casa", {URLDIR} is the URL directory
# in students.ic.unicamp.br where the file was fetched from,
# {FILEDATE} is the file's date as shown by {ls -l}.

# For the file "main.pov", shows the date before the camera/lights cleanup. 
# Also lists the file "main-orig.pov" which should be a copy of "main.pov"
# created before the camera cleanup.

BEGIN { 
  ra = "";
  this_yr = strftime("%Y");
  n = split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month_tag);
  split("", month_num);
  for (i = 1; i <= n; i++) { month_num[month_tag[i]] = sprintf("%02d", i); }
}

/^[=][=][=][ ][0-9]+[ ][=]/ { ra = $2; us = $4; next; } 

/^ln: creating symbolic link.*[\/](Makefile|missing|absent|title|camlight|rausrs)/ { next; }

/^http:/ { 
  url = $1; tra = $3;
  gsub(/^http:\/\/www.students.ic.unicamp.br\//, "", url);
  gsub(/\/[*]$/, "", url);
  next;
}

/^PATH = / { next; }

/^cmd = / { next; }

/^[`][a-z]+[0-9]+\/[a-z]+\/wget-log.txt['] ->/ { next; }

/^mkdir: created/ { next; }

/^Fetching ra=/ { next; }

/^fetching [a-z]+[0-9]+ [()]20/ { tp = $2; next; }

/^fetching files main.pov main.png/ { next; }

/^[*][*] file.*was not fetched/ { next; }

/^[*][*] file.*already exists, not fetched/ { next; }

/^removing camera and lights/ { next; }

/^fixing files/ { next; }

/^Fixing / { next; }

/^Done [()]see / { next; }

/^done[.]/ { next; }

/[-][-rwx]+[ \t][0-9]+[ \t][a-z0-9]+[ \t][a-z0-9]+[ \t]/ { 
  ma = $6; dy = $7; hm = $8; fn = $9;
  mn = month_num[ma]
  if (hm ~ /^[0-9][0-9][:][0-9][0-9]$/)
    { yr = this_yr; gsub(/[:]/, "", hm); hm = (hm "00"); }
  else if (hm ~ /^[12][09][0189][0-9]$/)
    { yr = hm; hm = "000000"; }
  else
    { printf "  ** BUG hm = %s «%s»\n", hm, $0 > "/dev/stderr"; }
    
  if (tra != substr(fn, 1, length(tra)))
    { printf "  ** BUG fn = %s tra = %s «%s»\n", fn, tra, $0 > "/dev/stderr"; }
    
  # List file that was fetched:
  printf "%-30s %s-%02d-%02d-%s %s\n", url, yr, mn, dy, hm, fn;
  next
}

/^[a-z]+[0-9]+\/[a-z]+\/[0-9]+\/main.(png|pov) ->/ {
  fno = $1; 
  fnn = $3;
  # printf "  fno = %s fnn = %s\n", fno, fnn > "/dev/stderr";
  if (fn != fno)
    { printf "  ** BUG fno = %s fn = %s «%s»\n", fno, fn, $0 > "/dev/stderr"; }
  if (tp != substr(fno, 1, length(tp)))
    { printf "  ** BUG fno = %s tp = %s «%s»\n", fno, tp, $0 > "/dev/stderr"; }
  next;
}

// { printf "  ** IGNORED «%s»\n",$0 > "/dev/stderr"; }

END{
  printf "Done.\n" > "/dev/stderr";
}