#!/xhbin/bash

#
# Moss
# 
# Run moss scripts on assignment, generate all files needed for detect
# cheating
#
# Commandline syntax:
# ./Moss <assign> <language> [-b <basefile>] [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.
#
# Copyright(C) 2009-2010 ISG
#

assign=''                      # assignment
lang=''                        # language
thershold1=5                   # thershold1, could be specified by argument 2
thershold2=''                  # thershold2, could be specified by argument 3
extra=''                       # extra arguments provided to Moss script

if [ ${#} -lt 2 -o ${#} -gt 6 ]; then
# Usage
    echo "Usage: ./Moss <assign> <language> [ -b base ] [ thershold1 [ thershold 2] ]"
    echo ""
    echo "      assign:  the assignment number"
    echo "    language:  the language used in the assignment"
    echo "        base:  full path to base file provided by instructors"
    echo "  thershold1:  the minimum similarity to be included in the result"
    echo "               default: 5"
    echo "  thershold2:  if this is set, then the file generated will be"
    echo "               filtered with double thershold."
    echo "               See man moss_makereadable for more details on thershold"
    echo ""
    echo "Examples:"
    echo "./Moss a01 scheme"
    echo "./Moss 1 c -b ${HOME}/samples/bst.c 10"
    echo "./Moss 05 java 10 20"
    exit -1
elif [ ${#} -ge 3 ]; then
# Parse arguments
    assign=${1}
    shift 1
    lang=${1}
    shift 1
    if [ ${#} -gt 1 ]; then
	extra=${1}
	test ${extra} = "-b"
	if [ ${?} = 0 ]; then
	    shift 1
	    extra="${extra} ${1}"
	    shift 1
	fi
    fi
    if [ ${#} -gt 0 ]; then
	thershold1=${1}
	shift 1
	if [ ${#} -gt 0 ]; then
	    thershold2=${1}
	    shift 1
	fi
    fi
fi

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

SOURCE=`yes d | /u/isg/bin/runMoss ${assign} "-l ${lang} ${extra} -d" | grep 'http://moss.stanford.edu/results/'`

# For some reason the following line does not work.
#
# SOURCE=`perl -e '$count=0; while (1) { print "r\nRENAMED" . ++$count . "\n"; }' | /u/isg/bin/runMoss ${assign} "-l ${lang} -d" | grep 'http://moss.stanford.edu/results/'`

# 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 ${assign}_${lang}_moss.html with thershold ${thershold1} ${thershold2}"


/u/isg/bin/moss_makereadable ${SOURCE} ${thershold1} ${thershold2} > ${assign}_${lang}_moss.html
# Read man moss_makereadable

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

echo "Generating grouping file to ${assign}_mossgrp.txt"

echo -n 'Source url: ' > ${assign}_mossgrp.txt
echo ${SOURCE} >> ${assign}_mossgrp.txt

/u/isg/bin/moss_getgroups ${assign}_${lang}_moss.html >> ${assign}_${lang}_mossgrp.txt
# Read man moss_getgroups

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

echo "Done."

exit 0