%% Copyright (C) 2008-2018 Achim D. Brucker, https://www.brucker.ch
%%
%% License:
%%   This program can be redistributed and/or modified under the terms
%%   of the LaTeX Project Public License Distributed from CTAN
%%   archives in directory macros/latex/base/lppl.txt; either
%%   version 1.3c of the License, or (at your option) any later version.
%%   OR
%%   The 2-clause BSD-style license.
%%
%%   SPDX-License-Identifier: LPPL-1.3c+ OR BSD-2-Clause
\NeedsTeXFormat{LaTeX2e}\relax
\ProvidesPackage{authorarchive}
  [0000/00/00 Unreleased v1.0.0+%
  Self-archiving information for scientific publications.]
%
\PassOptionsToPackage{hyphens}{url}
%
\RequirePackage{ifthen}
\RequirePackage[inline]{enumitem}
\RequirePackage{graphicx}
\RequirePackage{eso-pic}
\RequirePackage{intopdf}
\RequirePackage{kvoptions}
\RequirePackage{hyperref}
\RequirePackage{calc}
\RequirePackage{qrcode}
\RequirePackage{dtk-logos}
%
%Better url breaking
\g@addto@macro{\UrlBreaks}{\UrlOrds}
%
% Option declarations
% -------------------
\SetupKeyvalOptions{
  family=AA,
  prefix=AA@
}
%
\DeclareStringOption[.]{bibtexdir}
\DeclareStringOption[https://duckduckgo.com/?q=]{baseurl}
\DeclareStringOption[.pdf]{suffix}
\DeclareStringOption[UNKNOWN PUBLISHER]{publisher}[]
\DeclareStringOption[UNKNOWN YEAR]{year}[]
\DeclareStringOption[]{key}[]
\DeclareStringOption[]{doi}[]
\DeclareStringOption[]{doiText}[]
\DeclareStringOption[]{publisherurl}[]
\DeclareStringOption[UNKNOWN START PAGE]{startpage}[]
\DeclareStringOption[UNKNOWN PUBLICATION]{publication}[]

\DeclareBoolOption{ACM}
\DeclareBoolOption{acmart}
\DeclareBoolOption{ENTCS}
\DeclareBoolOption{IEEE}
\DeclareBoolOption{LNCS}
\DeclareBoolOption{LNI}
\DeclareBoolOption{nocopyright}
\DeclareBoolOption{nourl}
\DeclareBoolOption{nobib}
\DeclareBoolOption{orcidicon}
%\ProcessOptions\relax


% Default option rule
\DeclareDefaultOption{%
  \ifx\CurrentOptionValue\relax
    \PackageWarningNoLine{\@currname}{%
      Unknown option `\CurrentOption'\MessageBreak
      is passed to package `authorarchive'%
    }%
    % Pass the option to package color.
    % Again it is better to expand \CurrentOption.
    \expandafter\PassOptionsToPackage\expandafter{\CurrentOption}{color}%
  \else
    % Package color does not take options with values.
    % We provide the standard LaTeX error.
    \@unknownoptionerror
  \fi
}
\ProcessKeyvalOptions*

% Provide command for dynamic configuration seutp
\def\authorsetup{\kvsetkeys{AA}}

% Load local configuration
\InputIfFileExists{authorarchive.config}{}{}


\newlength\AA@x
\newlength\AA@y
\newlength\AA@width

\def\AA@bibBibTeX{\AA@bibtexdir/\AA@key.bib}
\def\AA@bibWord{\AA@bibtexdir/\AA@key.word.xml}
\def\AA@bibEndnote{\AA@bibtexdir/\AA@key.enw}
\def\AA@bibRIS{\AA@bibtexdir/\AA@key.ris}
\newboolean{AA@bibExists}
\setboolean{AA@bibExists}{false}
\IfFileExists{\AA@bibBibTeX}{\setboolean{AA@bibExists}{true}}{}
\IfFileExists{\AA@bibWord}{\setboolean{AA@bibExists}{true}}{}
\IfFileExists{\AA@bibEndnote}{\setboolean{AA@bibExists}{true}}{}
\IfFileExists{\AA@bibRIS}{\setboolean{AA@bibExists}{true}}{}



\setlength\AA@x{1in+\hoffset+\oddsidemargin}

\newcommand{\authorcrfont}{\footnotesize}
\newcommand{\authorat}[1]{\AtPageUpperLeft{\put(\LenToUnit{\AA@x},\LenToUnit{.2cm-\paperheight}){#1}}}
\newcommand{\authorwidth}[1]{\setlength{\AA@width}{#1}}
\setlength{\AA@width}{\textwidth}

\def\AA@pageinfo{}
\ifthenelse{\equal{\AA@startpage}{UNKNOWN START PAGE}}{%
}{%
  \setcounter{page}{\AA@startpage}%
  \def\AA@pageinfo{pp. \thepage--\pageref{\aa@lastpage}, }
}



%%%% sig-alternate.cls
\ifAA@ACM%
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=ACM}
  }{}
  \global\boilerplate={}
  \global\copyrightetc={}
  \renewcommand{\conferenceinfo}[2]{}
  \renewcommand{\authorcrfont}{\scriptsize}
  \setlength\AA@x{1in+\hoffset+\oddsidemargin}
  \setlength\AA@y{-\textheight+\topmargin+\headheight-\footskip} % -\voffset-\topmargin-\headheight-\footskip}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},\LenToUnit{\AA@y}){#1}}
  \setlength{\AA@width}{\columnwidth}
\fi
%
%%%% acmart.cls
\ifAA@acmart%
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=ACM}
  }{}
  \renewcommand{\authorat}[1]{\AtPageUpperLeft{\put(\LenToUnit{\AA@x},\LenToUnit{0.2cm-\paperheight}){#1}}}
  \setlength{\AA@width}{\textwidth}
\fi
%
%%%% LNCS
\ifAA@LNCS%
  \ifAA@orcidicon%
    \renewcommand{\orcidID}[1]{\href{https://orcid.org/#1}{%
      \textsuperscript{\,\includegraphics[height=2\fontcharht\font`A]{vector_iD_icon}}}}
  \else\relax\fi%
%  
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=Springer-Verlag}
  }{}
  \renewcommand{\authorcrfont}{\scriptsize}
  \@ifclasswith{llncs}{a4paper}{%
    \pdfpagesattr{/CropBox [92 114 523 780]}%
    \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},40){#1}}%
  }{%
    \pdfpagesattr{/CropBox [92 65 523 731]}% LNCS page: 152x235 mm
    \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},23){#1}}
  }
  \setlength{\AA@width}{\textwidth}
  \setcounter{tocdepth}{2}
\fi
%
%%%% LNI
\ifAA@LNI%
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=GI}
  }{}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},35){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \pdfpagesattr{/CropBox [70 65 526.378 748.15]} % TODO
  \setlength{\AA@width}{\textwidth}
  \setcounter{tocdepth}{2}
\fi
%
%%%% ENTCS
\ifAA@ENTCS%
  \addtolength{\voffset}{1cm}
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=Elsevier Science B.~V.}
  }{}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},\LenToUnit{-.5cm-\the\ht\AA@authoratBox}){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \setlength{\AA@width}{\textwidth}
\fi
%
%%%% IEEE
\ifAA@IEEE%
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=IEEE}
  }{}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},6){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \setlength{\AA@width}{\textwidth}
  \setcounter{tocdepth}{2}
\fi
%

\hypersetup{%
  draft         = false,
  bookmarksopen = true,
  bookmarksnumbered= true,
  pdfauthor     = {\@author},
  pdftitle      = {\@title},
}

\@ifpackageloaded{totpages}{%
  \def\aa@lastpage{TotPages}
}{%
  \RequirePackage{lastpage}
  \def\aa@lastpage{LastPage}
}
\newsavebox{\AA@authoratBox}

\AddToShipoutPicture*{%
  \setlength{\unitlength}{1mm}%
  \savebox{\AA@authoratBox}{%
    \parbox{1.4cm}{%
      \bgroup%
        \normallineskiplimit=0pt%
        \ifAA@nourl%
           \ifx\AA@doi\@empty%
             \hspace{0.585cm}
           \else%
              \qrcode[hyperlink,height=1.17cm,padding]{https://doi.org/\AA@doi}%
            \fi%
        \else%
          \qrcode[hyperlink,height=1.17cm,padding]{\AA@baseurl/\AA@key\AA@suffix}%
        \fi%
      \egroup%
    }%
    \parbox{\AA@width-1.4cm}{\authorcrfont%
      \ifAA@LNCS%
        \AA@publication, \AA@pageinfo  \AA@year. %
        \ifAA@nocopyright\else
          \textcopyright~\AA@year~\AA@publisher.
        \fi
        This is the author's
        version of the work. It is posted
        \ifAA@nourl\relax\else%
          at \url{\AA@baseurl/\AA@key\AA@suffix} %
        \fi
        \ifAA@nocopyright\relax\else
          by permission of \AA@publisher{}
        \fi
        for your personal use.
        \ifx\AA@doi\@empty%
          \relax
        \else
          The final publication is available at Springer via
          \ifx\AA@doiText\@empty%
            \url{https://doi.org/\AA@doi}.
          \else
            \href{https://doi.org/\AA@doi}{\AA@doiText}.
          \fi
        \fi
      \else
        \ifAA@nocopyright\relax\else
          \textcopyright~\AA@year~\AA@publisher. %
        \fi%
        This is the author's
        version of the work. It is posted
        \ifAA@nourl\relax\else%
          at \url{\AA@baseurl/\AA@key\AA@suffix} %
        \fi
        \ifAA@nocopyright\relax\else
          by permission of \AA@publisher{} %
        \fi
        for your personal use. Not for redistribution. The definitive
        version was published in \emph{\AA@publication}, \AA@pageinfo \AA@year%
        \ifx\AA@doi\@empty%
          \ifx\AA@publisherurl\@empty%
            .%
          \else
            \url{\AA@publisherurl}.%
          \fi
        \else
          \ifx\AA@doiText\@empty%
            , doi: \href{https://doi.org/\AA@doi}{\AA@doi}.%
          \else
            , doi: \href{https://doi.org/\AA@doi}{\AA@doiText}.%
          \fi
        \fi
      \fi
      \ifAA@nobib\relax\else%
        \ifthenelse{\boolean{AA@bibExists}}{%
          \hfill
          \begin{itemize*}[label={}, itemjoin={,}]
            \IfFileExists{\AA@bibBibTeX}{%
              \item \attachandlink{\AA@bibBibTeX}[application/x-bibtex]{BibTeX entry of this paper}{\BibTeX}%
            }{
              \typeout{No file \AA@bibBibTeX found. Not embedded reference in BibTeX format.}
            }
            \IfFileExists{\AA@bibWord}{%
              \item \attachandlink{\AA@bibWord}[application/xml]{XML entry of this paper (e.g., for Word 2007 and later)}{Word}%
            }{
              \typeout{No file \AA@bibWord found. Not embedded reference for Word 2007 and later.}
            }
            \IfFileExists{\AA@bibEndnote}{%
              \item \attachandlink{\AA@bibEndnote}[application/x-endnote-refer]{Endnote entry of this paper}{EndNote}%
            }{
              \typeout{No file \AA@bibEndnote found. Not embedded reference in Endnote format.}
            }
            \IfFileExists{\AA@bibRIS}{%
              \item \attachandlink{\AA@bibRIS}[application/x-research-info-systems]{RIS entry of this paper}{RIS}%
            }{
              \typeout{No file \AA@bibRIS found. Not embedded reference in RIS format.}
            }
          \end{itemize*}\\
        }{%
          \PackageError{authorarchive}{No bibliographic files found. Specify option 'nobib' if this is intended.}
        }
      \fi
    }
  }
  \authorat{\raisebox{\the\ht\AA@authoratBox}{\usebox{\AA@authoratBox}}}
}
