#!/bin/sh

###############################################################################
#                                                                             #
#   qcc - Quantum Circuit Compiler for Unix                                   #
#   J. Yamazaki, M. Suzuki and H. Watanabe                                    #
#   Copyright (C) 2003 QCAD project, all rights reserved.                     #
#                                                                             #
###############################################################################


###############################################################################
# Global valuables
###############################################################################
# ocppfile
# ofile
# ifile
# qcpp_args
# gcc_args

er_str0="Try 'qcc --help' for more information."
er_str1="No output file."
er_str2=" is unexpected option."
er_str3="'-o' option can be used only once."
er_str4="Too many input files."
er_str5="No input file."
er_str6="File not found."
er_str7="'-C' option can be used only once."

version_str1="qcc version 0.2"
version_str2="Copyright (C) 2003 QCAD project."

opt_stop=0
opt_create=0
opt_input=0
opt_output=0
opt_verbose=0

inst_dir=../calcunits

###############################################################################
print_usage()
{
	echo "Usage: qcc [options] file"
	echo "Options:"
	echo "  --help, -h ... Display this information"
	echo "  -c         ... Compile into c++, but do not link"
	echo "  -C [36mfile[00m    ... Create intermediate c++ file as [36mfile[00m"
	echo "                 (is effective only when '-c' is not selected)"
	echo "  -o [36mfile[00m    ... Place the output into [36mfile[00m"
	echo "                 (is effective for both c++ file and executables)"
	echo "  -v         ... Display the programs invoked by the compiler"
	echo "  -V         ... Display qcc version number"
	echo "Examples:"
	echo "  qcc -c sample.mcd -o sample.cpp           OK"
	echo "  qcc sample.mcd -o a.out                   OK"
	echo "  qcc sample.mcd -C sample.cpp -o a.out     OK"
	echo "  qcc -c sample.mcd -C sample.cpp -o a.out  [31;01mERROR![00m"
	echo "  [33mIn the last one, '-C' conflicts with '-o',"
	echo "  then create a.out as c++ file.[00m"
}

###############################################################################
option_create()
{
	if [ $opt_create -eq 0 ]; then
		if [ $1 ]; then
			ocppfile=$1
			opt_create=1
		else
			echo $er_str1
			exit 1
		fi
	else
		echo $er_str7
		exit 1
	fi
}

###############################################################################
option_o()
{
	if [ $opt_output -eq 0 ]; then
		if [ $1 ]; then
			ofile=$1
			opt_output=1
		else
			echo $er_str1
			exit 1
		fi
	else
		echo $er_str3
		exit 1
	fi
}

###############################################################################
###############################################################################
###############################################################################
###############################################################################
# Analyze arguments...

if [ $1 ]; then
	:
else
	echo
	echo $version_str2
	echo $version_str1
	echo
	print_usage
	echo
	exit 0
fi

while [ $1 ]
do
	case $1 in
	--help | -h)
		echo
		print_usage
		echo
		exit 0
		;;
	-c)
		opt_stop=1
		;;
	-C)
		shift
		option_create $1
		opt_create=1
		;;
	-o)
		shift
		option_o $1
		;;
	-v)
		opt_verbose=1
		;;
	-V)
		echo
		echo $version_str2
		echo $version_str1
		echo
		exit 0
		;;
	*)
		if [ $opt_input -eq 0 ]; then
			ifile=$1
			opt_input=1
		else
			echo "$er_str4   '$1'"
			exit 1
		fi
		;;
	esac

	if [ $1 ]; then
		shift
	fi
done

###############################################################################
# Check arguments...

if [ $opt_input -eq 0 ]; then
	echo $er_str5
	exit 1
fi

if !([ -r $ifile ]); then
	echo $er_str6
	exit 1
fi

###############################################################################
# Create qcpp args

qcpp_args=""
gcc_args="-I../qclib -I$inst_dir"

#if [ $opt_verbose -eq 1 ]; then
#	qcpp_args="-v"
#fi

qcpp_args="$qcpp_args $ifile"

if [ $opt_stop -eq 1 ]; then
	if [ $opt_output -eq 1 ]; then
		qcpp_args="$qcpp_args -o $ofile"
	fi
else
	if [ $opt_create -eq 0 ]; then
		ocppfile="qcctmp.cpp"
	fi
	qcpp_args="$qcpp_args -o $ocppfile"
#TODO:
	gcc_args="$ocppfile $gcc_args ../qclib/qclib.a -lm"
	if [ $opt_output -eq 1 ]; then
		gcc_args="$gcc_args -o $ofile"
	fi
fi

###############################################################################
# For debug
#echo "qcpp_args   = $qcpp_args"
#echo "gcc_args    = $gcc_args"
#echo "opt_stop    = $opt_stop"
#echo "opt_create  = $opt_create"
#echo "opt_verbose = $opt_verbose"
#echo "opt_input   = $opt_input"
#echo "opt_output  = $opt_output"
#echo "ocppfile    = $ocppfile"
#echo "ofile       = $ofile"
#echo "ifile       = $ifile"

###############################################################################
# Compile phase...

if [ $opt_verbose -eq 1 ]; then
	echo "qcpp $qcpp_args"
fi
if !(qcpp $qcpp_args); then
	exit 1
fi

if [ $opt_stop -eq 1 ]; then
	exit 0
fi

###############################################################################

if [ $opt_verbose -eq 1 ]; then
	echo g++ $gcc_args
fi

g++ $gcc_args

if [ $opt_create -eq 0 ] && [ -e $ocppfile ]; then
	rm -f $ocppfile
fi

exit 0
