#!/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 create a directory named <assign>_<lang> to hold the moss result.
# Will generate readableIndex.shtml for details and group.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
# Apr. 13, 2010: Porting to new runMoss script. -CL
#
# TODO:
#  - Verbose mode option.
#
# Copyright(C) 2009-2010 ISG
#

readonly runMossScript='/u/isg/bin/runMoss'
readonly makeReadable='/u/isg/bin/moss_makereadable'
readonly makeGroup='/u/isg/bin/moss_getgroups'

assign=''                      # assignment
lang=''                        # language
thershold=''                   # thershold
extra=''                       # extra arguments provided to Moss script
holdDir=''                     # directory to hold the result of moss scripts
verbose=1

if [ ${#} -lt 2 -o ${#} -gt 9 ]; then
# Usage
    echo "Usage: ./Moss <assign> <language> [OPTIONS...]"
    echo "Run moss scripts on the specified assignment."
    echo "Will create a directory named <assign>_<lang> to hold the moss result."
    echo 
    echo "Arguments:"
    echo " assign         the assignment number"
    echo " language       the language used in the assignment"
    echo
    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
    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 [ -z ${thershold} ]; then
    thershold=5
fi

# Setup the hold dir
holdDir="./${assign}_${lang}"

if [ ! -e ${holdDir} ]; then
    mkdir ${holdDir}
fi

pushd .

cd ${holdDir}

# Define output files
#
readonly OUTFILE="readableIndex.html"
readonly GROUP_OUTFILE="group.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=`perl -e '$count=0; while (1) { print "r\nRENAMED" . ++$count . "\n"; }' | ${runMossScript} ${assign} "-l ${lang} ${extra} -d" | tee "$tmpfile" | /usr/bin/tail -n 1`

rm "$tmpfile"

# See Twiki entry: mossscripts

if [ ${?} != 0 -o ! -s "${SOURCE}" -o ! -e "${SOURCE}" ]; 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}"

${makeReadable} ${SOURCE} ${thershold} > ${OUTFILE}
# Read man moss_makereadable

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

# Modify the relative path in the readable file
dirResult=`dirname ${SOURCE}`
sed -i -e "s:match\\([0-9]*\\).html:${dirResult}/match\\1.html:g" ${OUTFILE}

echo "Generating grouping file to ${GROUP_OUTFILE}"

${makeGroup} ${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