#! /bin/csh -f
# Last edited on 2003-12-10 11:13:01 by stolfi

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

set disc = mc930

# To be executed in a subdirectory "tpXX/casa" or "tpXX/aula"
# 
# 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.
# Requires a file "rausrs.txt" with the RA numbers and usernames
# of all students.
# 
# If "-check" is given, only checks which files exist, does not fetch 
# anything.

set check = 0;
while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-check" ) ) then
    set check = 1; 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

foreach rausr ( `cat rausrs.txt | egrep -v '^ *([#]|$)' | gawk '//{print $1 "." $2;}' ` ) 
  set ra = ${rausr:r}
  set usr = ${rausr:e}
  echo "=== ${ra} = ${usr} =================================================="
  
  # Create hand-in subdirectory for student
  set dir = "${ra}"
  if ( ! ( -d ${dir} ) ) mkdir ${dir}
  if ( ! ( -r ${dir}/Makefile ) ) then
    ( cd ${dir} && ln -s ../Makefile )
  endif
  
  # Fetch student files, if any:
  set pub = ~${usr}/public_html
  set expdir = ${pub}/${disc}/${mmdd}
  echo ${pub}
  if ( ! ( -d ${pub}/. ) ) then
    echo "** directory ${pub} not found"
    echo "${ra} : ${pub} not found" >> ${bugs}
  else if ( ! ( -d ${expdir} ) ) then
    echo "** ${expdir} not found **"
    echo "${ra} : ${expdir} not found" >> ${bugs}
  else if ( ! ( -r ${expdir}/main.pov ) ) then
    echo "** ${expdir}/main.pov not found **"
    echo "${ra} : ${expdir}/main.pov not found" >> ${bugs}
  else 
    if ( ${check} ) then
      ls -ld ${expdir}/*.{${exts}}
    else
      cp -aivu ${expdir}/*.{${exts}} $dir/
      ls -l $dir/*.{${exts}}
      foreach f ( `ls $dir/*.{${exts}} | egrep -v '[-]orig[.]'` )
        cp -pi $f ${f:r}-orig.${f:e}
      end
    endif
  endif
end