#!/bin/bash
#==============================================================================
# MODULE     : tm_dratex
# VERSION    : 1.0
# DESCRIPTION: A simple DraTex interface for TeXmacs
# COPYRIGHT  : (C) 2004 Nicolas Ratier (nicolas DOT ratier AT lpmo DOT edu)
#------------------------------------------------------------------------------
# COPYRIGHT  : (C)   DraTex latex package Eitan M. Gurari
# COPYRIGHT  : (C) AlDraTex latex package Eitan M. Gurari
#------------------------------------------------------------------------------
# Usage within TeXmacs:
#   + write (Al)DraTex-commands in multi-line separate by SHIFT-ENTER,
#   + then ENTER key terminates the input and sends it to latex.
# Informations:
#   + Output is the 2D graphic made via latex (package dratex}, 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>.
#==============================================================================

# option --texmacs
# for compatibility with TeXmacs interface convention and user information
if [ "$1" != "--texmacs" ]
then
	echo tm_dratex. 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 temporary file name
TEMP_FILE=TMPdratexTMP

# Startup banner
echo -n $DATA_BEGIN
echo verbatim: TeXmacs interface to \(Al\)DraTex \(High Level Drawing Facilities\)

# Prompt-input-dratex-output loop
while [ 1 ]; do
	# Prompt
	echo -n $DATA_BEGIN
	echo -n channel:prompt
	echo -n $DATA_END
	echo -n DraTex'] '
	echo -n $DATA_END

	# Read a line from stdin
	read -r input

	# Begin creation of LaTeX file
	echo -E "\documentclass{article}"                                >  $TEMP_FILE.tex
	echo -E "\input $TEXMACS_PATH/plugins/dratex/latex/DraTex.sty"   >> $TEMP_FILE.tex
	echo -E "\input $TEXMACS_PATH/plugins/dratex/latex/AlDraTex.sty" >> $TEMP_FILE.tex
	echo -E "\pagestyle{empty}"                                      >> $TEMP_FILE.tex
	echo -E "\begin{document}"                                       >> $TEMP_FILE.tex

	# Copy DraTex command in LaTeX file
	echo -E $input | tr  "~" "\n" | cat >> $TEMP_FILE.tex

	# Finish LaTeX file
	echo -E "\end{document}"            >> $TEMP_FILE.tex

	# Compile with latex, transform to .eps and cat .eps to TeXmacs.
	latex --interaction=nonstopmode  $TEMP_FILE.tex > /dev/null
	dvips -q -f -E $TEMP_FILE.dvi -o $TEMP_FILE.eps > /dev/null
	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.*
done
