#!/bin/sh

CC=gcc
CPPC=g++
JAVAC=javac
JAVA=java
PYTH=python

# Set this variable to 1 to preserve generated files.
PRESERVE=0

MESSAGE='Hello, world!'

############################################################################## 
# C compilation							      	     #
##############################################################################
FILE='hello.c'
/bin/echo "#include <stdio.h>" > $FILE
/bin/echo "int main(void) {" >> $FILE
/bin/echo -E "printf(\"${MESSAGE}\n\");" >> $FILE
/bin/echo -e "}\n" >> $FILE

OUTFILE='c_hello'
`$CC -o $OUTFILE $FILE > /dev/null 2>&1`
if [ $? -eq 0 ]; then
	echo "-> C compilation succeeded."
	chmod +x $OUTFILE
	OUTPUT=`./$OUTFILE 2>&1 | grep -c "$MESSAGE"`
	if [ $OUTPUT -eq 1 ]; then
		echo "-> Compiled C code executed correctly."
	else
		echo "-> Compiled C code did not execute as expected!"
		echo "   Verify that $CC is working correctly."
	fi
else
	echo "-> C compilation failed! Make sure $CC is installed."
fi

if [ $PRESERVE -ne 1 ]; then
	rm $FILE $OUTFILE > /dev/null 2>&1
fi

##############################################################################
# C++ compilation							     #
##############################################################################
FILE='hello.cpp'
/bin/echo "# include <iostream>" > $FILE
/bin/echo "int main() {" >> $FILE
/bin/echo -E "std::cout << \"${MESSAGE}\n\";" >> $FILE
/bin/echo -e "}\n" >> $FILE

OUTFILE='cpp_hello'
`$CPPC -o $OUTFILE $FILE > /dev/null 2>&1`
if [ $? -eq 0 ]; then
	echo "-> C++ compilation succeeded."
	chmod +x $OUTFILE
	OUTPUT=`./$OUTFILE 2>&1 | grep -c "$MESSAGE"`
	if [ $OUTPUT -eq 1 ]; then
		echo "-> Compiled C++ code executed correctly."
	else
		echo "-> Compiled C++ code did not execute as expected!"
		echo "   Verify that $CPPC is working correctly."
	fi
else
	echo "-> C++ compilation failed! Make sure $CPPC is installed."
fi

if [ $PRESERVE -ne 1 ]; then
	rm $FILE $OUTFILE > /dev/null 2>&1
fi

##############################################################################
# Java compilation							     #
##############################################################################
FILE='hello.java'
CLASS='HelloWorld'
/bin/echo "class $CLASS {" > $FILE
/bin/echo "public static void main (String[] args) {" >> $FILE
/bin/echo "System.out.println(\"${MESSAGE}\");" >> $FILE
/bin/echo -e "}\n}\n" >> $FILE

OUTFILE="${CLASS}.class"
`$JAVAC $FILE > /dev/null 2>&1`
if [ $? -eq 0 ]; then
	echo "-> Java compilation succeeded."
	OUTPUT=`$JAVA $CLASS 2>&1 | grep -c "$MESSAGE"`
	if [ $OUTPUT -eq 1 ]; then
		echo "-> Compiled Java code executed correctly."
	else
		echo "-> Compiled Java code did not execute as expected!"
		echo "   Verify that $JAVAC is working correctly."
	fi
else
	echo "-> Java compilation failed! Make sure $JAVAC is installed."
	echo "   Note that this is different from the $JAVA command."
fi

if [ $PRESERVE -ne 1 ]; then
	rm $FILE $OUTFILE > /dev/null 2>&1
fi

##############################################################################
# Python Interpretation							     #
##############################################################################
FILE='hello.py'
/bin/echo "print '${MESSAGE}'" > $FILE

OUTPUT=`$PYTH $FILE 2>&1 | grep -c "$MESSAGE"`
if [ $OUTPUT -eq 1 ]; then
	echo "-> Python interpretation succeeded."
else
	echo "-> Python interpretation failed! Make sure $PYTH is installed."
fi

if [ $PRESERVE -ne 1 ]; then
	rm $FILE > /dev/null 2>&1
fi
