% #!rm -f tigerpsdfmt4* && ptex2pdf -l -u -ot '-shell-escape' -od '-v' test-gin-rule-psd
% #!rm -f tigerpsdfmt4* && xelatex -shell-escape test-gin-rule-psd
% #!rm -f tigerpsdfmt4* && pdflatex -shell-escape test-gin-rule-psd
%#!rm -f tigerpsdfmt4* && lualatex -shell-escape test-gin-rule-psd
%%
%% This is file 'graphicxpsd.sty'.
%%
%% Copyright (c) 2017, 2018 Munehiro Yamamoto
%% 
%%   * e-mail:   munepixyz@gmail.com, munepi@greencherry.jp
%%   * GitHub:   https://github.com/munepi
%%   * Twitter:  @munepixyz
%% 
%% This package is licensed under the terms of the MIT License.
%%
\ProvidesPackage{graphicxpsd}
  [2018/01/26 v1.0  Adobe Photoshop Data format (PSD) support for graphicx package
                    with sips (Darwin/macOS)/convert (ImageMagick) command]

%% declare package errors
\def\gfxpsd@error{\PackageError{graphicxpsd}}
\def\gfxpsd@warning{\PackageWarning{graphicxpsd}}
\def\gfxpsd@warningnoline{\PackageWarningNoLine{graphicxpsd}}
\def\gfxpsd@info{\PackageInfo{graphicxpsd}}

%% support major graphicx drivers
\newif\if@gfxpsd@dvipdfmx \@gfxpsd@dvipdfmxfalse
\newif\if@gfxpsd@pdftex \@gfxpsd@pdftexfalse
\DeclareOption{dvipdfmx}{\@gfxpsd@dvipdfmxtrue\@gfxpsd@pdftexfalse}
\DeclareOption{xetex}{\@gfxpsd@dvipdfmxtrue\@gfxpsd@pdftexfalse}
\DeclareOption{luatex}{\@gfxpsd@dvipdfmxfalse\@gfxpsd@pdftextrue}
\DeclareOption{pdftex}{\@gfxpsd@dvipdfmxfalse\@gfxpsd@pdftextrue}

%% support two psd-to-pdf command: sips (Darwin/macOS), convert (ImageMagick)
\def\@gfxpsd@psdtopdf{sips}%% as default
\DeclareOption{imagemagick}{\gdef\@gfxpsd@psdtopdf{convert}}
\DeclareOption{sips}{\gdef\@gfxpsd@psdtopdf{sips}}

\ProcessOptions\relax

%% support the extension .psd (psdtopdf)
\if@gfxpsd@dvipdfmx
  \@namedef{Gin@rule@.PSD}#1{{psdtopdf}{.xbb}{#1}}
  \@namedef{Gin@rule@.psd}#1{{psdtopdf}{.xbb}{#1}}
\else\if@gfxpsd@pdftex
  \@namedef{Gin@rule@.PSD}#1{{psdtopdf}{.pdf}{#1}}
  \@namedef{Gin@rule@.psd}#1{{psdtopdf}{.pdf}{#1}}
\else
  \grnchry@error{Please choice specific dviware driver from the following list
    and set the one as a documentclass option.^^J
    available dviware drivers: dvipdfmx, xetex, luatex, pdftex}%
\fi\fi

%% then, include .psd file as converted PDF
\let\Ginclude@psdtopdf\Ginclude@pdf

%% compatibility for TL16 or lower version
\@ifundefined{GPT@space}{%
  \def\GPT@space{ }%
}{}

%% check shell escape trigger
\IfFileExists{shellesc.sty}{%%is bundled in TL15 or higher release version
\RequirePackage{shellesc}}{%%otherwise, such TL has no luatex.
  \protected\def\ShellEscape{\immediate\write18 }%%compatibility for TL14 or lower version
}

%% read .psd file
\let\clone@Gread@pdf\Gread@pdf
\def\Gread@psdtopdf#1{%
  \typeout{converting: \Gin@base\Gin@ext\GPT@space -> \Gin@base4gfxpsd.pdf}%
  \def\gfxpsd@tmp{sips}\ifx\@gfxpsd@psdtopdf\gfxpsd@tmp
    \ShellEscape{sips -s format pdf \Gin@base\Gin@ext\GPT@space --out \Gin@base4gfxpsd.pdf}%
  \else\def\gfxpsd@tmp{convert}\ifx\@gfxpsd@psdtopdf\gfxpsd@tmp
    \ShellEscape{convert \Gin@base\Gin@ext\GPT@space\Gin@base4gfxpsd.pdf}%
  \fi\fi
  %% renamed inside: foo.psd -> foo4gfxpsd.pdf
  \let\clone@Gin@base\Gin@base
  \def\Gin@base{\clone@Gin@base4gfxpsd}%
  \def\Gin@ext{.pdf}%
% \typeout{!!! #1}%%=> tigerpsdfmt4gfxpsd.xbb
% \typeout{!!! \Gin@base\Gin@ext}%%=> tigerpsdfmt4gfxpsd.pdf
  \clone@Gread@pdf{#1}}

\endinput
%%
%% End of file `graphicxpsd.sty'.
