#!/bin/sh
#
# Copyright (C) 2003 Michele Antonecchia <michelelug@telug.it>
#
# version 1.0b
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the 
# Free Software Foundation; either version 2 of the License, or (at your 
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  
# See the GNU General Public License for more details.
#
# A copy of the GNU General Public License should be present with the
# code, in file LICENSE
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

#------ default options ------------
agrpath="./"
destdir="./"
files="*.agr"
format="PNG"
#------ default options ------------

cdir="`pwd`"

tmppar="/tmp/tmpargparam"

SUCCESS="echo -en \\033[1;32m" ;
FAILURE="echo -en \\033[1;31m" ;
WARNING="echo -en \\033[1;33m" ;
NORMAL="echo -en \\033[0;39m" ;

#------ default format options ------------
png_compression="4"
png_transparent="off"
png_interlaced="off"
#------------------------------------------
eps_color="color"     # or grayscale
eps_level="2"       # or 1
eps_bbox="tight"      # or page
eps_docdata="8bit"  # or 7bit or binary
#------------------------------------------

clear

menu()
{
	declare -a testfile
        testfile=($agrpath/$files)
        if [ ! -e ${testfile[0]} ]; then
	       $WARNING; echo -n " WARNING: There are no "; 
	       $SUCCESS; echo -ne $files; 
	       $WARNING; echo -ne " files to convert in ";
	       $SUCCESS; echo -ne $agrpath ; 
	       $WARNING; echo " directory"; $NORMAL
        fi
	echo ""
	echo "1. Select Path of AGR files to convert   ["$agrpath"]"
	echo "2. Select destination directory          ["$destdir"]"
	echo "3. Choose the files to convert           ["$files"]"
	echo "4. Select/Modify output format/options   ["$format"] "
	echo ""
	echo "5. Load parameter file"
	echo "6. Store parameter file"
	echo ""
	echo "c. Convert now"
	echo ""
	echo "x. Exit"
	echo -n "Choice : "

	read CHOICE

	case "$CHOICE" in
		"1")
		echo -n "Select new agrpath  : "
		read agrpath_new
		if [ -d $agrpath_new ]; then
			agrpath=$agrpath_new
			
		else
			clear
			$FAILURE; echo -n " ERROR:  " $agrpath_new; echo ": Directory not found"; $NORMAL;
			menu
			
		fi
		clear
		menu
		;;
		"2")
		echo -n "Select new destination directory : "
		read destdir_new
		if [ -d $destdir_new ]; then
			destdir=$destdir_new
		else
			clear
			$FAILURE; echo -n " ERROR:  " $destdir_new; echo ":  Directory not found"; $NORMAL;
			menu
		fi
		clear
		menu
		;;
		"3")
		echo -n "Select files to converts : "
		read files
		clear
		menu
		;;
		"4")
		clear
		sformat
		;;
		"5")
		loadfile
		;;
		"6")
#	clear
		store
		;;
		"c")
		clear
		convert
		;;
		"x")
		exit 0
		;;
		*)
		clear
#		$WARNING; echo -n "          " $CHOICE; $FAILURE; echo "  Invalid selection."; $NORMAL;
		menu
	esac

}

sformat()
{
	echo ""
	echo -n "Current output format is  ";$SUCCESS; echo -ne $format; $NORMAL
	echo ""
	echo ""
	echo "0. Change/View output format options"
	echo ""
	echo "1. PNG"
	echo "2. EPS"
	echo ""
	echo "x. Return to presvious menu"
	echo -n "Format : "
	
	read SFORMAT

	case "$SFORMAT" in 
		"0")
		clear
		soptions
		;;
		"1")
		format="PNG"
		;;
		"2")
		format="EPS"
		;;
		"x")
		clear
		menu
		;;	
		"*")
		clear
#		$WARNING; echo -n "          " $SFORMAT; $FAILURE; echo " Invalid selection."; $NORMAL;
		sformat
	esac
#	menu		
	clear
	sformat
}

soptions()
{
	case "$format" in
		"PNG")
	        echo ""
		echo "1. Compression level ["$png_compression"]"
		echo "2. Transparent       ["$png_transparent"]"
		echo "3. Interlaced        ["$png_interlaced"]"
		echo ""
		echo "x. Return to previous menu" 
		echo -n "Change : "

		read PNGCHANGE
		echo ""
		case $PNGCHANGE in
			"1")
			echo -n "New compression level [0-9] : "
			read png_compression_new
			case $png_compression_new in
				0|1|2|3|4|5|6|7|8|9)
				png_compression=$png_compression_new
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_compression_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"2")
			echo -n "Transparent [on|off]: "
			read png_transparent_new
			case "$png_transparent_new" in
				"on"|"off")
				png_transparent="$png_transparent_new"
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_transparent_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"3")
			echo -n "Interlaced [on|off]: "
			read png_interlaced_new
			case "$png_interlaced_new" in
				"on"|"off")
				png_interlaced="$png_interlaced_new"
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_interlaced_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"x")
			clear
			sformat
			;;
			"*")
			clear
#			$WARNING; echo -n "          " $PDFCHANGE; $FAILURE; echo " Invalid selection."; $NORMAL;
			soptions
			;;
		esac
		clear
		soptions
		;;
		"EPS")
	        echo ""
		echo "1. Color format   ["$eps_color"]"
		echo "2. PS level       ["$eps_level"]"
		echo "3. Bounding Box   ["$eps_bbox"]"
		echo "4. Document Data  ["$eps_docdata"]"
		echo ""
		echo "x. Return to previous menu" 
		echo -n "Change : "

		read EPSCHANGE
		echo ""
		case $EPSCHANGE in
			"1")
			echo -n "New color format [1=color|2=grayscale]: "
			read eps_color_new
			case "$eps_color_new" in
				"1")
				eps_color=color
				;;
				"2")
				eps_color=grayscale
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_transparent_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"2")
			echo -n "PS Level [1|2]: "
			read eps_level_new
			case "$eps_level_new" in
				"1"|"2")
				eps_level=$eps_level_new
				;;
				"*")
				clear
				soptions
				;;				
			esac
			clear
			soptions
			;;
			"3")
			echo -n "EPS Bounding BOX [1=tight|2=page]: "
			read eps_bbox_new
			case "$eps_bbox_new" in
				"1")
				eps_bbox=tight
				;;
				"2")
				eps_bbox=page
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_transparent_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"4")
			echo -n "Document Data [1=7Bit|2=8Bit|3=binary]: "
			read eps_bbox_new
			case "$eps_bbox_new" in
				"1")
				eps_docdata=7bit
				;;
				"2")
				eps_docdata=8bit
				;;
				"3")
				eps_docdata=binary
				;;
				*)
				clear
#				$WARNING; echo -n "          " $png_transparent_new; $FAILURE; echo " Invalid selection."; $NORMAL;
				soptions
				;;
			esac
			clear
			soptions
			;;
			"x")
			clear
			sformat
			;;
			"*")
			clear
#			$WARNING; echo -n "          " $EPSCHANGE; $FAILURE; echo " Invalid selection."; $NORMAL;
			soptions
			;;
		esac
		clear
		soptions
		;;
	esac
}

convert()
{
	case "$format" in
		"PNG")
		rm -f $tmppar
		echo "hardcopy device \"PNG\"" >> $tmppar
		echo "device \"PNG\" op \"transparent:$png_transparent\" " >> $tmppar
		echo "device \"PNG\" op \"interlaced:$png_interlaced\" " >> $tmppar 
		echo "device \"PNG\" op \"compression:$png_compression\" " >> $tmppar
		echo "Converting all "$files" into "$format" format"
		cd $destdir
		for x in `ls $cdir/$agrpath/$files`; do
			xmgrace -hardcopy -param $tmppar $x
			echo -n "."
		done
		echo""
		echo "done"
		cd $cdir
		rm $tmppar
		;;
		"EPS")
		echo "hardcopy device \"EPS\"" >> $tmppar
		echo "device \"EPS\" op \"$eps_color\" " >> $tmppar
		echo "device \"EPS\" op \"level$eps_level\" " >> $tmppar
		echo "device \"EPS\" op \"bbox:$eps_bbox\" " >> $tmppar
		echo "device \"EPS\" op \"docdata:$eps_docdata\" " >> $tmppar
		echo "Converting all "$files" into "$format" format"
		cd $destdir
		for x in `ls $cdir/$agrpath/$files`; do
			xmgrace -hardcopy -param $tmppar $x
			echo -n "."
		done
		echo ""
		echo "done"
		cd $cdir
		rm $tmppar
		;;
	esac
	exit 0
}

loadfile()
{
	echo -n " Select filename to load parameters : "
	read ldfile
	if [ ! -e $ldfile ]; then
		echo " File not found "
		echo " Press any key to continue "
		read dummy
	fi
	while read LINE
	do
		declare $LINE
	done < $ldfile
	clear
	menu
}

store()
{
	echo " WARNING: If exists a file whit the same name"
	echo "          the old will be overwritte"
	echo -n " Select filename where store the parameter : "
	read stfile

	rm -f $stfile

#	echo "agrpath=\""$agrpath"\"" >> $stfile
#	echo "destdir=\""$destdir"\"" >> $stfile
	echo "files="$files >> $stfile
	echo "format="$format >> $stfile
	echo "png_compression="$png_compression >> $stfile
	echo "png_transparent="$png_transparent >> $stfile
	echo "png_interlaced="$png_interlaced >> $stfile
	echo "eps_color="$eps_color >> $stfile
	echo "eps_level="$eps_level >> $stfile
	echo "eps_bbox="$eps_bbox >> $stfile
	echo "eps_docdata="$eps_docdata >> $stfile
	clear
	menu

}

clear
menu

clear
exit 0

