#!/xhbin/bash

#
# Moss
# 
# Run moss scripts on assignment, generate all files needed for detect
# cheating
#
# Commandline syntax:
# ./Moss <assign> <language> [OPTIONS...] [thershold1 [thershold2]]
#
# Example:
# ./Moss 1 scheme 10
# ./Moss a1 c 5 10
# ./Moss a02 c -b ${HOME}/samples/tree.c
#
# Will generate <assign>_<language>_moss.shtml for details and <assign>_<language>_mossgrp.txt
# for a brief.
#
# Dependancies: runMoss moss_makereadable moss_getgroups
#
# Oct. 28, 2009: Created by CS 136 Tutor Ze Long
# Mar. 23, 2010: Added support for base files. - CL
# Mar. 26, 2010: Added support for optional verbose mode,
#                courtesy to Terry. - CL
#
# TODO:
#  - Verbose mode option.
#
# Copyright(C) 2009-2010 ISG
#

assign=''                      # assignment
lang=''                        # language
thershold=''                   # thershold
extra=''                       # extra arguments provided to Moss script
verbose=1

if [ ${#} -lt 2 -o ${#} -gt 9 ]; then
# Usage
    echo "Usage: ./Moss <assign> <language> [OPTIONS...]"
    echo "Arguments:"
    echo "  assign         the assignment number"
    echo "  language       the language used in the assignment"
    echo "Options:"
#    echo "  -v             Show output from runMoss"
    echo "  -b <base-dir>  full path to the directory containing files provided"
    echo "                 by instructors"
    echo "  -t <threshold> the minimum similarity to be included in the result"
    echo "                 Atmost two threshold can be specified. default: 5"
    echo "                 See man moss_makereadable for more details on thershold"
    echo "Examples:"
    echo "./Moss a01 scheme"
    echo "./Moss 1 c -b ${HOME}/samples 10"
    echo "./Moss 05 java 10 20"
    exit 1
else
# Parse arguments
    assign=${1}
    shift 1
    lang=${1}
    shift 1
    while [ ${#} -gt 0 ]; do
    case ${1} in
#    "-v")
#	shift 1
#	verbose=1
#	;;
    "-b")
        shift 1
	if [ ${#} -gt 0 ]
	    then
	    for i in ${1}/*; do
		extra="${extra} -b ${i}"
	    done
	    shift 1
	else
	    echo "Expect base dir"
	    exit 1
	fi
	;;
    "-t")
	shift 1
	if [ ${#} -gt 0 ]
	    then
	    thershold="${thershold} ${1}"
	    shift 1
	else
	    echo "Expect thershold"
	    exit 1
	fi
	;;
	esac
    done
fi

if [ ! -s thershold ]; then
    thershold=5
fi

# Define output files
#
readonly OUTFILE="${assign}_${lang}_moss.html"
readonly GROUP_OUTFILE="${assign}_${lang}_mossgrp.txt"

echo "Running Moss on Assignment ${assign} with language ${lang}"

# For duplication of the output of runMoss
tmpfile="/tmp/moss.$$"
mkfifo "$tmpfile"
chmod 600 "$tmpfile"
(
    { while read var; do
	    printf "%s\n" "$var"
	    done
    } < "$tmpfile"
)&

#if [ verbose = 1 ]; then
#    outputTo='| tee "$tmpfile"'
#else
#    outputTo=""
#fi

PERLSCRIPT=( 'perl' '-e' '$count=0; while (1) { print "r\nRENAMED" . ++$count . "\n"; }' )
SOURCE=`"${PERLSCRIPT[@]}" | /u/isg/bin/runMoss ${assign} "-l ${lang} ${extra} -d" | tee "$tmpfile" | grep 'http://moss.stanford.edu/results/'`

rm "$tmpfile"

# See Twiki entry: mossscripts

if [ -z "${SOURCE}" -o ${?} != 0 ]; then
    echo "Error when running runMoss on assignment ${assign}"
    exit 1
fi

echo "Result url: ${SOURCE}"
echo "Generating readable output to ${OUTFILE} with thershold ${thershold}"

/u/isg/bin/moss_makereadable ${SOURCE} ${thershold} > ${OUTFILE}
# Read man moss_makereadable

if [ ${?} != 0 ]; then
    echo "Error when creating readable output."
    exit 1
fi

echo "Generating grouping file to ${GROUP_OUTFILE}"

echo -n 'Source url: ' > "${GROUP_OUTFILE}"
echo ${SOURCE} >> "${GROUP_OUTFILE}"

/u/isg/bin/moss_getgroups ${OUTFILE} >> ${GROUP_OUTFILE}
# Read man moss_getgroups

if [ ${?} != 0 ]; then
    echo "Error when trying to group."
    exit 1
fi

echo "Done : ${assign} ${lang}"

exit 0