\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{acrn}[2022/09/06 acrn v1.0.0]
\RequirePackage{adjustbox}
\RequirePackage{etoolbox}

% acrn: auto copyright notice

%% Copyright (c) 2022 Vincent Kuhlmann

\newcount\acrn@increment
\acrn@increment=1\relax


\acrn@increment=0\relax

\newcount\acrn@cutoff
\acrn@cutoff=0\relax

\def\acrn@ids{}

\def\acrnAllAuthorsTrue{
    \acrn@cutoff=-1\relax
}

\def\acrnAllAuthorsFalse{
    \acrn@cutoff=0\relax
}

\DeclareOption{allauthors}{
    \acrnAllAuthorsTrue
}


\ProcessOptions

% ====== Defining new names =====

\def\acrn@defname#1#2#3{
    \edef\acrn@id{\expandafter\@gobble\string #1}
    \expandafter\def\expandafter\countname\expandafter{\csname acrn@count@\acrn@id\endcsname}

    \newcount\@copyrightCounter
    \expandafter\let\countname\@copyrightCounter

    \def\definecounterincrement##1{
        \def#1{%
            \advance##1\acrn@increment
        }
    }
    \expandafter\definecounterincrement\expandafter{\countname}

    \expandafter\def\csname acrn@info@\acrn@id\endcsname{{#2}{#3}}

    \expandafter\listadd\expandafter\acrn@ids\expandafter{\acrn@id}
}

% #1: Command to increment the copyright counter of the author
% #2: Author name
% #3: Year
\let\acrnDefName\acrn@defname



% ====== Sorting =====

\newcount\acrn@scoretobeat

% #1: callback
\def\acrn@getsorted#1{
    \begingroup
    %
    \expandafter\def\expandafter\templist\expandafter{\acrn@ids}%
    \def\onresult##1{%
        \listremove\templist{##1}%
        #1{##1}%
    }%
    %
    \def\@pophighest{%
        \acrn@scoretobeat=\acrn@cutoff\relax
        \let\currentHighest\relax
        \let\@prevdo\do
        \def\do####1{
            \expandafter\ifnum\csname acrn@count@####1\endcsname>\acrn@scoretobeat
                \def\currentHighest{####1}%
                \expandafter\acrn@scoretobeat\csname acrn@count@####1\endcsname
            \fi
        }
        \dolistloop{\templist}
        \let\do\@prevdo
        \unless\ifx\currentHighest\relax
            \expandafter\onresult\expandafter{\currentHighest}%
            \@pophighest
        \fi
    }%
    \@pophighest
    \endgroup
}

\def\acrnSorted{}
\newbool{isCopyrightEmpty}

% Create list of authors in descending order of frequency. Results in:
% - The etoolbox list \arcnSorted, and
% - The etoolbox boolean isCopyrightEmpty, indicating if \arcnSorted is empty
\def\acrnPrepare{%
    \def\acrnSorted{}
    \global\setbool{isCopyrightEmpty}{true}%
    \def\do##1{%
        \global\setbool{isCopyrightEmpty}{false}%
        \listgadd\acrnSorted{##1}%
    }%    
    \acrn@getsorted{\do}%
}

% ====== Printing =====

% #1: Author name
% #2: Year
% #3: Id
\def\acrnCopyrightline#1#2#3{
    Copyright (c) #2 #1
}

% #1: Id
\def\acrn@copyrightline@i#1{%
    \PackageInfo{acrn}{Copyright frequency of #1: \expandafter\the\csname acrn@count@#1\endcsname}%
    \typeout{[acrn] Copyright frequency of #1: \expandafter\the\csname acrn@count@#1\endcsname}
    \expandafter\expandafter\expandafter\acrnCopyrightline\csname acrn@info@#1\endcsname{#1}%
}

% Expands to \acrnCopyrightline for each author, in descending order of frequency
\def\copyrightlines{%
    \begingroup
    \let\do\acrn@copyrightline@i
    \dolistloop{\acrnSorted}%
    \endgroup
}

% Print \copyrightlines underneath each other
\def\printcopyright{
    \def\acrnCopyrightline##1##2##3{%
        \item Copyright (c) ##2 ##1\relax
    }
    \acrnPrepare
    \ifbool{isCopyrightEmpty}{
        Empty
    }{
        \ttfamily
        \begin{list}{}{
            \setlength{\leftmargin}{20pt}
            \itemindent-\leftmargin
            \setlength{\itemsep}{0pt}
            \setlength{\parskip}{0pt}
        }
            \copyrightlines
        \end{list}
    }
}



