%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% --------------------------------------------------------------
%% This file is a part of the bibstyle-ru package.
%% Made by Alexey Kuznetsov (mailtokuznetsov@gmail.com)
%% Version 1
%% --------------------------------------------------------------
\ProvidesPackage{bibstyles-ru}[v.1]
\PackageInfo{bibstyles-ru}{Russian bibliography styles} 

\RequirePackage{ifthen}
\newboolean{@bibru@authoryear}
\newboolean{@bibru@noautosup}
\newboolean{@bibru@supbib}
\newboolean{@bibru@super}

\DeclareOption{author&year}{\setboolean{@bibru@authoryear}{true}}
\DeclareOption{noautosup}{\setboolean{@bibru@noautosup}{true}}
\DeclareOption{supbib}{\setboolean{@bibru@supbib}{true}}
\DeclareOption{super}{\setboolean{@bibru@super}{true}}

\def\@bibru@style{gostd}

\DeclareOption{gost}{\def\@bibru@style{gost}}
\DeclareOption{gostl}{\def\@bibru@style{gostl}}
\DeclareOption{gosts}{\def\@bibru@style{gosts}}
\DeclareOption{gostls}{\def\@bibru@style{gostls}}
\DeclareOption{gostd}{\relax}
\DeclareOption{gostdl}{\def\@bibru@style{gostdl}}
\DeclareOption{gostds}{\def\@bibru@style{gostds}}
\DeclareOption{gostdls}{\def\@bibru@style{gostdls}}
\DeclareOption{brief-ru}{\def\@bibru@style{brief-ru}}
\DeclareOption{concise-ru}{\def\@bibru@style{concise-ru}}

\DeclareOption{gostns}{\def\@bibru@style{gostns}\setboolean{@bibru@authoryear}{true}}
\DeclareOption{gostlns}{\def\@bibru@style{gostlns}\setboolean{@bibru@authoryear}{true}}
\DeclareOption{gostdns}{\def\@bibru@style{gostdns}\setboolean{@bibru@authoryear}{true}}
\DeclareOption{gostdlns}{\def\@bibru@style{gostdlns}\setboolean{@bibru@authoryear}{true}}
\DeclareOption{briefns-ru}{\def\@bibru@style{briefns-ru}\setboolean{@bibru@authoryear}{true}}
\DeclareOption{concisens-ru}{\def\@bibru@style{concisens-ru}\setboolean{@bibru@authoryear}{true}}

\DeclareOption*{\let\@bibru@style\CurrentOption
\PackageInfo{bibstyles-ru}{\MessageBreak
   The bibliography style `\CurrentOption'    is used\MessageBreak}}
\ProcessOptions\relax

\bibliographystyle{\@bibru@style}

\ifthenelse{\boolean{@bibru@authoryear}}
  {  \RequirePackage[sort]{natbib}
     \citestyle{agu}
     \let\cite\citep 
  }{ \ifthenelse{\boolean{@bibru@supbib}}
       {  \RequirePackage[unicode,linktocpage]{hyperref}
       }{ \RequirePackage[unicode,linktocpage]{hyperref}
          \ifthenelse{\boolean{@bibru@super}}
             {  \RequirePackage[numbers,sort&compress,super]{natbib}
                \def\@biblabel#1{\textsuperscript{#1\thinspace}\kern-\labelsep}
             }{ \RequirePackage[numbers,sort&compress]{natbib}
                \setcitestyle{citesep={,\kern-0.2em}}
                \def\@biblabel#1{#1.}
  } } } 

\def\supbibprefix#1{\def\@bibru@prefix@letter{#1}}
\supbibprefix{A}
\def\supbibname#1{\def\refnamesupplemental{#1}}
\def\@bibru@prefix{}

\ifthenelse{\boolean{@bibru@supbib}}%
  { \ifthenelse{\boolean{@bibru@noautosup}}{}{
      \RequirePackage{shellesc}
      \IfFileExists{supplemental.aux}{\ShellEscape{bibtex supplemental.aux}}{\relax} }
    \RequirePackage[resetlabels]{multibib}
    \newcites{supplemental}{\mbox{Список работ, опубликованных автором по теме диссертации}}
    \bibliographystylesupplemental{\@bibru@style}
    \let\supplementalbib\bibliographysupplemental
    \let\citeS\citesupplemental
    \let\nociteS\nocitesupplemental
    \ifthenelse{\boolean{@bibru@authoryear}}
      {}{ \ifthenelse{\boolean{@bibru@super}}
            {  \def\@biblabel#1{\textsuperscript{\@bibru@prefix#1\thinspace}\kern-\labelsep}
               \def\@citex[#1]#2{\leavevmode
                    \let\@citea\@empty
                    \@cite{\@for\@citeb:=#2\do
                      {\@citea\def\@citea{,\penalty\@m}%
                       \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
                       \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
                       \@ifundefined{b@\@citeb}{\hbox{\reset@font\bfseries ?}%
                         \G@refundefinedtrue
                         \@latex@warning
                           {Citation `\@citeb' on page \thepage \space undefined}}%
                   {\@cite@ofmt{\csname b@\@citeb\endcsname}}}}{#1}}
               \def\@cite#1#2{\textsuperscript{#1\if@tempswa,#2\fi}}
            }{ \def\@biblabel#1{\@bibru@prefix#1.} }
          \AddToHook{cmd/supplementalbib/before}{\def\@bibru@prefix{\@bibru@prefix@letter}}
          \AddToHook{cmd/supplementalbib/after}{\def\@bibru@prefix{}}
          \def\@bibitem#1{\@skiphyperreftrue\H@item\@skiphyperreffalse
            \Hy@raisedlink{\hyper@anchorstart{cite.#1\@extra@b@citeb}\relax\hyper@anchorend}%
            \if@filesw\begingroup
               \let\protect\noexpand
               \immediate\write\@auxout{\string\bibcite{#1}{\@bibru@prefix\the\value{\@listctr}}}%
            \endgroup\fi
            \ignorespaces} }
  }{  } 
