#! /bin/bash

# Run this from hwsubmit/core directory for assignments with .c submissions that should be executable.
# Process homework $1 by making subdirectory of .graded for each student and moving .c file there if it exists.
# Then cd into the subdirectory to compile and/or execute.
# Use $2 and following as extra gcc flags for assembly.
# Can follow on invocation of unzip+file.

# $var:t    ${var##*/}
# $var:h    ${var%/*}
# $var:e    ${var##*.}
# $var:r    ${var%.*}

fullprogname=$0
fullprogname=`echo $fullprogname | sed 's+^\([^/]\)+./\1+'`
progdir=${fullprogname%/*}
progname=${fullprogname##*/}
stage="`expr $progname : 'hwto\(.*\)'`"

hwno=$1; shift

for stud in *; do
  mkdir -p $stud/.graded/$hwno
  if [[ -e $stud/$hwno.c ]]; then
    mv -i $stud/$hwno.c $stud/.graded/$hwno
  fi
  pushd $stud/.graded/$hwno
  case $stage in 
    assembly) gcc -S $* -o $hwno.s $hwno.c >&$hwno.errors ;;
    executable)
      gcc -o $hwno $hwno.c >&$hwno.errors
      ;;
    execute)
      gcc -o $hwno $hwno.c >&$hwno.errors
      echo | ./$hwno > $hwno.out
      ;;
  esac
  popd
done
