%% 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 of the License, or any later version.
%%   OR
%%   The 2-clause BSD-style license.
\NeedsTeXFormat{LaTeX2e}\relax
\ProvidesPackage{authorarchive}
  [2018/02/25 Unreleased v1.0.0+%
  Self-archiving information for scientific publications.]
%
\RequirePackage{ifthen}
\RequirePackage{graphicx}
\RequirePackage{hyperref}
\RequirePackage{eso-pic}
\RequirePackage{embedfile}
\RequirePackage{kvoptions}
\RequirePackage{calc}
\RequirePackage{qrcode}
%
% Option declarations
% -------------------
\SetupKeyvalOptions{
  family=AA,
  prefix=AA@
}

\DeclareStringOption[.]{bibtexdir}
\DeclareStringOption[https://duckduckgo.com/?q=]{baseurl}
\DeclareStringOption[.pdf]{suffix}
\DeclareStringOption[UNKOWN PUBLISHER]{publisher}[]
\DeclareStringOption[UNKOWN YEAR]{year}[]
\DeclareStringOption[]{key}[]
\DeclareStringOption[]{doi}[]
\DeclareStringOption[]{doiText}[]
\DeclareStringOption[]{publisherurl}[]
\DeclareStringOption[1]{startpage}[]
\DeclareStringOption[UNKNOWN PUBLICATION]{publication}[]

\DeclareBoolOption{ACM}
\DeclareBoolOption{ENTCS}
\DeclareBoolOption{IEEE}
\DeclareBoolOption{LNCS}
\DeclareBoolOption{LNI}
\DeclareBoolOption{nocopyright}
\DeclareBoolOption{nourl}
\DeclareBoolOption{nobib}
%\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

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

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

\setcounter{page}{\AA@startpage}

\ifAA@ACM%
  \setkeys{AA}{publisher=ACM}
  \renewcommand{\authorat}[1]{\AtPageUpperLeft{\put(\LenToUnit{\AA@x},\LenToUnit{-\paperheight}){#1}}}
  \setlength{\AA@width}{\textwidth}
\fi
%%%% LNCS
\ifAA@LNCS%
  \setkeys{AA}{publisher=Springer-Verlag}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},27){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \pdfpagesattr{/CropBox [92 65 523 731]}% LNCS page: 152x235 mm
  \setlength{\AA@width}{\textwidth}
  \setcounter{tocdepth}{2}
\fi
%
%%%% LNI
\ifAA@LNI%
  \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}
  \setkeys{AA}{publisher=Elsevier Science B.~V.}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},-250){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \def\@oddfoot{}
  \def\@evenfoot{}
  \def\firstfootline{}
  \headsep 8pt
  \renewcommand\rightheadline{\ifnum\value{page}=\hypergetpageref{FirstPage}\relax
    \else{\hfil\scriptsize\emph{\lastname / \AA@publication} \hfil {\rmfamily\thepage}}\fi}
  \renewcommand\leftheadline{\scriptsize{\rmfamily\thepage} \hfil \emph{\lastname /
      \AA@publication}\hfil}
  %  467.717 x 680.315 pts
  % \pdfpagesattr{/CropBox [70 110 535 850]}
  \pdfpagesattr{/CropBox [70 95 535 775]}
  \setlength{\AA@width}{\textwidth}
\fi
%
%%%% IEEE
\ifAA@IEEE%
  \setkeys{AA}{publisher=IEEE Computer Society}
  \renewcommand{\authorat}[1]{\put(\LenToUnit{\AA@x},12){#1}}
  \renewcommand{\authorcrfont}{\scriptsize}
  \setlength{\AA@width}{\textwidth}
  \setcounter{tocdepth}{2}
\fi
%

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

\ifAA@nobib\relax\else%
  \AtBeginDocument{%
    \IfFileExists{\AA@bibtexdir/\AA@key.bib}{%
      \embedfile[filespec=\AA@key.bib,%
        desc={BibTeX entry of this paper.},%
        stringmethod=escape,%
        mimetype=plain/text,%
      ]{\AA@bibtexdir/\AA@key.bib}}{
      \typeout{No file \AA@bibtexdir/\AA@key.bib found. Not embedded reference in BibTeX format.}
    }%
    \IfFileExists{\AA@bibtexdir/\AA@key.enw}{%
      \embedfile[filespec=\AA@key.enw,%
        desc={Endnote entry of this paper.},%
        stringmethod=escape,%
        mimetype=plain/text,%
      ]{\AA@bibtexdir/\AA@key.enw}}{
      \typeout{No file \AA@bibtexdir/\AA@key.enw found. Not embedded reference in Endnote format.}
    }%
    \IfFileExists{\AA@bibtexdir/\AA@key.ris}{%
      \embedfile[filespec=\AA@key.ris,%
        desc={RIS entry of this paper.},%
        stringmethod=escape,%
        mimetype=plain/text,%
      ]{\AA@bibtexdir/\AA@key.ris}}{
      \typeout{No file \AA@bibtexdir/\AA@key.ris found. Not embedded reference in RIS format.}
    }%
    \IfFileExists{\AA@bibtexdir/\AA@key.word.xml}{%
      \embedfile[filespec=\AA@key.word.xml,%
        desc={XML entry of this paper (e.g., for Word 2007 and later).},%
        stringmethod=escape,%
        mimetype=plain/text,%
      ]{\AA@bibtexdir/\AA@key.word.xml}}{
      \typeout{No file \AA@bibtexdir/\AA@key.word.xml found. Not embedded reference for Word 2007 and later.}
    }%
  }%
\fi
\AtEndDocument{\label{LastPage}}

\AddToShipoutPicture*{%
  \setlength{\unitlength}{1mm}%
  \authorat{%
    \ifAA@nourl%
       \ifx\AA@doi\@empty%
         \hspace{0.585cm}
       \else%
          \raisebox{.5cm}[.5cm]{\qrcode[hyperlink,height=1.17cm,padding]{https://doi.org/\AA@doi}}%
        \fi%
    \else%
      \raisebox{.5cm}[.5cm]{\qrcode[hyperlink,height=1.17cm,padding]{\AA@baseurl/\AA@key\AA@suffix}}%
    \fi%
    \authorcrfont\parbox[b]{\AA@width-1.4cm}{
      \ifAA@LNCS%
        \AA@publication, pp. \thepage--\pageref{LastPage}, \AA@year.
        \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.
        \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}, pp.~\thepage--\pageref{LastPage}, \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
    }
  }
}
