#
#	This is Sub Makefile for Gnuplot.
#

#### Input extention ####
# Defined at Main

#### Suffixes rule ####

#### INCLUDE ####
# Use TIPS File.
# -include ${EOS_HOME}/tutorial/TIPS/XXXX/Makefile/Makefile-lib

#### Definition ####
# Defined at Main
# gnuplot Application directory path
G_PLOT_PATH=${GNUPLOT_PATH}
G_PLOT_EXE="${GNUPLOT_PATH}gnuplot"

#### Rules of the list created ####


##### Commands #####
gnuplot2D::
	@if [ ! -z $(G_PLOT_EXE) -a ! -z "$(IN_GNU_FILE_NAME)" -a ! -z "$(IN_GNU)" ] ; then \
		if [ ! -z "$(GNU_X_SIZE)" -a ! -z "$(GNU_Y_SIZE)" -a ! -z "$(OUT_GNU_FILE_NAME)" -a ! -z "$(OUT_GNU)" ] ; then \
			OPTION1="set terminal png size $(GNU_X_SIZE), $(GNU_Y_SIZE); set output '$(OUT_GNU_FILE_NAME).$(OUT_GNU)' ;"; \
		else \
			OPTION2="pause -1 ;" ; \
		fi; \
		$(G_PLOT_EXE) -e "$${OPTION1} plot $$(make gnuplotSet) $${OPTION2}" ; \
		echo "success gnuplot2D"; \
	else \
		echo "failed gnuplot2D"; \
	fi;
	
gnuplot3D::
	@if [ ! -z $(G_PLOT_EXE) -a ! -z "$(IN_GNU_FILE_NAME)" -a ! -z "$(IN_GNU)" ] ; then \
		if [ ! -z "$(GNU_X_SIZE)" -a ! -z "$(GNU_Y_SIZE)" -a ! -z "$(OUT_GNU_FILE_NAME)" -a ! -z "$(OUT_GNU)" ] ; then \
			OPTION1="set terminal png size $(GNU_X_SIZE), $(GNU_Y_SIZE); set output '$(OUT_GNU_FILE_NAME).$(OUT_GNU)' ;"; \
		else \
			OPTION2="pause -1 ;" ; \
		fi; \
		$(G_PLOT_EXE) -e "$${OPTION1} splot $$(make gnuplotSet) $${OPTION2}" ; \
		echo "success gnuplot3D"; \
	else \
		echo "failed gnuplot3D"; \
	fi;

gnuplotSet::
	@declare infile=( $(IN_GNU_FILE_NAME) ); \
	infilenum=$${#infile[@]}; \
	declare inext=( $(IN_GNU) ); \
	inextnum=$${#inext[@]}; \
	declare viewname=( $(VIEW_GNU_FILE_NAME) ); \
	viewnum=$${#viewname[@]}; \
	if [ $$infilenum -le 1 ] ; then \
		for (( num = 0 ; $$num < $$inextnum ; num++ )) ; \
		do \
			fileext="$$infile.$${inext[$$num]}"; \
			if [ $$num -le $$viewnum ] ; then \
				view="$${viewname[$$num]}"; \
				$(RM) $$view; \
				ln -s "$$fileext" $$view; \
			else \
				view="$$fileext"; \
			fi; \
			if [ ! -z "$${OPTION3}" ] ; then \
				OPTION3="$${OPTION3} ,"; \
			fi; \
			OPTION3="$${OPTION3} '$${view}'"; \
			if [ ! -z "$(GNU_STYLE)" ] ; then \
				OPTION3="$${OPTION3} with $(GNU_STYLE)"; \
			fi; \
		done; \
	elif [ $$inextnum -le 1 ] ; then \
		for (( num = 0 ; $$num < $$infilenum ; num++ )) ; \
		do \
			fileext="$${infile[$$num]}.$$inext"; \
			if [ $$num -le $$viewnum ] ; then \
				view="$${viewname[$$num]}"; \
				$(RM) $$view; \
				ln -s "$$fileext" $$view; \
			else \
				view="$$fileext"; \
			fi; \
			if [ ! -z "$${OPTION3}" ] ; then \
				OPTION3="$${OPTION3} ,"; \
			fi; \
			OPTION3="$${OPTION3} '$${view}'"; \
			if [ ! -z "$(GNU_STYLE)" ] ; then \
				OPTION3="$${OPTION3} with $(GNU_STYLE)"; \
			fi; \
		done; \
	else \
		for (( num = 0 ; $$num < $$infilenum ; num++ )) ; \
		do \
			fileext="$${infile[$$num]}.$${inext[$$num]}"; \
			if [ $$num -le $$viewnum ] ; then \
				view="$${viewname[$$num]}"; \
				$(RM) $$view; \
				ln -s "$$fileext" $$view; \
			else \
				view="$$fileext"; \
			fi; \
			if [ ! -z "$${OPTION3}" ] ; then \
				OPTION3="$${OPTION3} ,"; \
			fi; \
			OPTION3="$${OPTION3} '$${view}'"; \
			if [ ! -z "$(GNU_STYLE)" ] ; then \
				OPTION3="$${OPTION3} with $(GNU_STYLE)"; \
			fi; \
		done; \
	fi; \
	echo "$${OPTION3} ;";

help_Gnuplot::
	@echo
	@echo "Gnuplot"
	@echo "This is Sub Makefile for Gnuplot."
	@echo "(Note) Set the gnuplot path to \"G_PLOT_PATH\" in this lib file."
	@echo
	@echo "	(Command):"
	@echo "		gnuplot2D:		view 2D data by gnuplot."
	@echo "		gnuplot3D:		view 3D data by gnuplot."
	@echo "		(Note) multiple input files -> one output file(display or image)"
	@echo
	@echo "	(Setting Data):"
	@echo "		G_PLOT_PATH:		gnuplot application directory path in this lib file.(Essential)"
	@echo "		IN_GNU_FILE_NAME:	input file names as Array.(if it has 1 data only, all data is treated as it.)"
	@echo "		IN_GNU:			input file extentions as Array.(if it has 1 data only, all data is treated as it.)"
	@echo "		(Note) Input file is required to be 2D or 3D that is delimited by space or tab."
	@echo "		====== If the following is set, output the image. ======"
	@echo "		OUT_GNU_FILE_NAME:	output file name."
	@echo "		OUT_GNU:		output file extention."
	@echo "		GNU_STYLE:		plot style.(lines, points, linespoints, ...)"
	@echo "		GNU_X_SIZE:		output tiff size x"
	@echo "		GNU_Y_SIZE:		output tiff size y"
	@echo "		====== Option ======"
	@echo "		VIEW_GNU_FILE_NAME:	displayfile names on gnuplot as Array."
	@echo

##### Commands(Input to Output) #####

