#!/bin/bash
# Run alvor test on a project
# ./alvorGen.sh {workspace} {project} enable_log

eclipse_executable=""

if [[ $eclipse_executable == "" ]]; then
  printf "Eclipse executable not set, modify the 'eclipse_executable' variable to set it!\n"
  exit 1
fi

cd "$2"

log="/dev/null" # logging disabled by default
if ! [ -z $3 ] && [ $3 == "enable_log" ]; then
  log="/dev/tty" # logging enabled
fi

printf '%s\n' "Creating files .project, .alvor and .classpath for project {${PWD##*/}}"
cat > "$2/.project" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>${PWD##*/}</name>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
		</buildCommand>
		<buildCommand>
			<name>com.googlecode.alvor.builder.AlvorBuilder</name>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>org.eclipse.jdt.core.javanature</nature>
		<nature>com.googlecode.alvor.builder.AlvorNature</nature>
	</natures>
</projectDescription>
EOF

cat > "$2/.alvor" << EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<alvor effortLevel="2">
  <hotspots>
    <hotspot argumentIndex="1" argumentTypes="*" className="java.sql.Connection" methodName="prepareStatement"/>
  </hotspots>
  <checkers>
    <checker checkerName="Generic-Syntax" driverName="" password="" url="" userName=""/>
  </checkers>
</alvor>
EOF

cat > "$2/.classpath" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
	<classpathentry kind="output" path="bin"/>
</classpath>
EOF

printf '%s\n' "Importing project {${PWD##*/}} to $1"
{
$eclipse_executable \
  -nosplash \
  -data "$1" \
  -application org.eclipse.cdt.managedbuilder.core.headlessbuild \
  -import     "$2" \
  -build      "${PWD##*/}"
} &> $log

printf '%s\n' "Building project {${PWD##*/}}"
{
$eclipse_executable \
  -nosplash \
  -application org.eclipse.jdt.apt.core.aptBuild \
  -data "$1"
} &> $log
