% Copyright (C) 2004, 2005, 2006 Arne Jorgensen

% This file is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.

% This file is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.

% You should have received a copy of the GNU General Public License
% along with this file; if not, write to the Free Software Foundation,
% Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

\NeedsTeXFormat{LaTeX2e}[1995/12/01]

% subversion date and revision
\def\dkbib@svn $#1 #2 #3 #4-#5-#6 #7 #8 ${%
  \def\dkbib@sty@revision{#3}
  \def\dkbib@sty@date{#4/#5/#6}
}
\dkbib@svn $Id: dk-bib.sty 92 2006-05-21 21:18:10Z arne $
\def\dkbib@version{0.6}

\ProvidesPackage{dk-bib}[\dkbib@sty@date\space v\dkbib@version\space(r\dkbib@sty@revision) Danish variants of the standard BibTeX styles]

\RequirePackage{xkeyval}

\newif\ifdkbib@url
\dkbib@urlfalse
\DeclareOptionX{url}[true]{%
  \csname dkbib@url#1\endcsname}

\newif\ifdkbib@isbn
\dkbib@isbnfalse
\DeclareOptionX{isbn}[true]{%
  \csname dkbib@isbn#1\endcsname}

\newif\ifdkbib@issn
\dkbib@issnfalse
\DeclareOptionX{issn}[true]{%
  \csname dkbib@issn#1\endcsname}

\newif\ifdkbib@annote
\dkbib@annotefalse
\DeclareOptionX{annote}[true]{%
  \csname dkbib@annote#1\endcsname}

\newif\ifdkbib@printing
\dkbib@printingfalse
\DeclareOptionX{printing}[true]{%
  \csname dkbib@printing#1\endcsname}

\newif\ifdkbib@apalike
\dkbib@apalikefalse
\DeclareOptionX{apalike}[true]{%
  \csname dkbib@apalike#1\endcsname}

\newif\ifdkbib@fixcitedash
\dkbib@fixcitedashtrue
\DeclareOptionX{fixcitedash}[true]{%
  \csname dkbib@fixcitedash#1\endcsname}

\newcount\dkbib@maxordinaldepth
\dkbib@maxordinaldepth0
\newcommand*\dkbib@DeclareOrdinalWord[2]{
  \ifnum#1>\dkbib@maxordinaldepth
    \dkbib@maxordinaldepth#1
  \fi
  \@namedef{dkbib@word#1}{#2}%
}
\dkbib@DeclareOrdinalWord{1}{f\o rste}
\dkbib@DeclareOrdinalWord{2}{anden}
\dkbib@DeclareOrdinalWord{3}{tredje}
\dkbib@DeclareOrdinalWord{4}{fjerde}
\dkbib@DeclareOrdinalWord{5}{femte}
\dkbib@DeclareOrdinalWord{6}{sjette}
\dkbib@DeclareOrdinalWord{7}{syvende}
\dkbib@DeclareOrdinalWord{8}{ottende}
\dkbib@DeclareOrdinalWord{9}{niende}
\dkbib@DeclareOrdinalWord{10}{tiende}
\dkbib@DeclareOrdinalWord{11}{elvte}
\dkbib@DeclareOrdinalWord{12}{tolvte}
\dkbib@DeclareOrdinalWord{13}{trettende}
\dkbib@DeclareOrdinalWord{14}{fjortende}
\dkbib@DeclareOrdinalWord{15}{femtende}
\dkbib@DeclareOrdinalWord{16}{sekstende}
\dkbib@DeclareOrdinalWord{17}{syttende}
\dkbib@DeclareOrdinalWord{18}{attende}
\dkbib@DeclareOrdinalWord{19}{nittende}
\dkbib@DeclareOrdinalWord{20}{tyvende}

\newcount\dkbib@ordinaldepth
\dkbib@ordinaldepth\dkbib@maxordinaldepth
\DeclareOptionX{ordinaldepth}{%
  \dkbib@ordinaldepth#1}

\ifnum\dkbib@ordinaldepth>\dkbib@maxordinaldepth
  \PackageWarningNoLine{dk-bib}{Ordinal depth set to
    \the\dkbib@ordinaldepth, but maximum is \the\dkbib@maxordinaldepth}
\fi

\newif\ifdkbib@ordinalstoword
\dkbib@ordinalstowordfalse
\DeclareOptionX{ordinals2word}[true]{%
  \csname dkbib@ordinalstoword#1\endcsname}

\ProcessOptionsX

% Work around a wrong implementation  of \MakeUppercase (according to
% Morten H\o gholm who also provided this implementation)
\DeclareRobustCommand\dkbib@MakeUppercase[1]{%
  \begingroup
  \def\i{I}\def\j{J}%
  \def\dkbib@reserved@a##1##2{\let##1##2\dkbib@reserved@a}%
  \expandafter\dkbib@reserved@a\@uclclist\dkbib@reserved@b{\dkbib@reserved@b\@gobble}%
  \protected@edef\dkbib@reserved@a{\endgroup\uppercase{#1}}%
  \dkbib@reserved@a
}
\protected@edef\dkbib@MakeUppercase#1{\dkbib@MakeUppercase{#1}}

\AtBeginDocument{%
  %%% fixcitedash
  \@ifpackageloaded{cite}%
    {\ifdkbib@fixcitedash%
        \providecommand{\dkbibdash}{\hbox{-}\penalty\@m}%
        \renewcommand{\citedash}{\dkbibdash}%
      \fi}%
    {\relax}%
  %%% apalike
  \ifdkbib@apalike%
    \RequirePackage{dk-apali}%
  \fi%
  %%% url
  \ifdkbib@url%
    \RequirePackage{url}%
    \providecommand{\dkbiburl}[1]{URL \url{#1}}%
  \else%
    \def\dkbiburl#1{\unskip\@gobble}%
  \fi%
  %%% isbn
  \ifdkbib@isbn%
    \providecommand{\dkbibisbn}[1]{ISBN #1}%
  \else%
    \def\dkbibisbn#1{\unskip\@gobble}%
  \fi%
  %%% issn
  \ifdkbib@issn%
    \providecommand{\dkbibissn}[1]{ISSN #1}%
  \else%
    \def\dkbibissn#1{\unskip\@gobble}%
  \fi%
  %%% annote
  \ifdkbib@annote%
    \providecommand{\dkbibannote}[2]{%
      \begin{quotation}\noindent
        \dkbib@MakeUppercase #2
      \end{quotation}
    }%
  \else%
    \def\dkbibannote#1#2{\unskip}%
  \fi%    
  %%% edition/printing
  \DeclareRobustCommand{\dkbibedition}[2]{%
    \ifx\@empty#1\@empty\else%
      \ifdkbib@ordinalstoword%
        \@ifundefined{dkbib@word#1}%
          {#1\dkbib@addperiod~udgave}%
          {\ifnum#1>\dkbib@ordinaldepth
             #1\dkbib@addperiod~udgave%
           \else
             \@nameuse{dkbib@word#1}~udgave%
           \fi}%
      \else
        #1\dkbib@addperiod~udgave%
      \fi
      \ifdkbib@printing
        \ifx\@empty#2\@empty\else, \fi
      \fi
    \fi
    \ifdkbib@printing
      \ifx\@empty#2\@empty\else
        \ifdkbib@ordinalstoword%
          \@ifundefined{dkbib@word#2}%
            {#2\dkbib@addperiod~oplag}%
            {\ifnum#2>\dkbib@ordinaldepth
               #2\dkbib@addperiod~oplag%
             \else
               \@nameuse{dkbib@word#2}~oplag%
             \fi}%
        \else
          #2\dkbib@addperiod~oplag%
        \fi
      \fi
    \fi
  }
  \DeclareRobustCommand\dkbibEdition[2]{%
    \protected@edef\reserved@a{\csname dkbibedition \endcsname{#1}{#2}}%
    \expandafter\dkbib@MakeUppercase\reserved@a}
}
