#!/bin/bash

#
# 
# Copyright (c) 2007-2012,
#   Lingxiao Jiang         <lxjiang@ucdavis.edu>
#   Ghassan Misherghi      <ghassanm@ucdavis.edu>
#   Zhendong Su            <su@ucdavis.edu>
#   Stephane Glondu        <steph@glondu.net>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
#

TOOVERWRITE=

clean()
{
	echo -n "...deleting intermediate vector files..."
	find "$SRC_DIR" -iname '*.vec' -print0 | xargs -0 -r --max-args 500 rm -f 
	echo "Done"
}

clean_log()
{
	echo -n "...deleting vgen logs..."
	find "$TIME_DIR" -iname 'vgen_*' -print0 | xargs -0 -r --max-args 500 rm -f 
	echo "Done"
	echo -n "...deleting html transforming logs..."
	rm -f "$TIME_DIR/`basename "$SRC2HTM_EXEC"`.log"
	echo "Done"
	echo -n "...deleting vector db files..."
	find "$VECTOR_DIR" -iname 'vdb_*' -print0 | xargs -0 -r --max-args 500 rm -f 
	echo "Done"
}

#vgen $1 $2: $1=min_token; $2=stride
vgen()
{
	if (($# != 2));
	then
		return 1
	fi

	echo -n "$FUNCNAME: $1 $2 ..."  # FUNCNAME: the current function name; maintained by Bash
	if [[ "$TOOVERWRITE" != "true" && -s "$VECTOR_DIR/vdb_$1_$2" ]];
	then
	  echo
	  echo "Warning: Vec file $VECTOR_DIR/vdb_$1_$2 exists. Delete it manually or use '$0 overwrite'."
	  echo "$FUNCNAME: $1 $2 ...Skip."
	else
	  echo -n > "$TIME_DIR/vgen_$1_$2"

#NOTE: "for .. in .." has trouble when a filename contains spaces; use "while read .." instead:
	  ( time \
	    find "$SRC_DIR" -iname "$FILE_PATTERN" | while read file;
#	    for file in `find "$SRC_DIR" -iname "$FILE_PATTERN"`;
	    do
		echo "Parsing $file" >> "$TIME_DIR/vgen_$1_$2"
		if [[ ! -s "${file}.vec" || "$TOOVERWRITE" = "true" ]];
		then
			"$VGEN_EXEC" -i "$file" -m $1 -t $2
		fi
	    done \
	  ) 1>>"$TIME_DIR/vgen_$1_$2" 2>&1

	  find "$SRC_DIR" -iname '*.vec' -print0 | xargs -0 --max-args=100 cat > "$VECTOR_DIR/vdb_$1_$2"
	  echo "Done. Log: $TIME_DIR/vgen_$1_$2"
        fi

	if [[ -s "$VECTOR_DIR/vdb_$1_$2" ]]; then
		return 0
	else
		return 1
	fi
}

# transform source code to html:
tohtml()
{
	  if ( which "$SRC2HTM_EXEC" >& /dev/null );
	  then
		  echo -n "Transforming source code to html..."
		  ( time \
		    find "$SRC_DIR" -iname "$FILE_PATTERN" | while read file;
	  	    do
			    if [[ ! -e "${file}.html" || "$TOOVERWRITE" = "true" ]];
			    then
				    echo "$SRC2HTM_EXEC $SRC2HTM_OPTS $file"
				    "$SRC2HTM_EXEC" "$SRC2HTM_OPTS" "$file"
			    fi
		    done \
		  ) 1>>"$TIME_DIR/`basename "$SRC2HTM_EXEC"`.log" 2>&1
		  echo "Done. Log: $TIME_DIR/`basename "$SRC2HTM_EXEC"`.log"
	  fi
}

. `dirname $0`/configure

if [[ $# -ge 1 ]]; then
	case "$1" in
		clean )
			clean
			exit $?
			;;
		clean_all )
			clean
			clean_log
			exit $?
		       	;;
		overwrite )
			TOOVERWRITE="true"
			;;
		* )
			echo "Usage: $0 [overwrite | clean | clean_all]"
			exit 1
			;;
	esac
fi

errcode=0
echo "$VGEN_EXEC $FILE_PATTERN"
echo 

for t in ${MIN_TOKENS};
do
	for s in ${STRIDE};
	do
		vgen $t $s
		retcode=$?
		if [[ $errcode -eq 0 ]]; then
			errcode=$retcode
		fi
		clean # remove .vec so not to intervene with the following vgens
		echo
	done
done

tohtml
exit $errcode

