#!/bin/bash

# AUTHOR:
# Francesc Su\~nol <francesc@fa.upc.edu>

# HISTORY: 
# - Created by Francesc Su\~nol in 2006-12-13.
# - Modified by Francesc Su\~nol in 2008-06-05: Added the extension
#   conditional.
# - Modified by Francesc Su\~nol in 2009-02-27: Added the auxfile
#   option, in order to import graphics in pslatex term.
# - (Add your modifiactions and date here).

# REQUIRES: 
# gnuplot, latex, dvips, epstool, epstopdf, xpdf.

# OBJECTIVES: 
# Create a plot (from a gnuplot script) ready to include in 
# a LaTeX document.

# DESCRIPTION:
# This script has one gnuplot script file as an INPUT (no need to 
# 'set term' nor 'set out'), and one .eps with one .pdf plots as 
# OUTPUT. The .pdf plot opens automatically in order to see the 
# result at the moment.

# EXAMPLE OF INPUT:
# The input-file can contain the next 4 lines (without the # symbol):
# set border 31 lw 0.3
# set xlabel '$t$ (s)'
# set ylabel '$y$ (m)'
# plot 1-x**2 ti '$y(t)=1-t^2$' 

# USAGE:
# bash gp2epspdf.bash input-file 

# NOTES:
# If you want to  see the line styles and point types available, 
# create an input-file containing only the "test" word.
# If the name of the input-file is "XXXX.gp", the output files will 
# be "XXXX.eps" and "XXXX.pdf". If the extension of the input file
# is not ".gp", the ".eps" and ".pdf" extensions will be added to the
# name of the input file.

# COPYRIGHT:
# 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 3 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.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Copyright 2006, 2007, 2008, 2009 Francesc Su\~nol.


echo '\documentclass{article}
\usepackage{graphicx,psfrag}
\usepackage{color}
\usepackage{bm}
\begin{document}
\thispagestyle{empty}
\begin{figure}[!h]
\begin{center}
\input{gpplot.tex}
\end{center}
\end{figure}
\end{document}' > gp2epspdfaux.tex

name=`echo "$1"|sed 's/\.[^\.]*$//'`
extension=`echo "$1"|awk -F . '{print $NF}'`

if [ "$extension" = "gp" ]; then

echo -e "\033[01;31m.gp extension detected. Generating aux files... \033[00m"
echo "set term pslatex color auxfile 
set out 'gpplot.tex' 
" > gp2epspdfauxtmpl.gp
cat $1 >> gp2epspdfauxtmpl.gp
echo "
set out ; set term X11" >> gp2epspdfauxtmpl.gp

echo -e "\033[01;33mGnuplotting... \033[00m"
gnuplot gp2epspdfauxtmpl.gp
echo -e "\033[01;33mLaTeXing... \033[00m"
latex -halt-on-error -interaction nonstopmode gp2epspdfaux.tex > /dev/null 
echo -e "\033[01;33mGenerating EPS and PDF files... \033[00m"
dvips -q -D600 -E* -o gp2epspdfaux.eps gp2epspdfaux.dvi
epstool --bbox --copy --output $name.eps gp2epspdfaux.eps
rm gpplot.tex gpplot.ps gp2epspdfaux.aux gp2epspdfaux.dvi gp2epspdfaux.eps gp2epspdfaux.log gp2epspdfaux.tex gp2epspdfauxtmpl.gp
epstopdf $name.eps
echo -e "\n\033[01;32mDone! \033[00m \n"

else

echo -e "\032[01;33mNo .gp extension detected! Generating aux files... \033[00m"
echo "set term pslatex color auxfile
set out 'gpplot.tex' 
" > gp2epspdfauxtmpl.gp
cat $1 >> gp2epspdfauxtmpl.gp
echo "
set out ; set term X11" >> gp2epspdfauxtmpl.gp

gnuplot gp2epspdfauxtmpl.gp
latex -halt-on-error -interaction nonstopmode gp2epspdfaux.tex 
dvips -D600 -E* -o gp2epspdfaux.eps gp2epspdfaux.dvi
epstool --bbox --copy --output $1.eps gp2epspdfaux.eps
rm gpplot.tex gpplot.ps gp2epspdfaux.aux gp2epspdfaux.dvi gp2epspdfaux.eps gp2epspdfaux.log gp2epspdfaux.tex gp2epspdfauxtmpl.gp
epstopdf $1.eps
echo -e "\n\033[01;32mDone! \033[00m \n"

fi
exit 0

