#!/usr/pkg/bin/bash
#==============================================================================
# MODULE     : tm_texgraph
# VERSION    : 0.04
# DESCRIPTION: A simple TeXmacs interface for TeXgraph
# COPYRIGHT  : 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 version 3 or later.
# It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
# in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
#==============================================================================

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
TEMP_MACRO=macrotmp
	
# 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

	# determine which macro to preload
	echo -E $input > $TEMP_MACRO
	MACROS=nomacro
	MACROS=`grep {preload=.*} $TEMP_MACRO | sed 's/{/\n/g' | sed 's/}/\n/g' | grep preload=.* | sed 's/preload=//g' | sed 's/"/\"/g'`  2> tmp_log

	# begin creation of teg file
	echo -E "100#-5#5#-5#5#1#1##"  >> $TEMP_FILE.teg
	echo -E "101#0.25#0.25#0.5#0.25#0#1#2##"  >> $TEMP_FILE.teg
	echo -E "18##[theta:=0.5236, phi:=1.0472,OriginalCoord(1),IdMatrix(),IdMatrix3D(),ModelView(ortho)]##"  >> $TEMP_FILE.teg
	if [ "$MACROS" != "nomacro" ] 
	then 	echo -E "18##InputMac("$MACROS")##" > $TEMP_FILE.teg
	fi
	echo -E ""  >> $TEMP_FILE.teg
	echo -E "14#TeXmacs#[OriginalCoord(0), "        >> $TEMP_FILE.teg

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

	# finish .teg file
	echo -E "]#-1##" >> $TEMP_FILE.teg

	# determine the export type chosen by the user (eps, epsc, jpg, png...)
	EXP_CHOICE=epsc
	EXP_CHOICE=`grep {export=.*} $TEMP_FILE.teg | sed 's/{/\n/g' | sed 's/}/\n/g' | grep export=.* | sed 's/export=//g'`

	# compile with CmdTeXgraph, transform to .EXPORT_CHOICE and cat file to TeXmacs.
	EXPT=epsc
	if [ "$EXP_CHOICE" = "epsc" ] ; then EXPT='epsc'
	elif [ "$EXP_CHOICE" = "eps" ] ; then EXPT='eps'
	elif [ "$EXP_CHOICE" = "pdfc" ] ; then EXPT='pdfc'
	elif [ "$EXP_CHOICE" = "pdf" ] ; then EXPT='pdf'
	#elif [ "$EXP_CHOICE" = "jpg" ] ; then EXPT='pdfc'
	#elif [ "$EXP_CHOICE" = "png" ] ; then EXPT='pdfc'
	fi

	if [ "$OSTYPE" = "cygwin" ] ; then EXT='.bat'; else EXT=''; fi
        shext=`command -v CmdTeXgraph.sh`
        if [ "$shext" != "" ] ; then EXT='.sh'; fi
	CmdTeXgraph$EXT $EXPT $TEMP_FILE 2> tmp_log

 	if [ -s $TEMP_FILE.pdf ] 
	then
		#convert -density 600 -depth 8 -quality 75 $TEMP_FILE.pdf $TEMP_FILE.jpg
		pdftops -eps $TEMP_FILE.pdf $TEMP_FILE.eps
		echo -n $DATA_BEGIN                             
 		echo -n verbatim:
		echo -n $DATA_BEGIN
		echo -n ps:
		cat $TEMP_FILE.eps
	     	#jpeg2ps -r 0 $TEMP_FILE.jpg
		echo -n $DATA_END
		echo -ne "\n"
		rm $TEMP_FILE.* $EXPT $EXP_CHOICE 2> /dev/null
 	elif [ -s $TEMP_FILE.eps ]
	then
		echo -n $DATA_BEGIN                             
 		echo -n verbatim:
		echo -n $DATA_BEGIN
		echo -n ps:
		cat $TEMP_FILE.eps
		echo -n $DATA_END
		echo -ne "\n"
		rm $TEMP_FILE.* $EXPT $EXP_CHOICE 2> /dev/null
	else
		echo -n $DATA_BEGIN
		echo -n verbatim:
		cat $TEMP_FILE
		echo -n $DATA_BEGIN
		cat $TEMP_FILE.log
		echo -n $DATA_END
		echo -ne "\n"
		rm $TEMP_FILE.* $EXPT $EXP_CHOICE 2> /dev/null
	fi
done
