#!/bin/sh
#
# texpsinclude, see http://www.ma.utexas.edu/mp_arc/index-90.html
## 
## Usage: texpsinclude <texfile> <psfiles>  >  <outfile>
## 
## <texfile> is the original .tex file (TeX, LATeX, AMSTeX, etc.)
## <psfiles> is a list of .ps files like: fig1.ps bar.ps fig2.ps
## <outfile> will be the .tex file produced by texpsinclude
## 
## To get more information, type: texpsinclude info
## or to see the TeX macros used: texpsinclude macros
## 
###########################################################################
#: Here's the problem that texpsinclude solves:
#: 
#: You have a TeX file called foo.tex that you want to distribute as a
#: single TeX file.  The problem is that foo.tex needs two Postscript
#: files, bar1.ps and bar2.ps, for embedded figures.  You'd like a single
#: TeX file which somehow includes bar1.ps and bar2.ps.  When TeX
#: processes foo.tex, TeX should extract bar1.ps and bar2.ps from
#: foo.tex.  
#: 
#: Here's how to do what you want to do:
#: 
#:   texpsinclude foo.tex. bar1.ps bar2.ps > bigfoo.tex
#: 
#: (In general: "texpsinclude <texfile> <psfiles>  >  <outfile>".)
#: If you enter this command, the result is a new file called bigfoo.tex.
#: The file bigfoo.tex contains foo.tex, bar1.ps, and bar2.ps.
#: If you give bigfoo.tex to a friend, she can make your document with:
#: 
#:   tex bigfoo.tex
#: 
#: This command writes out bar1.ps and bar2.ps, and the command also
#: TeX's foo.tex.
#: 
#: Note: If *you* run tex on bigfoo.tex in the same directory,
#: then TeX will write over your .ps files.
#: Be careful when testing your bigfoo.tex.
#: 
#: --Jamie Stephens, 16 Nov 94
#: 
#: 
#: Note': The <psfiles> need not necessarily contain PostScript code.
#: texpsinclude can be used to include other types of ASCII files
#: into a .tex file, such as source code for a computer program, etc.
#:
###########################################################################
#% % TeX macros for dumping included Postscript to files.
#% % Adapted from Knuth's \answer macro in the TeXbook.
#% % Jamie Stephens, jamies@math.utexas.edu, 28 Nov 94
#% 
#% \def\endofps{EndOfTheIncludedPostscriptMagicCookie}
#% \chardef\other=12
#% \newwrite\psdumphandle 
#% \outer\def\psdump#1{\par\medbreak
#%   \immediate\openout\psdumphandle=#1
#%   \copytoblankline}
#% \def\copytoblankline{\begingroup\setupcopy\copypsline}
#% \def\setupcopy{\def\do##1{\catcode`##1=\other}\dospecials
#%   \catcode`\\=\other \obeylines}
#% {\obeylines \gdef\copypsline#1
#%   {\def\next{#1}%
#%   \ifx\next\endofps\let\next=\endgroup %
#%   \else\immediate\write\psdumphandle{\next} \let\next=\copypsline\fi\next}}
#% \outer\def\closepsdump{
#%   \immediate\closeout\psdumphandle}
#% 
#%%% EXAMPLE (remove the leading % signs to make it work):
#%%%
#%%%\psdump{example.ps}These three lines
#%%%are going be dumped "as is"
#%%%to the file example.ps
#%%%EndOfTheIncludedPostscriptMagicCookie
#%%%\closepsdump
###########################################################################

if [ $# -eq 0 ]; then
  sed -n 's/^## //p' $0 >&2
  exit 1
elif [ "$1" = "info" ]; then
  sed -n 's/^#: //p' $0 >&2
  exit 0
elif [ "$1" = "macros" ]; then
  sed -n 's/^#%.//p' $0 >&2
  exit 0
else
  for F in $@; do
    if [ ! -r "$F" ]; then
      echo "Could not find (or read) $F" >&2
      exit 1
    fi
  done
fi

TEXFILE="$1"
shift

cat <<EOF
% This TeX file was generated by $0
% from the original TeX file $TEXFILE
% and the PostScript files $@.
% TeX writes out the included PostScript to files.
%
EOF

CUT=`sed -n '/^[^%]*\\\document[cs][lt][ay][sl][se]/=' "$TEXFILE" | head -1`
if [ -n "$CUT" ]; then
  echo "% Here is part of $TEXFILE:"
  head -$CUT "$TEXFILE"
fi

echo
echo "% Here are the"
sed -n 's/^#% //p' $0

for FILE in $@; do
  echo "Including $FILE" >&2
  echo "% Here is the PostScript for $FILE:"
  echo "\message{Writing file $FILE}"
  echo -n "\psdump{$FILE}"
  cat "$FILE"
  echo "EndOfTheIncludedPostscriptMagicCookie"
  echo
  echo "\closepsdump"
  echo
done

echo "Including $TEXFILE" >&2

if [ -n "$CUT" ]; then
  echo "% Finally, here is the rest of $TEXFILE:"
  echo -n "% "
  tail +$CUT "$TEXFILE"
else
  echo "% Finally, here is $TEXFILE:"
  cat "$TEXFILE"
fi

