#! /bin/bash
# Last edited on 2010-07-08 18:37:37 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="fetch all labwork files for a single day and student"
PROG_HELP=(
  "${PROG_NAME} [-check] {LABDIR} {RA} {SITE} {USR} {EXDIR} [{FILE}..]"
)
PROG_INFO=(
  "\nNAME"
  "\n  ${PROG_NAME} - ${PROG_DESC}."
  "\n"
  "\nSYNOPSIS"
  "\n  ${PROG_HELP[@]}"
  "\n"
  "\nDESCRIPTION"
  "\n"
  "Copies the specified homework source {FILE}s from \n"
  "\"http://{SITE}/~{USR}/{EXDIR}\"\n"
  "to directory {LABDIR}/{RA}/.\n"
  "\n"
  "The dierctory {LABDIR} must be \"*/casa\" or \"*/aula\".\n"
  "\n"
  "Creates write-protected \"-orig\" copies for those files in \n"
  "directory {LABDIR}/{RA}/.\n"
  "\n"
  "Appends report messages to file \"{LABDIR}/wget-log.txt\".\n"
  "\n"
  "If \"-check\" is given, only checks which files exist in \n"
  "the remote directory; does not fetch anything, and does not\n"
  "write to the log file.\n"
)

export PATH="tools:../tools:../../tools:../../../tools:../../../../tools:${PATH}"

checkop=0;
while [[ ( $# -ge 1 ) && ( "/$1" =~ "/-.*" ) ]]; do
  if [[ ( $# -ge 1 ) && ( "/$1" == "/-check" ) ]]; then
    checkop=1; shift; 
  else
    echo "usage: ${PROG_HELP}" 1>&2; exit 1;
  fi
done

if [[ $# -lt 4 ]]; then
  echo "usage: ${PROG_HELP}" 1>&2; exit 1
fi

labdir="$1"; shift
ra="$1"; shift
site="$1"; shift
usr="$1"; shift
exdir="$1"; shift

if [[ $# -lt 1 ]]; then
  echo "no files to fetch?" 1>&2;
  echo "usage ${PROG_HELP}" 1>&2; exit 1
fi

files=( "$@" )

if [[ ! ( -d ${labdir} ) ]]; then
  echo "directory ${labdir} not found" 1>&2; exit 1
fi

if [[ ( "/${labdir##*/}" != "/aula" ) && ( "/${labdir##*/}" != "/casa" ) ]]; then
  echo "directory ${labdir} should be */aula or */casa" 1>&2; exit 1
fi

# Note: this line is important for {extract-fetched-files-from-log}
echo "=== ${ra} = ${usr} =================================================="

# Create hand-in subdirectory for student
todir="${labdir}/${ra}"
if [[ ! ( -d ${todir} ) ]]; then mkdir ${todir}; fi
if [[ ! ( -e ${todir}/Makefile ) ]]; then
  ( cd ${todir} && ln -s ../Makefile )
fi
if [[ ! ( -e ${todir}/camlight.inc ) ]]; then
  ( cd ${todir} && ln -s ../camlight.inc )
fi

# Write {wget} log file to student's directory:
wlog="${todir}/wget-log.txt"

# Fetch student files, if any:
wpref="http://${site}/~${usr}/${exdir}"
echo "${wpref}/* -> ${todir} 1>&2"
for fname in ${files[@]} ; do
  if [[ ${checkop} -gt 0 ]]; then
    wget -nv --spider "${wpref}/${fname}"; 
  else
    otf="${todir}/${fname}"
    svf="${otf%.*}-orig.${otf##*.}"
    if [[ -a ${otf} ]]; then
      echo "** file ${otf} already exists, not fetched" 2>> ${wlog};
    elif [[ -a ${svf} ]]; then
      echo "** file ${svf} already exists, ${otf} not fetched" 2>> ${wlog};
    else
      wget -nv "${wpref}/${fname}" -O ${otf} 2>> ${wlog};
      if [[ ! -s ${otf} ]] ; then
        # Failed to get file, or file has zero length
        echo "** file ${fname} was not fetched or is empty" 2>> ${wlog};
        rm -f ${otf}
      else
        ls -l ${otf}
        echo "${otf} -> ${svf} 1>&2"
        cp -pi ${otf} ${svf}
        chmod a-w ${svf}
        ls -l ${svf}
        if [[ ${otf} == *.pov ]]; then
          echo "removing camera and lights from ${otf} ..."
          filter-files `which remove-cam-lights-and-crunch` ${otf}
        fi
      fi
    fi
  fi
done