#!/bin/bash
#==============================================================================
# MODULE     : tm_texgraph
# VERSION    : 0.04
# DESCRIPTION: A simple TeXmacs interface for TeXgraph
# BY	     : Emmanuel Corcelle (corcelle at gmail dot com)
#------------------------------------------------------------------------------
# Based on   : A simple PSTricks interface for TeXmacs 
# COPYRIGHT  : (C) 2004 Nicolas Ratier (nicolas DOT ratier AT lpmo DOT edu))
#------------------------------------------------------------------------------
# COPYRIGHT  : (C) TeXgraph by Patrick Fradin (pfradin at tuxfamily point org) (http://texgraph.tuxfamily.org/)
#------------------------------------------------------------------------------
# tm_texgraph 
# ========== 
# bash script for interfacing TeXgraph from TeXmacs
# needs option --texmacs for compatibility with TeXmacs interface convention and user information
#
# usage within TeXmacs:
# =====================
# write texgraph-commands within the input line, use as many commands as necessary, 
# divide them by the "," chararacter, because the ENTER key terminates the input and sends it to TeXgraph.
# output is the graph made via TeXgraphCmd, latex, and dvips -E mode.
#
# Temporary file are made in ~/.TeXmacs/system/tmp
#------------------------------------------------------------------------------
# This software falls under the GNU general public license and comes WITHOUT
# ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
# If you don't have this file, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#==============================================================================

if [ "$1" != "--texmacs" ]
then
	echo tm_texgraph. This script should be started only from TeXmacs.
	exit
fi	

# control characters
tmp=`echo DATA_BEGIN=X DATA_END=Y DATA_ESCAPE=Z | tr "XYZ" "\002\005\027" `
eval $tmp

# defining temporary files directory and make it if it doesn't exist
TEMP_DIR=~/.TeXmacs/system/tmp
if [ -d $TEMP_DIR ]
then
	cd $TEMP_DIR
else	
	mkdir -p $TEMP_DIR
	cd $TEMP_DIR
fi

# defining primary temp file name
TEMP_FILE=texgraphtmp
	
# startup banner
echo -n $DATA_BEGIN
echo verbatim:TeXmacs interface to TeXgraph.
echo To write LaTeX code inside labels, use \\\\ instead of \\. 
echo For example, write $\\\\pi$ instead of $\\pi$
echo -n $DATA_END

# prompt-input-texgraph-output loop
while [ 1 ]; do
	# prompt
	echo -n $DATA_BEGIN
	echo -n channel:prompt
	echo -n $DATA_END 
	echo -n TeXgraph'] '
	echo -n $DATA_END 
	 
	# read a line from stdin
	read input

	# begin creation of TeX file
	echo -E "\documentclass{article}" >  $TEMP_FILE.tex
	echo -E "\usepackage{texgraph}"    >> $TEMP_FILE.tex
	echo -E "\pagestyle{empty}"       >> $TEMP_FILE.tex
	echo -E "\begin{document}"        >> $TEMP_FILE.tex
        echo -E "\begin{texgraph}[export=epsc]" >> $TEMP_FILE.tex
        echo -E "HideStyle:=dashed,"      >> $TEMP_FILE.tex
        echo -E "Marges(0.25,0.25,0.25,0.25),"      >> $TEMP_FILE.tex
	# other commands to initialize the graph can be included here

	# copy TeXgraph in latex file
	echo -E $input | tr  "~" "\n" | cat >> $TEMP_FILE.tex

	# finish TeX file
	echo -E "\end{texgraph}" >> $TEMP_FILE.tex
        echo -E "\end{document}" >> $TEMP_FILE.tex

	# compile with latex, transform to .eps and cat .eps to TeXmacs.
	latex --shell-escape --interaction=nonstopmode --draftmode $TEMP_FILE.tex 1> /dev/null 2> tmp_log
 	if [ -s texgraphtmp1.eps ]
	then
		echo -n $DATA_BEGIN                             
 		echo -n verbatim:
		echo -n $DATA_BEGIN
		echo -n ps:
		cat texgraphtmp1.eps
		echo -n $DATA_END
		echo -ne "\n"
		rm $TEMP_FILE.* texgraphtmp1.* tmp*
	else
		echo -n $DATA_BEGIN
		echo -n verbatim:
		cat texgraphtmp1.log
		echo -n $DATA_BEGIN
		echo -n ps:
		echo -n $DATA_END
		echo -ne "\n"
		rm $TEMP_FILE.*  texgraphtmp1.* tmp*	
	fi
done
