%
% This file belongs to the MFTOEPS package.
%
% TeX file approximately emulating the following AWK program:
%   BEGIN {N=""}
%   $0 ~ /^:EPS FILE NAME: / {
%    if (NF==3) {N="vicar.eps"} else {N=$NF ".eps"} print "extracting " N}
%   $0 ~ /^:PS: / {print substr($0,6) > N}
% (see SPLITEPS.AWK)
%
% REMARK: it was a suggestion of Phil Taylor to use TeX instead of AWK.

\ifx\inputname\undefined \errmessage{\noexpand\inputname NOT DEFINED}\fi
\newread\currentinput \immediate\openin\currentinput\inputname\relax
\ifeof\currentinput \errmessage{FILE \inputname\space NOT FOUND}\fi

\newwrite\currentoutput

\newif\ifcontinueloop
\newif\ifoutputopen \outputopenfalse

\def\EPSfirstline{EPS FILE NAME}
\def\EPSline{PS}

\def\gobblespace#1{#1}
\def\truncatekey#1:#2:#3\relax{%
 \def\linecontents{#3}%
 \ifx\linecontents\empty
 \else
  \edef\linecontents{\expandafter\gobblespace\linecontents}%
 \fi}

\long\def\getkey#1:#2:{\gdef\prekey{#1}\gdef\key{#2}}

\def\getline{%
 \immediate\read\currentinput to \currentline \edef\currentline{\currentline}%
 \setbox0\hbox{\expandafter\getkey\currentline::}}

\def\fixnewoutputname{%
 \expandafter\truncatekey\currentline\relax
 \edef\outputname{\linecontents}%
 \ifx\outputname\empty \edef\outputname{vicar.eps }%
 \else \edef\outputname{\outputname.eps }\fi
 \immediate\write16{extracting \outputname}}

\def\optionallyopenout{%
 \ifx\key\EPSfirstline
  \optionallycloseout \fixnewoutputname
  \immediate\openout\currentoutput\outputname\relax
  \ifeof\currentoutput
   \errmessage{CANNOT OPEN \outputname FOR WRITING}\global\outputopenfalse
  \else
   \global\outputopentrue
  \fi
 \fi}

\def\optionallycloseout{\ifoutputopen \immediate\closeout\currentoutput \fi}

\def\optionallyputline{%
 \ifx\key\EPSline
  \expandafter\truncatekey\currentline\relax
  \immediate\write\currentoutput{\linecontents}%
 \fi}

\def\checklooping{\continuelooptrue\ifeof\currentinput\continueloopfalse\fi}

\def\neutralizecatcodes{%
 \catcode`\\12 \catcode`\%12 \catcode`\&12 \catcode`\_12
 \catcode`\^12 \catcode`\{12 \catcode`\}12 \catcode`\/12 }

\def\getlines{%
 \begingroup
  \endlinechar=-1
  \neutralizecatcodes
  \loop
   \getline \ifx\prekey\empty \optionallyopenout \optionallyputline \fi
  \checklooping \ifcontinueloop \repeat
 \endgroup
 \optionallycloseout}

\getlines

\end
