%% LaTeX2e file `asyfig.sty'
%% generated by the `filecontents' environment
%% from source `asyfig' on 2010/03/20.
%%
\ProvidesPackage{asyfig}[2010/03/20 v0.1c
  Commands for using asymptote figures]

% This package is the main user interface for inserting external |asy| figures
% into the document.

\RequirePackage{%
  asyalign,color,ifmtarg,ifpdf,ifplatform,import,
  graphicx,pdftexcmds,suffix,xkeyval}

% Better conditionals than |\newif| provides:
\def\@True{11}
\def\@False{01}
\def\asy@If#1{\if#1\relax\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}

\let\asy@always\@False
\let\asy@never\@False
\let\asy@process\@False

% Package options:
\define@choicekey*{asyfig.sty}{process}[\@tempa\@tempb]{all,none,auto}{%
  \ifcase\@tempb\relax
    \let\asy@always\@True
  \or
    \let\asy@never\@True
  \or
  \fi
}
\ExecuteOptions{process=auto}

\ProcessOptionsX

% \subsection{Auxiliary macros}

\def\asy@splitpath#1/#2/{%
% Recursive macro that is used like\\
% \qquad |\asy@splitpath abc/def/ghi.asy/\@nil/|\\
% It defines |\asy@filename| $\to$ |ghi.asy| and |\asy@path| $\to$ |abc/def/|
  \ifx\@nil#2\relax
% If input is \<anything>|/\@nil/| then we've reached the end:
    \def\asy@filename{#1}%
  \else
% Otherwise we're in the middle of the slash-separated list;
% build up |\asy@path|, and iterate:
    \edef\asy@path{\asy@path#1/}%
    \def\@tempa{\asy@splitpath#2/}%
    \expandafter\@tempa
  \fi
}

\newcommand\asypath[1]{\def\asy@pathprefix{#1}}
\asypath{}

\def\asy@asyfile{\asy@pathprefix\asy@path\asy@filename.asy}
\def\asy@texfile{\asy@pathprefix\asy@path\asy@filename\string_.tex}

\def\asy@cmdsep{\ifwindows \string& \else; \fi}

% \subsection{The main macro}

\newcommand\asyfig[1]{%
  \let\asy@path\@empty
  \asy@splitpath #1/\@nil/%
  \IfFileExists{\asy@asyfile}{%
    \asy@If\asy@process{}{%
    \asy@If\asy@always{%
      \let\asy@process\@True
    }{%
      \IfFileExists{\asy@texfile}{%
        \asy@If\asy@never{}{%
          % compare file dates to see if we want to reprocess:
          \ifnum\pdf@strcmp{\pdf@filemoddate{\asy@texfile}}
                           {\pdf@filemoddate{\asy@asyfile}} < \z@
            \let\asy@process\@True
          \fi
        }%
      }{% if the .tex file doesn't exist, either
        % give an error or process the .asy file:
        \asy@If\asy@never{%
          \PackageError{asyfig}{%
            ^^J\space\space\space\space
            "\asy@pathprefix\asy@path\asy@filename.asy" requires processing%
          }{%
            The generated file that is required to insert the asy graphic,
            ^^J\space\space\space\space
            "\asy@pathprefix\asy@path\asy@filename\string_.tex"^^J%
            does not exist.
            Please process the asy figure manually or de-activate the^^J%
            [process=none] package option.
          }%
        }{%
          \let\asy@process\@True
        }
      }%
    }}%
    \asy@If\asy@process{%
      \edef\@tempa{\asy@pathprefix\asy@path}%
      \pdf@system{%
        echo "^^J====== ASY PROCESS =====^^J"
        \asy@cmdsep
        \ifx\@tempa\@empty\else
          cd \@tempa
          \asy@cmdsep
        \fi
        \ifpdf pdf\fi latex
          -shell-escape
          -interaction=batchmode
          -jobname=\asy@filename-comp
        \unexpanded{%
          "\RequirePackage{asyprocess}\ProcessAsy
           \documentclass{article}
           \begin{document}\ShowAsy
           \end{document}"
        }%
        \asy@cmdsep
        echo "^^J==== ASY END PROCESS ===^^J"
      }%
    }{}%
    \import{\asy@pathprefix\asy@path}{\asy@filename\string_.tex}%
  }{%
    \PackageWarning{asyfig}{%
      ^^J\space\space
      "\asy@pathprefix\asy@path\asy@filename.asy" not found.^^J%
      This warning occurred%
    }%
  }%
  \let\asy@process\@False
}

% The starred version of \cmd\asyfig\ processes the graphic always:
\WithSuffix\newcommand\asyfig*[1]{%
  \begingroup
    \let\asy@process\@True
    \csname\NoSuffixName\asyfig\endcsname{#1}%
  \endgroup
}

