#! /bin/bash
# pst2pdf_for_latexmk
# PSTricks 2 PDF converter :
# Usage: 
#      pst2pdf_for_latexmk
# produces PDF files for all files of the form *-fig*.tex
#      pst2pdf_for_latexmk <FILE> 
# only considers FILE-fig*.tex

# Modified from pst2pdf distributed with pdftricks.sty to use latexmk

# IT REQUIRES VERSION 3.21 OR HIGHER OF latexmk

# For each pdf file will only be made if the tex source is out-of-date
# This version of pst2pdf does not clean up generated files: they are
# needed by latexmk to determine whether or not the pdf file is
# out-of-date. 
#
#   To use this automatically with latexmk (linux/UNIX system assumed)
#      1. Install this script (pst2pdf_for_latexmk) somewhere in your PATH
#      2. Put a line like the following in an initialization file for latexmk:
#            $pdflatex = 'pdflatex %O %S; pst2pdf_for_latexmk %B';

# 13 Oct 2016 John Collins: Remove outdated URL and version information
#  1 Oct 2007 John Collins: Remove path from latexmk
# 28 Sep 2007 Herb Schulz processing steprs of figure files changed for no rotation and better BoundingBox
# 27 Sep 2007 John Collins
# 26 Sep 2007 John Collins

Myname='Pst2pdf_for_latexmk'
myname='pst2pdf_for_latexmk'

echo "This is $myname modified to use latexmk, by John Collins"

FILE=$1
if test -z $FILE; then
		FIGURES=`ls *-fig*.tex`;
else
		FIGURES=`ls -- $FILE-fig*.tex`;
fi


if test -z "$FIGURES"; then
   echo $Myname: No files to process
else
  echo $Myname: Using latexmk to process: $FIGURES 
#  latexmk -pdfdvi -ps- -dvi- -e '$dvipdf = q{dvips -E -o %B.eps %S && epstopdf %B.eps --outfile=%D}'  $FIGURES
  latexmk -pdfdvi -ps- -dvi- -e '$dvipdf = q{dvips -o %B.ps %S ; ps2pdf13 -dAutoRotatePages=/None %B.ps ; pdfcrop %B.pdf ; /bin/mv %B-crop.pdf %D}'  $FIGURES
fi
