%% Copyright (C) 2008-2023 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}
  [2023/02/10 v1.3.0
  Self-archiving information for scientific publications.]
%
\PassOptionsToPackage{hyphens}{url}
%
\RequirePackage{ifthen}
\RequirePackage[inline]{enumitem}
\RequirePackage{orcidlink}
\RequirePackage{eso-pic}
\RequirePackage{intopdf}
\RequirePackage{kvoptions}
\RequirePackage{hyperref}
\RequirePackage{calc}
\RequirePackage{qrcode}
\RequirePackage{etoolbox}
\newrobustcmd\BibTeX{Bib\TeX}
%
%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*

\newcommand{\AA@defIncludeFiles}{
  \def\AA@bibBibTeX{\AA@bibtexdir/\AA@key.bib}
  \def\AA@bibBibTeXLong{\AA@bibtexdir/\AA@key.bibtex}
  \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}
}
\AA@defIncludeFiles

\newboolean{AA@bibExists}
\setboolean{AA@bibExists}{false}
\newcommand{\AA@defIncludeSwitches}{
  \IfFileExists{\AA@bibBibTeX}{\setboolean{AA@bibExists}{true}}{}
  \IfFileExists{\AA@bibBibTeXLong}{\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}}{}
}
\AA@defIncludeSwitches


% Provide command for dynamic configuration setup
% \def\authorsetup{\kvsetkeys{AA}}
\newcommand{\authorsetup}[1]{%
  \kvsetkeys{AA}{#1}
  \AA@defIncludeFiles
  \AA@defIncludeSwitches
}

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

%  define proxy command for setting PDF attributes
\ExplSyntaxOn
\@ifundefined{pdfmanagement_add:nnn}{%
    \newcommand{\AA@pdfpagesattribute}[2]{\pdfpagesattr{/#1 #2}}%
 }{%
    \newcommand{\AA@pdfpagesattribute}[2]{\pdfmanagement_add:nnn{Pages}{#1}{#2}}%
 }%
\ExplSyntaxOff

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

\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]{\orcidlink{#1}}
  \else\relax\fi%
%  
  \ifthenelse{\equal{\AA@publisher}{UNKNOWN PUBLISHER}}{%
    \setkeys{AA}{publisher=Springer-Verlag}
  }{}
  \renewcommand{\authorcrfont}{\scriptsize}
  \@ifclasswith{llncs}{a4paper}{%
    \AA@pdfpagesattribute{CropBox}{[92 114 523 780]}%
    \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},40){#1}}%
  }{%
    \AA@pdfpagesattribute{CropBox}{[92 65 523 731]}%
    \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}
  \AA@pdfpagesattribute{CropBox}{[70 65 526.378 748.15]}
  \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,
}

\@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\relax%
           \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%
    }%
    \ifAA@nourl\ifx\AA@doi\@empty\addtolength{\AA@width}{1.4cm}\fi\fi
    \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 \expanded{\attachandlink[\AA@key.bib]{\AA@bibBibTeX}[application/x-bibtex]{BibTeX entry of this paper}{\BibTeX}}%
            }{%
              \IfFileExists{\AA@bibBibTeXLong}{%
                \item \expanded{\attachandlink[\AA@key.bib]{\AA@bibBibTeXLong}[application/x-bibtex]{BibTeX entry of this paper}{\BibTeX}}%
              }{%
                \typeout{No file \AA@bibBibTeX{} (and no \AA@bibBibTeXLong) found. Not embedded reference in BibTeX format.}%
              }%
            }%
            \IfFileExists{\AA@bibWord}{%
              \item \expanded{\attachandlink[\AA@key.word.xml]{\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 \expanded{\attachandlink[\AA@key.enw]{\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 \expanded{\attachandlink[\AA@key.ris]{\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}}}
}
