#! /bin/csh -f
# Last edited on 2005-01-04 14:10:53 by stolfi

set usage = "$0 [-check] MM-DD"

set disc = mc930

# To be executed in a subdirectory "tpXX/casa" or "tpXX/aula"
# 
# Requires a file "rausrs.txt" with the RA numbers and usernames
# of all students.
# 
# Fetches the ".pov", ".inc", and ".gif/.png" files of all students,
# from ~[ra]NNNNNN/public_html/${disc}/MM-DD/ to ./NNNNNN,
# where NNNNNN is the student's RA number.
# Also creates copy of every file, named "*-orig.{pov,inc,png,gif}",
# in preparation of editing and re-rendering by the instructor.
# 
# If "-check" is given, only checks which files exist, does not fetch 
# anything.

set checkopt = ( );
while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-check" ) ) then
    set checkopt = ( "-check" ); shift; 
  else
    echo "usage" ${usage}"; exit 1
  endif
end

if ( $#argv != 1 ) then
  echo "usage" ${usage}"; exit 1
endif

set mmdd = "$1"; shift

set bugs = "bugs.txt"
set exts = "pov,inc,png,gif,dat,pas,c,pl,php3,tcl,sh,java"

if ( -r ${bugs} ) then
  mv -v ${bugs} ${bugs}~~
endif
touch ${bugs}

# Make sure that there are links to files peculiar to this homework:
foreach f ( \
  Makefile \
  missing.png missing-i.png \
  absent.png absent-i.png \
  rausrs.txt aus.txt title.txt \
)
  if ( ! -r ${f} ) ln -s ../${f}
end

set labdir = ../${pwd:t}
if ( ( "/${labdir:t}" != "/aula" ) && ( "/${labdir:t}" != "/casa" ) ) then
  echo "current directory should be */aula or */casa"; exit 1
endif

foreach rausr ( `cat rausrs.txt | egrep -v '^ *([#]|$)' | gawk '//{print $1 "." $2;}' ` ) 
  set ra = ${rausr:r}
  set usr = ${rausr:e}
  fetch-student-files ${checkopt} ${labdir} ${ra} ~${usr} ${disc}/${mmdd}
end