#!/bin/sh
#
# tmtotmml 'fichier.tm' produit 'fichier.tex' dans le même chemin

in_file="$1"

buf_init="${TMPDIR:=/tmp}/tmtotmml$$1"

if [ ! "$2" = "" ]; then
    out_file=$2
else
    tmml_dir=`dirname $in_file`
    tmml_file=`basename $in_file .tm`.tex
    out_file=$tmml_dir/$tmml_file
fi

rm -f $out_file

trap 'rm -f "$buf_init" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 13 15

cat > "$buf_init" <<-EOF
(define f 
  (url-concretize (url "$PWD" (string-append "local:" "$out_file"))))
(display-err* "save to '" f "'\n")
(save-buffer f "latex")
(quit-TeXmacs) 
EOF

texmacs -s -b "$buf_init" $in_file >/dev/null

