#!/bin/bash

# Copyright (c) Gene Cooperman 2001, 2004
# Permission to use is freely granted as long as this copyright notice remains.
# This software has no warranty.

if test "$1" = ""; then
  echo "USAGE:  "`basename $0` FILE
  echo "        (USAGE for instructor):  "`basename $0`' [--create | --is-late]'
  echo "          Rename this script to submit-COURSE-hwWHICH."
  echo "          For example, submit-csu660-hw2 will"
  echo "            create and manage /net/course/cs3650/homework/hw2.handin"
  echo "          Similarly, submit-csg389-hw1-sect1 will"
  echo "            create and manage /net/course/cs3650/homework/hw1-sect1.handin"
  exit 0
fi

# Directories: /net/course/$course/homework/$hw.handin
#              /net/course/$course/homework/$hw.handin-late
#  where $course and $hw are numbers, e.g.:  /net/course/cs3650/homework/hw3.handin

# Set handin directory to:  go-r,go+wx,+t
# For example:  chmod go-r,go+wx,+t hw3.handin
# When a homework is late, set handin directory to:  go-wx,-t
#                 and set handin-late directory to:  go-r,go+wx,+t
# One can do this automatically as:  (sleep XXX; chmod ...)
#   if you trust the computer to stay up.

# Script will guess homework num based on name:  submit-$course-hw#-...
# e.g.: change name to:  submit-1100-hw4 for hw4 of com1100
#       Currently only guesses hw# and nothing else.

course=`basename $0 | sed -e 's%^submit-%%' | sed -e 's%-.*%%'`
# if test $course -lt 100 -o $course -gt 9999; then
#   echo "Can't find correct course number."
#   echo "  expecting script with name:  submit-COURSE-hwHW_NUM-..."
#   exit 1
# fi
# course_alt=1355
# course=1130-2

if echo `basename $0` | grep -q short-hw; then
  hw=short-hw`basename $0 | sed -e 's%^.*-short-hw%%'`
else
  hw=hw`basename $0 | sed -e 's%^.*-hw%%'`
fi
dir=/net/course/$course/homework/$hw.handin

# Handle --create
if test "$1" = --create; then
  echo "Creating $hw.handin and initializing with correct permissions"
  if test ! -r /net/course/$course/homework/$hw.handin; then
    if ls -lgd . | grep -qv '^......s'; then
      echo "Parent directory doesn't have group sticky bit set. Do: chmod g+s ."
    fi
    mkdir -p /net/course/$course/homework/$hw.handin
    chmod go-r,go+wx,+t /net/course/$course/homework/$hw.handin
    # Group should include instructor and grader
    chmod g+r /net/course/$course/homework/$hw.handin
  elif test ! -d /net/course/$course/homework/$hw.handin; then
    echo "$hw.handin already exists and is not a directory"
  else
    echo "$hw.handin already exists"
  fi

  touch /tmp/$USER.tmp
  if chgrp $course /tmp/$USER.tmp > /dev/null 2>&1; then
    echo "Setting group permission for group $course"
    if test -r /net/course/$course/homework/$hw.handin; then
      chgrp $course /net/course/$course/homework/$hw.handin
      chmod g+rwx /net/course/$course/homework/$hw.handin
    fi
  fi
  rm -f /tmp/$USER.tmp
  exit 0
fi

# Handle --is-late
if test "$1" = --is-late; then
  echo "Creating $hw.handin-late and modifying permissions"
  if test ! -r /net/course/$course/homework/$hw.handin-late; then
    mkdir /net/course/$course/homework/$hw.handin-late
    chmod go-wx,-t /net/course/$course/homework/$hw.handin
    # Group should include instructor and grader
    chmod g+rwx /net/course/$course/homework/$hw.handin
    chmod go-r,go+wx,+t /net/course/$course/homework/$hw.handin-late
    # Group should include instructor and grader
    chmod g+r /net/course/$course/homework/$hw.handin-late
  elif test ! -d /net/course/$course/homework/$hw.handin-late; then
    echo "$hw.handin-late already exists and is not a directory"
  else
    echo "$hw.handin-late already exists; changing permissions only"
    chmod go-wx,-t /net/course/$course/homework/$hw.handin
    chmod go-r,go+wx,+t /net/course/$course/homework/$hw.handin-late
  fi
  touch /tmp/$USER.tmp
  if chgrp $course /tmp/$USER.tmp > /dev/null 2>&1; then
    if test -r /net/course/$course/homework/$hw.handin-late; then
      chgrp $course /net/course/$course/homework/$hw.handin-late
      chmod g+rwx /net/course/$course/homework/$hw.handin-late
      chgrp $course /net/course/$course/homework/$hw.handin
      chmod g+rwx /net/course/$course/homework/$hw.handin
    fi
  fi
  rm -f /tmp/$USER.tmp
  exit 0
fi

if test ! -w $dir -a -d ${dir}-late; then
  dir=${dir}-late
  echo ""
  echo "This homework appears to be late."
  echo "  It will be submitted to $dir ."
  echo "If the homework is in fact on time, please speak to the"
  echo "  instructor about a bug in this script."
fi

if test $# != 1; then
  echo ""
  echo Usage:  $0 FILENAME
  echo "  Places user file in $dir/USERNAME-FILENAME.RANDOM"
  echo "    where RANDOM makes it difficult for others to find the file"
  echo "  The preferred format for FILENAME is:  FILE.tar.gz"
  echo ""
  exit 1
fi

from_file=$1
to_file=$dir/`whoami`-`basename $from_file`.$RANDOM$RANDOM
echo dir: $dir
echo to_file: $to_file

if test ! -f $from_file; then
  echo "Sorry.  I can't find the file: $from_file"
  echo "Please try again."
fi

echo ""
echo "If your file is successfully submitted, you will see"
echo "  a confirmation message at the end of this command."
echo ""

if cp $from_file $to_file; then
  chmod go+r $to_file
  echo "Your file, $from_file, has been successfully submitted"
  echo "  as file:  $to_file"
  echo ""
  echo "If you change your mind, you can delete the file by doing:"
  echo "  rm $to_file"
  echo ""
  # echo "Please remember to bring hardcopy of your program output"
  # echo "  to hand in during class."
  echo ""
elif ! test -d /net/course/$course; then
  echo "I can't find /net/course/$course"
  echo "Please check if this host is really at ccs.neu.edu."
elif ! test -d "$dir"; then
  echo "It appears that the instructor forgot to create"
  echo "  the handin directory.  Please send e-mail or speak to the"
  echo "  instructor, remiding him or her to create the handin directory."
  if test -d /net/course/$course; then
    echo ""
    echo Also sending mail to username '"'`stat -c %U /net/course/$course`'"' \
         for you.
cat <<EOF | mail -s "`basename $0`: handin directory does not exist." \
	`stat -c %U /net/course/$course`
    Please create the handin directory.
EOF
  fi
else
  echo "Unable to copy from $from_file to $to_file"
  echo "Please check that the file exists:  ls -l $from_file"
  echo ""
  echo "If you still have problems, please speak to the instructor."
  echo ""
  exit 1
fi

if test -f $to_file -a ! -s $to_file; then
  echo "****************************************************************"
  echo "* WARNING: THE FILE YOU SUBMITTED IS SIZE ZERO!                *"
  echo "*    Please check your file again, and consider re-submitting. *"
  echo "****************************************************************"
fi
